Thread: struct array trouble

  1. #1
    Registered User
    Join Date
    Jan 2006
    Posts
    31

    struct array trouble

    Code:
    #include <stdio.h>
    #include <string.h>
    
    void main()
    {
     struct oz
     {
      char actor[30];
      char character[30];
      int age;
     }
    
     struct oz actor[4];
     int x;
    
     strcpy(actor[0].actor,"Judy Garland");
     strcpy(actor[0].character,"Dorothy");
     actor[0].age=17;
    
     strcpy(actor[1].actor,"Ray Bolger");
     strcpy(actor[1].character,"Scarecrow");
     actor[1].age=35;
    
     strcpy(actor[2].actor,"Bert Lahr");
     strcpy(actor[2].character,"Cowardly Lion");
     actor[2].age = 44;
    
     strcpy(actor[3].actor,"Jack Haley");
     strcpy(actor[3].character,"Tin Woodsman");
     actor[3].age = 40;
    
     puts("Wizard of Oz Database!");
    
     for(x=0;x<4;x++)
     {
      printf("%s played %s, and was %i years old.\n",actor[x].actor, actor[x].character, actor[x].age);
     }
    }
    whenever i compile this, it says ''two or more data types in declaration of 'actor''
    what does this mean? id appreciate if anyone can help.

  2. #2
    Registered User
    Join Date
    Nov 2001
    Posts
    255
    could be fussing that your struct has an actor variable and the you declare an oz variable named actor?
    hooch

  3. #3
    Registered User
    Join Date
    Jan 2006
    Posts
    31
    is oz a type of variable? what can i do to fix this?

  4. #4
    Registered User
    Join Date
    Nov 2001
    Posts
    255
    well structs declared outside of main are global so nothing can share its name you can either rename the actor inside of main or rename the actor inside of oz that should fix it.

    and for future reference try not to reuse names so closely together it confuses most people
    hooch

  5. #5
    Registered User
    Join Date
    Mar 2005
    Posts
    135
    Quote Originally Posted by chasingxsuns
    Code:
    #include <stdio.h>
    #include <string.h>
    
    void main()
    {
     struct oz
     {
      char actor[30];
      char character[30];
      int age;
     }; <------------------ you forgot to terminate your struct definition with a semicolon
    
     struct oz actor[4];
     int x;
    
     strcpy(actor[0].actor,"Judy Garland");
     strcpy(actor[0].character,"Dorothy");
     actor[0].age=17;
    
     strcpy(actor[1].actor,"Ray Bolger");
     strcpy(actor[1].character,"Scarecrow");
     actor[1].age=35;
    
     strcpy(actor[2].actor,"Bert Lahr");
     strcpy(actor[2].character,"Cowardly Lion");
     actor[2].age = 44;
    
     strcpy(actor[3].actor,"Jack Haley");
     strcpy(actor[3].character,"Tin Woodsman");
     actor[3].age = 40;
    
     puts("Wizard of Oz Database!");
    
     for(x=0;x<4;x++)
     {
      printf("%s played %s, and was %i years old.\n",actor[x].actor, actor[x].character, actor[x].age);
     }
    }
    whenever i compile this, it says ''two or more data types in declaration of 'actor''
    what does this mean? id appreciate if anyone can help.

    I pointed out your mistake above; find it. The warning you get shouldn't really matter even though it's not good design to use the same name for a struct-variable and a struct-member. You're qualifying member actor with the struct-variable actor prefix which is what tells the two apart in usage.


    xeddiex.

  6. #6
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Are you guys serious?

    Code:
     struct oz
     {
      char actor[30];
      char character[30];
      int age;
     };    // TRAILING SEMICOLON IS REQUIRED
    ...and main's return type should be int, not void.
    Sent from my iPadŽ

  7. #7
    Registered User
    Join Date
    Jan 2006
    Posts
    31
    i tried changing the actor inside main to both 'person' and 'aaa' and changing the actor inside oz to 'uuu' and it did the same thing. know anything else that could be wrong?

  8. #8
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Two people just told you what's wrong.
    Sent from my iPadŽ

  9. #9
    Registered User
    Join Date
    Feb 2006
    Posts
    1

    Replay of your program

    Dear friend,
    You can use.But your codeing is have syntax error.Structre is must ended with };
    in your coding the strcture doesn't ened.
    corrected coding is:
    Code:
    /*Codeing
    Name:SABARINATHAN.S
    PH:09841414918
    DATE:27-02-2006*/
    #include <stdio.h>
    #include <string.h>
    
     struct oz {
      char actor[30];
      char character[30];
      int age;
     };
    main()
    {
    
     struct oz actor[4];
     int x;
    
     strcpy(actor[0].actor,"Judy Garland");
     strcpy(actor[0].character,"Dorothy");
     actor[0].age=17;
    
     strcpy(actor[1].actor,"Ray Bolger");
     strcpy(actor[1].character,"Scarecrow");
     actor[1].age=35;
    
     strcpy(actor[2].actor,"Bert Lahr");
     strcpy(actor[2].character,"Cowardly Lion");
     actor[2].age = 44;
    
     strcpy(actor[3].actor,"Jack Haley");
     strcpy(actor[3].character,"Tin Woodsman");
     actor[3].age = 40;
    
     puts("Wizard of Oz Database!");
    
     for(x=0;x<4;x++)
     {
      printf("%s played %s, and was %i years old.\n",actor[x].actor, actor[x].character, actor[x].age);
     }
    }
    Out Put is:
    Wizard of Oz Database!
    Judy Garland played Dorothy, and was 17 years old.
    Ray Bolger played Scarecrow, and was 35 years old.
    Bert Lahr played Cowardly Lion, and was 44 years old.
    Jack Haley played Tin Woodsman, and was 40 years old.

  10. #10
    Registered User
    Join Date
    Jan 2006
    Posts
    31
    oh im sorry i didnt see xeddiex's post. lol sorry for the extra trouble guys.

  11. #11
    Registered User
    Join Date
    Nov 2001
    Posts
    255
    oh ya evil semi colons lol.
    hooch

  12. #12
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    main() is int main(), as mentioned, not main() and definitely not void main(). And you should also return something from main(), probably 0.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  2. Looking for constructive criticism
    By wd_kendrick in forum C Programming
    Replies: 16
    Last Post: 05-28-2008, 09:42 AM
  3. Concatenating in linked list
    By drater in forum C Programming
    Replies: 12
    Last Post: 05-02-2008, 11:10 PM
  4. How to access struct fields in array
    By dv007 in forum C++ Programming
    Replies: 3
    Last Post: 01-18-2006, 09:51 AM
  5. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM