Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Array elements of struct and struct members

Tags:

c

struct

I want to ask about a struct declaration in C. For example,

struct Person
{
    char name[50];
    int citNo;
    float salary;
} prsn[20];

What does [20] do? What does it mean? Does it limit the name to 20 (from 50) or limit the prsn from prsn[1] to prsn[20]?

And if I write the code like this:

struct Person
{
    char name[50];
    int citNo;
    float salary;
};
struct Person prsn[20];

Does it do the same thing?

like image 678
wiryadev Avatar asked Dec 10 '19 13:12

wiryadev


People also ask

Can an array be a member of a struct?

struct student { char name[20]; int roll_no; float marks; }; The student structure defined above has a member name which is an array of 20 characters. Let's create another structure called student to store name, roll no and marks of 5 subjects.

How do you access an array member of a struct?

1. Array elements are accessed using the Subscript variable, Similarly Structure members are accessed using dot [.] operator.

What are struct variables and struct members?

Structures (also called structs) are a way to group several related variables into one place. Each variable in the structure is known as a member of the structure. Unlike an array, a structure can contain many different data types (int, float, char, etc.).


3 Answers

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.

like image 178
dbush Avatar answered Oct 27 '22 17:10

dbush


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 from prsn[1] to prsn[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

like image 34
ryyker Avatar answered Oct 27 '22 16:10

ryyker


You are declaring an Array of 20 struct of that type

like image 1
Math Lover Avatar answered Oct 27 '22 15:10

Math Lover