This is the problem:
When i am trying to assign value to the name, I get an error.Code:struct person { int age; char name[10]; };
What is the problem here?Code:struct person Bob; Bob.age = 20; Bob.name = "Bob";
This is the problem:
When i am trying to assign value to the name, I get an error.Code:struct person { int age; char name[10]; };
What is the problem here?Code:struct person Bob; Bob.age = 20; Bob.name = "Bob";
You can't assign strings to arrays like that (other than at the time of initialization). You need to copy the characters over one at a time, or have a function that does it for you (strcpy).
Quzah.
Hope is the first step on the road to disappointment.
oh, yeah I forgot that, thank you!
Sorry for double post, but I have another question about a structure:
I have declared a structure
And then I have to use the structure in a function:Code:struct driver { int age; char name[20]; }
(Well, this was actually declared by my teacher, so I don't understand all the code.)Code:struct driver *readDrivers(char *file)
So, far I understand; we have a structure driver with the tag *readDrivers what is actually a function with a parameter *file.
And I have write the code, for reading from file. And it works, but when I try to assign the variables to members, i get an error.
Please correct me, and explain the code. Thank you!Code:fscanf(f,"%d",readDrivers.age); // this should read from a file "f", and assign the digit to readDrivers.age
1) The file pointer points to an array of characters that should tell you what file to read the data from. (If you don't understand this, I suggest you read the FAQ on pointers and character arrays).
2)Then, in that function you will have an array of driver structs in which you read in all the drivers from the file. Then you return a pointer to this array (which is what the struct driver* is for). Note however that you also need to keep track of how many elements are in that array so you would probably need another integer passed as a parameter to that function.
3) readDrivers is a FUNCTION not a structure. You can't READ into it, you can CALL it to execute something meaningful.
1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
3. Get rid of conio.h and other antiquated DOS crap headers.
4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.
thanks a lot!
I am trying to understand it. It is a little complicated at first.
but, I still don't see how do we then save the data.
Small example, if we have a .txt with two names and age.
Like so;
Then we must get an array of two structs. And in my logic; the first struct is saved in array[0] and second at array[1].Code:Bob 31 John 42
But how do we save them?
For a normal structure is just:
Code:struct driver firstone; firstone.name = "Bob";
You can use strcpy, or strncpy() which is probably better since it allows you to set a max amount of characters. Just remember to think about the terminating \0 for strings.
So then declare an array of structs of the size you want, or some larger static size, read the data from the file and store it at each of the array locations appropriately:
Code:struct driver my_drivers[10]; int i; for(i = 0; i < 10;i++) { //read one line of data from the file in two variables, name and age strcpy(my_drivers[i]->name,name); //assuming my_drivers[i].name has memory allocated my_drivers[i].age = age; }
1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
3. Get rid of conio.h and other antiquated DOS crap headers.
4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.
ok, this is very helpful.
Last edited by theMaze; 04-18-2011 at 06:36 AM.
I really don't understand your question at all. A struct is just another type - a complex type. It usually has more than one field of data, in your case age and name. So each one should be stored in the appropriate field of the appropriate struct object.
1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
3. Get rid of conio.h and other antiquated DOS crap headers.
4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.