Q:

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

0

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.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

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

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

C Arrays Solved Programs – C Programming

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
<< Write a C Program to understand arrays within stru...