Quick struct/class question

This is a discussion on Quick struct/class question within the C++ Programming forums, part of the General Programming Boards category; i see some structs defined like this: Code: struct weaponstruct { char wname[15]; int wstr, wprice, wbought; } weapon[22]; see ...

  1. #1
    Registered User GrNxxDaY's Avatar
    Join Date
    Jul 2002
    Posts
    140

    Quick struct/class question

    i see some structs defined like this:

    Code:
    struct weaponstruct
    {
       char wname[15];
       int wstr, wprice, wbought;
    } weapon[22];
    see that last array? weapon[22]? what is that for. I don't understand it because it comes after the } but before the ;

  2. #2
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680
    The 22 means you have created an array of structures (with size 22). You can store 22 weapons in this array. The first element in an array always starts with 0.
    Code:
    strcpy(weapon[0].name, "weapon 1"); /* change name of first weapon in array */
    B.t.w. you can also write it like this (there are even more ways):
    Code:
    struct weaponstruct
    {
       char wname[15];
       int wstr, wprice, wbought;
    };
    
    struct weaponstruct weapon[22];

  3. #3
    Registered User GrNxxDaY's Avatar
    Join Date
    Jul 2002
    Posts
    140
    i'm not sure i understand.

    is weapon declared somewhere else in the code?? *cry*
    could you give me an example of how this weapon array would work? and how it is related to the weaponstruct structure?

    thankee =((


  4. #4
    Registered User
    Join Date
    Jul 2002
    Posts
    273
    Just like Monster said, it's a way of creating an instance of the struct right there. I never saw the reason for it because it looks confusing and doesn't gain you anything but that is what it is.

  5. #5
    Seeking motivation... endo's Avatar
    Join Date
    May 2002
    Posts
    537
    Code:
    struct weaponstruct
    {
       char wname[15];
       int wstr, wprice, wbought;
    }singleWeapon, *weaponPointer, arrayOfWeapons[ 22 ];
    This confused me for ages, but its simple really. The identifiers before the ; are declaring different objects of the struct type. The example above first defines a weaponstruct then creates 1 weapon, a pointer to a weapon and an array of weapons.

  6. #6
    Registered User GrNxxDaY's Avatar
    Join Date
    Jul 2002
    Posts
    140
    i think i understand it.. almost a little, but still hvae a question..

    then creates 1 weapon, a pointer to a weapon and an array of weapons.
    the singleWeapon and the other identifiers, .. im wondering how those relate to the above struct declaration?

    (i think i get this part)dose hte arrayOfWeapons[22] mean that it creates 22 instances of weaponstruct type ? so you can access one of those instances by doing arrayOfStructure[3]
    err.. well.. that wouldn't make sense now that i think about it?? pweeze help?

  7. #7
    Seeking motivation... endo's Avatar
    Join Date
    May 2002
    Posts
    537
    Thats it you've got it!!! I never use it but I suppose it might be helpful

  8. #8
    Unregistered
    Guest
    The use of the keyword struct is used in both C and C. The underlying theory is the same in both languages: structs can be used to create a user defined type that holds member(s) that are somehow related to one another. The implementation of this theory is a little different between the two languages however.

    in C++ you use the keyword struct just like you do the keyword class. Here's an example:

    struct nameOfStruct
    {
    //code goes here
    };

    In C++ you never use the keyword struct in the program again however. For example, in main() if you want to declare an instance of type nameOfStruct or an array of type nameOfStruct, you go like this:

    nameOfStruct struct1;
    nameOfStruct arrayOfStruct[22];

    You don't use the keyword struct to precede the declaration of the instance of the struct or array of struct, etc. Same with using structs as function parameters in function declarations, etc.

    void useStruct(nameOfStruct);
    void display(nameOfStruct[]);

    Not so in C. In C you can declare the struct type just like above:

    struct Cstruct
    {
    //code goes here
    };

    but now in main(), or wherever, you have to use the keyword struct in declaring instances of the struct. Like this:

    struct Cstruct struct1;
    struct arrayOfCstruct CstructArray[22];

    and when used as function parameters, etc.

    void useCstruct(struct Cstruct);
    void display(struct arrayOfCstruct[22]);

    To avoid having to use the keyword struct over and over again you can typedef the struct like this:

    typedef struct Cstructs
    {
    //code goes here
    };

    To avoid having to use the keyword struct and the name of the type when declaring instances of the structs you can also do this:

    struct Cstruct
    {
    //code goes here
    }example1, example2, *ptr, arrayEx[22];

    now example1 and example2 are instances of the C struct type called Cstruct, ptr is a pointer to a Cstruct, and arrayEx is an array of 22 Cstructs.

    In a carry over from C to C++ you can use the latter technique when declaring instances of structs or classes in C++, too. That way you don't have to use the type name when declaring the instances of the structs.

    As you can see, C++ structs are much more programmer friendly and can do anything C style structs can do. Unfortunately, while C style structs can be used in C++, you can't use C++ style structs in C.

  9. #9
    Seeking motivation... endo's Avatar
    Join Date
    May 2002
    Posts
    537
    Originally posted by Unregistered
    ...but now in main(), or wherever, you have to use the keyword struct in declaring instances of the struct. Like this:

    struct Cstruct struct1;
    struct arrayOfCstruct CstructArray[22];

    and when used as function parameters, etc.

    void useCstruct(struct Cstruct);
    void display(struct arrayOfCstruct[22]);

    To avoid having to use the keyword struct over and over again you can typedef the struct like this:

    typedef struct Cstructs
    {
    //code goes here
    };

    I always wondered about that but never got a satisfactory answer from anywhere, thx very much

  10. #10
    Registered User GrNxxDaY's Avatar
    Join Date
    Jul 2002
    Posts
    140
    wow - that explanation really helps, unregistered

    you should make a book about c++ and let me buy it

    thaknee

  11. #11
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Even though I mostly use c++ I still have a c-style habit of always making structs like this:
    Code:
    typedef struct my_struct {
        //whatever data types
    } my_struct;
    This is the C way of producing C++ struct names. That way
    Code:
    my_struct ms;
    //and
    my_struct ms_array[255];
    would be valid in both languages.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Very quick math question
    By jverkoey in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 10-26-2005, 11:05 PM
  2. very quick question.
    By Unregistered in forum C++ Programming
    Replies: 7
    Last Post: 07-24-2002, 03:48 AM
  3. quick question
    By Unregistered in forum C++ Programming
    Replies: 5
    Last Post: 07-22-2002, 04:44 AM
  4. Quick Question Regarding Pointers
    By charash in forum C++ Programming
    Replies: 4
    Last Post: 05-04-2002, 11:04 AM
  5. Quick question: exit();
    By Cheeze-It in forum C Programming
    Replies: 6
    Last Post: 08-15-2001, 05:46 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21