Thread: Problem with structure

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    10

    Problem with structure

    This is the problem:

    Code:
    struct person
    {
         int age;
         char name[10];
         
    };
    When i am trying to assign value to the name, I get an error.

    Code:
    struct person Bob;
    
    Bob.age = 20;
    Bob.name = "Bob";
    What is the problem here?

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    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.

  3. #3
    Registered User
    Join Date
    Mar 2011
    Posts
    10
    oh, yeah I forgot that, thank you!

  4. #4
    Registered User
    Join Date
    Mar 2011
    Posts
    10
    Sorry for double post, but I have another question about a structure:


    I have declared a structure
    Code:
    struct driver
    {
         int age;
         char name[20];
    }
    And then I have to use the structure in a function:
    Code:
    struct driver *readDrivers(char *file)
    (Well, this was actually declared by my teacher, so I don't understand all the code.)


    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.

    Code:
    fscanf(f,"%d",readDrivers.age);
    
    // this should read from a file "f", and assign the digit to readDrivers.age
    Please correct me, and explain the code. Thank you!

  5. #5
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    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.

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by claudiu View Post
    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.
    If this is not a dynamically allocated array, then it has to be static, otherwise you'll lose the array when the function ends.

    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    Registered User
    Join Date
    Mar 2011
    Posts
    10
    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;
    Code:
    Bob 31
    John 42
    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].


    But how do we save them?
    For a normal structure is just:
    Code:
    struct driver firstone;
    firstone.name = "Bob";

  8. #8
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    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.

  9. #9
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    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.

  10. #10
    Registered User
    Join Date
    Mar 2011
    Posts
    10
    ok, this is very helpful.
    Last edited by theMaze; 04-18-2011 at 06:36 AM.

  11. #11
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 04-25-2010, 10:57 AM
  2. Structure problem
    By lolguy in forum C Programming
    Replies: 4
    Last Post: 12-18-2008, 10:32 PM
  3. Replies: 9
    Last Post: 05-21-2007, 12:10 AM
  4. Replies: 4
    Last Post: 11-22-2006, 12:20 PM
  5. C structure within structure problem, need help
    By Unregistered in forum C Programming
    Replies: 5
    Last Post: 11-30-2001, 05:48 PM