i see some structs defined like this:
see that last array? weapon[22]? what is that for. I don't understand it because it comes after the } but before the ;Code:struct weaponstruct { char wname[15]; int wstr, wprice, wbought; } weapon[22];
i see some structs defined like this:
see that last array? weapon[22]? what is that for. I don't understand it because it comes after the } but before the ;Code:struct weaponstruct { char wname[15]; int wstr, wprice, wbought; } weapon[22];
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.
B.t.w. you can also write it like this (there are even more ways):Code:strcpy(weapon[0].name, "weapon 1"); /* change name of first weapon in array */
Code:struct weaponstruct { char wname[15]; int wstr, wprice, wbought; }; struct weaponstruct weapon[22];
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 =((
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.
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.Code:struct weaponstruct { char wname[15]; int wstr, wprice, wbought; }singleWeapon, *weaponPointer, arrayOfWeapons[ 22 ];
i think i understand it.. almost a little, but still hvae a question..
the singleWeapon and the other identifiers, .. im wondering how those relate to the above struct declaration?then creates 1 weapon, a pointer to a weapon and an array of weapons.
(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?
Thats it you've got it!!! I never use it but I suppose it might be helpful
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.
I always wondered about that but never got a satisfactory answer from anywhere, thx very muchOriginally 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
};
wow - that explanation really helps, unregistered
you should make a book about c++ and let me buy it
thaknee
Even though I mostly use c++ I still have a c-style habit of always making structs like this:
This is the C way of producing C++ struct names. That wayCode:typedef struct my_struct { //whatever data types } my_struct;
would be valid in both languages.Code:my_struct ms; //and my_struct ms_array[255];