Search This Blog

About Me

My photo
Hello, my name is Gurnoor Singh. I am the creator of NoorEDU, a blog for about education . I live in India. I study in BCA Trade.In my free time, I enjoy hiking, practicing photography, and exploring the city by bike.

Thursday, 10 September 2020

Passing structure to function in C

 

Passing structure to function in C:

It can be done in below 3 ways.

  1. Passing structure to a function by value
  2. Passing structure to a function by address(reference)
  3. No need to pass a structure – Declare structure variable as global

Example program – passing structure to function in C by value:

In this program, the whole structure is passed to another function by value. It means the whole structure is passed to another function with all members and their values. So, this structure can be accessed from called function. This concept is very useful while writing very big programs in C.

COMPILE & RUN

Output:

Id is: 1
Name is: Raju
Percentage is: 86.500000

Example program – Passing structure to function in C by address:

In this program, the whole structure is passed to another function by address. It means only the address of the structure is passed to another function. The whole structure is not passed to another function with all members and their values. So, this structure can be accessed from called function by its addres

COMPILE & RUN

Output:

Id is: 1
Name is: Raju
Percentage is: 86.500000

Example program to declare a structure variable as global in C:

Structure variables also can be declared as global variables as we declare other variables in C. So, When a structure variable is declared as global, then it is visible to all the functions in a program. In this scenario, we don’t need to pass the structure to any function separately.

COMPILE & RUN

Output:

Id is: 1
Name is: Raju
Percentage is: 86.500000

Continue on C – Structure using Pointer….

Continue on C – Structure within Structure….

Continue on C – Structure Memory Allocation….

Continue on C – Structure Padding….

0 comments:

Post a Comment

Bitwise Operator in C