Array elements of struct and struct members
The two pieces of code above are equivalent.
In the first one, you define struct Person
and define prsn
as an array of 20 elements of that struct at the same time. In the second, you first define the struct then separately define the array.
In C, array indexes start at 0, so in both cases the prsn
array contains elements indexed from 0 to 19. This does not affect the size of the name
member, which is a 50 element array. You have an array of 20 struct Person
, each of which contains a 50 element array of char
called name
.
Regarding making the array size unlimited, an array must have a size, either specified explicitly between [
and ]
or implicitly via an initializer list. The size can be a variable however such an array cannot be defined at file scope, and the size variable must have been assigned a value previously.
What does [20] do? What does it mean?
The comments below show common nomenclature for the parts of your struct:
struct Person { //struct name (Person)
char name[50]; // \
int citNo; // --struct members
float salary; // /
} prsn[20]; // instance of struct Person array
The [20]
indicates that this instance of struct Person
is an array of 20 separate collections of the 3 members. Each element of the array can be accessed using array notation. For example, in a loop:
int main(int argc, char *argv[])
{
for(int i=0;i<20;i++)// note i goes from 0 to 19
{
//.....
//assuming members have been populated
printf( "%d)\nName is: %d\ncitNo is: %d salary is: %f\n\n", prsn[i].name, prsn[i].citNo, prsn[i].salary);
}
return 0;
}
Does the [20] limit the name to 20 (from 50) or limit the
prsn
fromprsn[1]
toprsn[20]
?
The member name[50]
defines a 50 char array. Its size is not affected in any way by the [20]
index used to size the array of struct. i.e. as you have it defined, there are 20 instances of prsn
, each instance containing 3 members: char [50]
, int
and float
. And by your definition, the 20 instances created by the [20]
allows the array to be accessed with index values from 0
through 19
. (See loop illustration above.)
EDIT to address OP question in comments:
And what i have to do if i want to make the elements unlimited ?
If you want to use the empty array brackets, ( []
) the definition must include a struct initializer list. For example:
... } prsn[] = {{"Bill", 1, 23000.00}, {"John", 2, 45000.00}, ...};
If the size of the struct array is not known at compile time, and needs to be sized according to information available only at run time, then either dynamic memory allocation or a VLA can be used. First, for dynamic memory, instead of defining with array notation, create a pointer instance:
... } *prsn;
Then, in a function, use calloc
or malloc
to create memory for say 1000 instances:
int someFunction(void)
{
prsn = calloc(1000, sizeof(struct Person));
if(prsn)
{
// Use instances of prsn
// free when finished
free(prsn);
}
For VLA the instances created must have local scope. So, inside a function somewhere, do this:
int someFunction(int sizeOfStruct)
{
struct Person newPerson[sizeOfStruct] = {0};
Note this method does not require freeing memory associated with newPerson