Write a C Program to understand how array of structures sent to function

By | 21.03.2017

C Program to understand how array of structures sent to function


Write a C Program to understand how array of structures sent to function. Here’s a Simple Program to understand array of structures in C Programming Language.


Array of Structures


Structure is collection of different data type. An object of structure represents a single record in memory, if we want more than one record of structure type, we have to create an array of structure or object.

As we know, an array is a collection of similar type, therefore an array can be of structure type. You can store “n” number of students record by declaring structure variable as ‘struct student record[n]“, where n can be 1000 or 5000 etc.

This program is used to store and access “name, roll no. and marks ”  for many students using array of structures members.


Syntax for declaring structure array : :


struct struct-name
{
datatype var1;
datatype var2;
– – – – – – – – – –
– – – – – – – – – –
datatype varN;
};

struct struct-name obj [ size ];


Accessing Structure Members


To access any member of a structure, we use the member access operator (.). The member access operator is coded as a period between the structure variable name and the structure member that we wish to access. You would use the keyword struct to define variables of structure type.


Below is the source code for C Program to understand how array of structures sent to function which is successfully compiled and run on Windows System to produce desired output as shown below :


SOURCE CODE : :


/* Program to understand how an array of structures is sent to a function */


#include<stdio.h>

struct student {
                char name[20];
                int rollno;
                int marks;
               };
void display(struct student);
void dec_marks(struct student stuarr[ ]);
int main( )
{
        int i;
        struct student stuarr[3] = {
                                        {"Mary", 12, 98},
                                        {"John", 11, 97},
                                        {"Tom", 12, 89}
                                    };
        dec_marks(stuarr);
        for(i=0; i<3; i++ )
                display(stuarr[i]);

                return 0;

}


void dec_marks(struct student stuarr[])
{
        int i;
        for(i=0; i<3; i++)
                stuarr[i].marks = stuarr[i].marks-10;
}

void display(struct student stu)
{
        printf("Name  - %s\t", stu.name);
        printf("Rollno  - %d\t", stu.rollno);
        printf("Marks  - %d\n", stu.marks);
}

OUTPUT : :


//OUTPUT ::


Name  - Jackman Rollno  - 12    Marks  - 88
Name  - John    Rollno  - 11    Marks  - 87
Name  - Ramsey  Rollno  - 12    Marks  - 79

If you found any error or any queries related to the above program or any questions or reviews , you wanna to ask from us ,you may Contact Us through our contact Page or you can also comment below in the comment section.We will try our best to reach upto you in the short interval.


Thanks for reading the post.

4 1 vote
Article Rating
Category: Arrays Programs C Programming Tags:

About Tunde A

My name is Tunde Ajetomobi, a Tech Enthusiast and Growth Hacker. I enjoy creating helpful content that solves problem across different topics. Codezclub is my way of helping young aspiring programmers and students to hone their skills and find solutions on fundamental programming languages.

Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments