Passing in a character string in a linked list struct - c

I have declared my struct as
struct linkedStruct
{
char name;
int size;
enum groupstatus status;
struct linkedStruct* next;
};
I am trying to pass a char* name variable(a string of characters) in my code into a single node of this struct.
For example, user enters in Jon, i want my char name to store the character string Jon.
I am having some trouble figuring how to go about this. I tried just simply stating (assuming my node name is temp)
temp->name = name;
However when I print out the name value when I store it, printf returns nothing.
I originally had it as name[30] in my struct, as we are to assume that the name is less than 30 characters, however this ultimately confused me more because I kept getting errors when trying to store the name into the struct with a for loop.
Other implementation of my struct
struct linkedStruct
{
char name[30]; //now a character array
int size;
enum groupstatus status;
struct linkedStruct* next;
};
The code was
for(int i = 0; i< sizeof(name); i++){
name[i] = temp->name[i];
//seemed totally wrong to do but I had no idea how to go about it
}
I was just wondering how I would go about storing it in either situation, as my current code does not work. In my name[30] implementation, I have no idea how to state/access the array notation of the struct.

It's not possible. A variable of type char has room for exactly one character.
If you know names are limited to 30 characters, you should have
char name[30];
in the structure declaration. Strings in C are copied using strcpy(), so you'd do something like this to set the name inside the structure:
struct linkedStruct temp;
strcpy(temp.name, "hello");

You need:
strcpy(temp->name, name);
Instead of:
temp->name = name;
Which copies a string from the pointer temp->name to memory at pointer name.
Note
You must ensure that name string doesn't contain more than 29 characters.

Related

how to change a variable in an array of struct without changing others

I am trying to change a variable in a struct object. But every time I change it, other objects will change as well.
Here is my struct
struct room{
char * S;
char * N;
char * W;
char * E;
char South;
char North;
char West;
char East;
char * Name;
};
and here is block I ran in main method
int numOfRooms=0;
struct room * rooms;
rooms=(struct room*)malloc(sizeof(*rooms));
do{
rooms=(struct room*)realloc(rooms,sizeof(*rooms)*(numOfRooms+1));
fscanf(fp,"%s%c",name,&temp);
printf("%s ",name);
printf("%d",numOfRooms);
rooms[numOfRooms].Name=name;
printf("%s ",rooms[0].Name);
numOfRooms++;
}while(temp!='\n');
and the output is: START 0START FOYER 1FOYER ELEVATOR 2ELEVATOR
which should be: START 0START FOYER 1START ELEVATOR 2START
that's my problem. Every time I try to change Name in my struct, the Name for previous ones change as well.
name was declared
char string[20];
char defa[16]="No Path This Way";
char temp;
char * input;
char * name=string;
When you do this:
rooms[numOfRooms].Name=name;
You're making Name point to the same thing that name points to. It is not copying the value. This means that all of your room instances have their Name member pointing to the same place.
You should use strdup to create a new dynamically allocated string from the one you use to read the user input:
rooms[numOfRooms].Name=strdup(name);
Be sure to call free on this field when you're done with it.
variable name is not declared.
This is very likely the wrong way to assign a variable. (but since you didn't show the declaration of name, it is hard to be sure)
rooms[numOfRooms].Name=name; // Use Strcpy, strdup, or similar.

C Program crashes after assigning value to string

Relatively new to C, don't see what I'm doing wrong here, this piece of code crashes after assigning the 3rd string:
QW_Be *sentence = (QW_Be*)malloc(sizeof(QW_Be*));
sentence->questionword->word = words[0];
sentence->verb->word = words[1];
sentence->subject->word = words[2]; //crashes here ?
words is an char *[ ], and here's the structure of "sentence":
typedef struct QW_Be{
Word *questionword;
Word *verb;
Word *subject;
Word *rest[];
} QW_Be;
and here's the structure of 'Word':
typedef struct Word{
char *word;
word_type type;
char *context;
} Word;
If you need any more info, just ask !
You should allocate the size of the struct and not the size of a pointer to the struct:
QW_Be *sentence = (QW_Be*)malloc(sizeof(QW_Be));
If you are crashing in an assign value to a string it could be the fact you are assigning a value in a place of the memory who is'nt yours. Besides, make sure to always declare how much memory you want for your string and assign your values in the right place.

C - Incompatible types in assignment - structs and character arrays

Alright so the gist of my situation is that I'm stuck receiving an incompatibles types in assignment when trying to initialize my struct. I am fairly new to C and understanding pointers proves quite the challenge for me but I have looked at similar questions to this error and tried different fixes and have had no luck so far. If someone could fix this for me, you would be my hero.
struct Employee {
char* name[100];
int birth_year;
int starting_year;
};
struct Employee* make_employee(char* name, int birth_year, int starting_year);
int main(){
//some main stuff code
}
struct Employee* make_employee(char* name, int birth_year, int starting_year){
struct Employee* newEmpl = (struct Employee*)malloc(sizeof(struct Employee));
newEmpl->name = name;
newEmpl->birth_year = birth_year;
newEmpl->starting_year = starting_year;
return newEmpl;
}
The assignment errors occurs on the name = name line. I don't know why.
Also if I switch that line with
strcpy(&(newEmpl->name), name);
I get:
warning: passing argument 1 of 'strcpy' from incompatible pointer type
I've tried to find the problem for 2 hours, and no luck, thought I'd give a shot here.
char* name[100];
is an array of pointers to char but:
char* name;
is a pointer to char.
Here:
newEmpl->name = name;
You are trying to assign a pointer to char to the array but you cannot in C assign a pointer to an array! In fact you cannot assign anything to an array in C.
Check you are using the correct types in your program. Are you sure you want to use char *name[100]; and not char name[100]; (an array of char)? Then to copy a string it, use strcpy or strncpy and not the = operator as you cannot assign something to an array.
In your structure, change
char* name[100]; //an array of pointers to character
to
char name[100]; // a character array
Then, in your make_employee() function, instead of
newEmpl->name = name; //arrays cannot be assigned
use
strcpy(newEmpl->name, name); // copy the contains of name to newEmpl->name
or
strncpy(newEmpl->name, name, 99); // limit to 99 elements only + terminating null
Notes:
Please do not cast the return value of malloc() and family.
Please check for the success of malloc() and family before using the returned pointer.

display a specified index of a pointer char*

I am trying to point on a specified character in a string contained on a structure
here my code
typedef struct{
char *name;
int age;
}PERSON, *person;
int main(){
person serenity;
serenity = (person)malloc(sizeof(PERSON));
strcpy(&(serenity->name),"Serenity");
printf("%c",*(&(serenity->name)+1));
}
here i wanted to display the second character which is 'e' but it shows 'n' instead
anyone can explain me what is wrong with this,
thank you
You have not allocated memory for name
typedef struct{
char *name;
int age;
}PERSON, *person;
int main(){
person serenity;
serenity = malloc(sizeof(PERSON));
serenity->name = malloc(sizeof("Serenity")); //<< Missing
strcpy((serenity->name),"Serenity");
printf("%c",*((serenity->name)+1)); // << Also you want the value in pointer name NOT its address
return 0;
}
Outputs e. Also since you tagged C there is no need to cast the return type of malloc.
Okay, okay... All of those answers aside, if you do not aim to change the characters inside the string "Serenity" in the future, you could just do the following:
#include <stdio.h>
typedef struct{
const char *name; // <-- added const
int age;
}PERSON, *person;
int main( ){
person serenity;
serenity = (person) malloc( sizeof( PERSON ) );
serenity->name = "Serenity"; // <-- simply assigned the pointer with the
// address to the array of constant characters
printf( "%c", *( serenity->name + 1 ) ); // <-- changed this
}
This statement
serenity = (person)malloc(sizeof(PERSON));
allocates the structure
typedef struct{
char *name;
int age;
}PERSON
however name is kept uninitialized and points somewhere in memory causing a crash when you copy to it.
So instead of
strcpy(&(serenity->name),"Serenity");
write
serenity->name = strdup("Serenity");
which is the same as
serenity->name = malloc(strlen("Serenity")+1);
strcpy(serenity->name,"Serenity");
don't forget to free that string as well later.
Try printf("%c",*(serenity->name+1));, also do strcpy(serenity->name,"Serenity");.
If you have a pointer char* name; you access the second element by doing name[1] or *(name+1). &name will give you the address where the pointer address of name is stored. This is not what you want here.
Another issue in your program is that you never allocate memory for the variable name. You need a serenity->name = (char*)malloc(128);. But using an arbitrary length like 128 is very dangerous in combination with strcpy. Use strncpy instead of strcpy to work around this.

How to work with string fields in a C struct?

I'm having trouble making a database based on a singly-linked list in C,
not because of the linked list concept but rather the string fields in the struct themselves.
This is an assignment in C and as far as I know (I'm a newbie), C doesn't recognize 'string' as a data type.
This is what my struct code looks like:
typedef struct
{
int number;
string name;
string address;
string birthdate;
char gender;
} patient;
typedef struct llist
{
patient num;
struct llist *next;
} list;
I was thinking of making a struct for the strings themselves so that I can use them in the struct, like this:
typedef struct string
{
char *text;
} *string;
Then I will malloc() each one of them when it is required to make new data of the string type (array of char).
typedef struct string
{
char *text;
} *string;
int main()
{
int length = 50;
string s = (string) malloc(sizeof string);
s->text = (char *) malloc(len * sizeof char);
strcpy(s->text, patient.name->text);
}
Can someone help me figure this out?
Thank you.
On strings and memory allocation:
A string in C is just a sequence of chars, so you can use char * or a char array wherever you want to use a string data type:
typedef struct {
int number;
char *name;
char *address;
char *birthdate;
char gender;
} patient;
Then you need to allocate memory for the structure itself, and for each of the strings:
patient *createPatient(int number, char *name,
char *addr, char *bd, char sex) {
// Allocate memory for the pointers themselves and other elements
// in the struct.
patient *p = malloc(sizeof(struct patient));
p->number = number; // Scalars (int, char, etc) can simply be copied
// Must allocate memory for contents of pointers. Here, strdup()
// creates a new copy of name. Another option:
// p->name = malloc(strlen(name)+1);
// strcpy(p->name, name);
p->name = strdup(name);
p->address = strdup(addr);
p->birthdate = strdup(bd);
p->gender = sex;
return p;
}
If you'll only need a few patients, you can avoid the memory management at the expense of allocating more memory than you really need:
typedef struct {
int number;
char name[50]; // Declaring an array will allocate the specified
char address[200]; // amount of memory when the struct is created,
char birthdate[50]; // but pre-determines the max length and may
char gender; // allocate more than you need.
} patient;
On linked lists:
In general, the purpose of a linked list is to prove quick access to an ordered collection of elements. If your llist contains an element called num (which presumably contains the patient number), you need an additional data structure to hold the actual patients themselves, and you'll need to look up the patient number every time.
Instead, if you declare
typedef struct llist
{
patient *p;
struct llist *next;
} list;
then each element contains a direct pointer to a patient structure, and you can access the data like this:
patient *getPatient(list *patients, int num) {
list *l = patients;
while (l != NULL) {
if (l->p->num == num) {
return l->p;
}
l = l->next;
}
return NULL;
}
I think this solution uses less code and is easy to understand even for newbie.
For string field in struct, you can use pointer and reassigning the string to that pointer will be straightforward and simpler.
Define definition of struct:
typedef struct {
int number;
char *name;
char *address;
char *birthdate;
char gender;
} Patient;
Initialize variable with type of that struct:
Patient patient;
patient.number = 12345;
patient.address = "123/123 some road Rd.";
patient.birthdate = "2020/12/12";
patient.gender = 'M';
It is that simple. Hope this answer helps many developers.
While Richard's is what you want if you do want to go with a typedef, I'd suggest that it's probably not a particularly good idea in this instance, as you lose sight of it being a pointer, while not gaining anything.
If you were treating it a a counted string, or something with additional functionality, that might be different, but I'd really recommend that in this instance, you just get familiar with the 'standard' C string implementation being a 'char *'...
You could just use an even simpler typedef:
typedef char *string;
Then, your malloc would look like a usual malloc:
string s = malloc(maxStringLength);
This does not work:
string s = (string)malloc(sizeof string);
string refers to a pointer, you need the size of the structure itself:
string s = malloc(sizeof (*string));
Note the lack of cast as well (conversion from void* (malloc's return type) is implicitly performed).
Also, in your main, you have a globally delcared patient, but that is uninitialized. Try:
patient.number = 3;
patient.name = "John";
patient.address = "Baker street";
patient.birthdate = "4/15/2012";
patient.gender = 'M';
before you read-access any of its members
Also, strcpy is inherently unsafe as it does not have boundary checking (will copy until the first '\0' is encountered, writing past allocated memory if the source is too long). Use strncpy instead, where you can at least specify the maximum number of characters copied -- read the documentation to ensure you pass the correct value, it is easy to make an off-by-one error.

Resources