Write a C Program to understand how pointer to structure variable is sent to function

By | 21.03.2017

C Program to understand how pointer to structure variable is sent to function


Write a C Program to show an example how pointer to structure is sent to function. Here’s a Simple Program to check how pointer to structure variable is sent to function in C Programming Language.

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


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 pointer to structure variable is 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 a pointer to structure variable is sent to a function */


#include<stdio.h>
struct student {
                char name[20];
                int rollno;
                int marks;
               };
void display(struct student *);
void inc_marks(struct student *);
int main( )
{
        struct student stu1 = {"John", 12, 87};
        struct student stu2 = {"Snow", 18, 90};
        inc_marks(&stu1);
        inc_marks(&stu2);
        display(&stu1);
        display(&stu2);

        return 0;

}
void inc_marks(struct student *stuptr)
{
        (stuptr->marks)++;
}
void display(struct student *stuptr)
{
        printf("Name   - %s\t", stuptr->name);
        printf("Rollno - %d\t", stuptr->rollno);
        printf("Marks  - %d\n", stuptr->marks);
}

OUTPUT : :


//OUTPUT ::


Name   - John   Rollno - 12     Marks  - 88
Name   - Snow   Rollno - 18     Marks  - 91

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.

0 0 votes
Article Rating
Category: C Programming Pointer Programs 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