C Programming Tutorial
About Lesson

C Structure and Function

In this tutorial, we will learn to pass structure members as arguments to a function and to return struct from a function with the help of examples.


Passing structure to functions

  • A Structure variables can be passed to a function and returned in a similar way as normal arguments.
  • A structure can be passed to any function from main() function or from any sub function.
  • Structure definition will be available within the function only.
  • It won’t be available to other functions unless it is passed to those functions by value or by address (reference).

Ways to pass structure

There are three ways to pass structures to a function in C programming :

  1. Passing structure to a function by value
  2. Passing structure to a function by address
  3. Without passing structure – by declare structure as global variable

Example : Passing structure to a function by value

#include <stdio.h>
#include <string.h>
struct studentData {
  char name[20];
  int id;
  int age;
};

// function prototype
struct studentData getStudentData() {
  struct studentData student1;
  printf("Enter name : ");
  scanf ("%s", student1.name);
  printf("Enter Id : ");
  scanf("%d", &student1.id);
  printf("Enter age : ");
  scanf("%d", &student1.age);
  return student1;
}

int main() {
  struct studentData s1;
  s1 = getStudentData();
  printf("Student1 Data\n");
  printf("Name : %s\n", s1.name);
  printf("Id : %d\n", s1.id);
  printf("Age : %d\n", s1.age);
  return 0;
}

Output

Enter name : Tony
Enter Id : 89
Enter age : 12
Student1 Data
Name : Tony
Id : 89
Age : 12

Here, we have passing the values of variable student1 to main function.


Example : Passing structure to a function by address

#include <stdio.h>
#include <string.h>
struct studentData {
  char name[20];
  int id;
  int age;
};

// function prototype
void func(struct studentData *Student1) {
  printf("Name : %s\n", Student1->name);
  printf("ID : %d\n", Student1->id);
  printf("Age : %d\n", Student1->age);
}

int main() {
  struct studentData Student1;
  strcpy(Student1.name, "Rabbit");
  Student1.id = 23;
  Student1.age = 16;
  func(&Student1);
  return 0;
}

Output

Name : Rabbit
ID : 23
Age : 16

Here, we have pass the address of the variable Student1 to the function struct studentData *student1 and access these values using -> arrow operator.


Example : By declaring a structure variable as Global variable

#include <stdio.h>
#include <string.h>
struct studentData {
  char name[20];
  int id;
  int age;
};

  struct studentData Student1;
// function prototype
  void printData() {
  printf("Name : %s\n", Student1.name);
  printf("ID : %d\n", Student1.id);
  printf("Age : %d\n", Student1.age);
}

int main() {
  strcpy(Student1.name, "Steve");
  Student1.id = 11;
  Student1.age = 7;
  printData();
  return 0;
}

Output

Name : Steve
ID : 11
Age : 7

Here, we have declare Student1 variable as global variable.

Note : It is not required to pass the structure to function, when we declare the structure variable as global variable.

Exercise Files
No Attachment Found
No Attachment Found
error: Content is protected !!