Q:

Write a C Program to understand how structure variable returned from function

0

Write a C Program to understand how the structure variable is returned from a function. Here’s a Simple Program to check how structure variable returned from function in C Programming Language.

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

All Answers

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

C Structure :

  • C Structure is a collection of different data types which are grouped together and each element in a C structure is called member.
  • If you want to access structure members in C, structure variable should be declared.
  • Many structure variables can be declared for same structure and memory will be allocated for each separately.

 

  • It is a best practice to initialize a structure to null while declaring, if we don’t assign any values to structure members.

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.

 

Below is the source code for C Program to understand how the structure variable is returned from a function which is successfully compiled and run on Windows System to produce desired output as shown below :

SOURCE CODE : :

/* Program to understand how a structure variable is returned from a function */



#include<stdio.h>
struct student {
                char name[20];
                int rollno;
                int marks;
               };
void display(struct student);
struct student change(struct student stu);
int main( )
{
        struct student stu1 = {"John", 12 , 87 };
        struct student stu2 = {"Ramsey", 18, 90};
        stu1 = change(stu1);
        stu2 = change(stu2);
        display(stu1);
        display(stu2);
        
        return 0;
        
}
struct student change(struct student stu)
{
        stu.marks = stu.marks + 5;
        stu.rollno = stu.rollno - 10;
        return stu;
}

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   - John   Rollno - 2      Marks  - 92
Name   - Ramsey Rollno - 8      Marks  - 95

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

total answers (1)

C Basic 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 how structure memb...