Skip to main content

Structures and Functions in C

Structures and Functions

Here are some different ways of using structures and functions together:

  • Structure as a function Argument: This allows the function to access and modify the data contained in the structure. The structure can be passed by value or by reference.

  • Returning a structure from a function: A function can return a structure to the calling code, which can then access the data contained in the structure.

  • Using pointers to structures in a function: Pointers to structures can be used to pass a reference to the structure to a function, which can then modify the data contained in the structure.

Structure as a function Argument

Steps:

  • Define the structure: Define the structure using the struct keyword and specify the members of the structure.
  • Declare the function: Declare the function that will use the structure as a parameter. The function should have a parameter of the structure type.
  • Pass the structure to the function: When calling the function, pass the structure as an argument. The structure can be passed by value or by reference, depending on the needs of the function.
  • Access the structure members: Within the function, access the members of the structure using the dot (.) operator. The dot operator is used to access a specific member of the structure.

Example

#include <stdio.h>

// define the structure
struct student
{
char name[50];
int id;
float gpa;
};

// declare the function
void printStudent(struct student s);

int main()
{
// define a structure variable
struct student john = {"John Doe", 1234, 3.5};

// call the function with the structure as an argument
printStudent(john);

return 0;
}

// define the function
void printStudent(struct student s)
{
printf("Name: %s\n", s.name);
printf("ID: %d\n", s.id);
printf("GPA: %f\n", s.gpa);
}
Output:

Explanation

  • A structure student is defined with three members: name, id, and gpa.
  • The function printStudent takes a student structure as a parameter, and prints out the values of the structure's members.
  • The main function creates a student structure named john, and passes it as an argument to the printStudent function.
  • The function then prints out the values of the structure's members.

Returning a structure from a function

A structure can be returned from a function in the same way as other data types, such as integers or floats.

  • Define the structure: Define the structure using the struct keyword and specify the members of the structure.
  • Declare the function: Declare the function that will return the structure. The function should have a return type of the structure type.
  • Create the structure: Create an instance of the structure inside the function.
  • Set the structure members: Set the members of the structure inside the function.
  • Return the structure: Return the instance of the structure using the return statement.

Example

#include <stdio.h>

// define the structure
struct rectangle
{
int length;
int width;
};

// declare the function
struct rectangle getRectangle(int l, int w);

int main()
{
// call the function to get a rectangle
struct rectangle r = getRectangle(5, 10);

// print the values of the rectangle
printf("Length: %d\n", r.length);
printf("Width: %d\n", r.width);

return 0;
}

// define the function
struct rectangle getRectangle(int l, int w)
{
// create an instance of the structure
struct rectangle r;

// set the members of the structure
r.length = l;
r.width = w;

// return the structure
return r;
}
Output:

Explanation

  • A structure rectangle is defined with two members: length and width.
  • The function getRectangle takes two integers as parameters, and returns a rectangle structure.
  • Inside the function, an instance of the structure is created, and the members are set using the passed parameters.
  • The instance of the structure is then returned using the return statement.
  • In the main function, the getRectangle function is called with two integers as arguments, and the returned structure is assigned to a variable r.
  • The values of the structure's members are then printed out using printf statements.

Using pointers to structures in a function

Using pointers to structures in a function can be very useful in certain situations, particularly when working with large structures or when you want to modify the contents of the structure.

Steps:

  • Define the structure: Define the structure using the struct keyword and specify the members of the structure.
  • Declare the function: Declare the function that will use the pointer to the structure. The function should take a pointer to the structure as an argument.
  • Create the structure: Create an instance of the structure and allocate memory for it using the malloc function.
  • Pass the pointer to the function: Pass the pointer to the structure to the function.
  • Use the pointer to access the structure members: Inside the function, use the arrow operator -> to access the members of the structure through the pointer.
  • Free the memory: When you're done using the structure, free the memory using the free function.

Example

#include <stdio.h>
#include <stdlib.h>

// define the structure
struct person
{
char name[50];
int age;
};

// declare the function
void printPerson(struct person *p);

int main()
{
// create a pointer to a person structure
struct person *ptr;

// allocate memory for the structure
ptr = (struct person *)malloc(sizeof(struct person));

// set the values of the structure members using the pointer
strcpy(ptr->name, "John");
ptr->age = 30;

// call the function with the pointer to the structure
printPerson(ptr);

// free the memory allocated for the structure
free(ptr);

return 0;
}

// define the function
void printPerson(struct person *p)
{
printf("Name: %s\n", p->name);
printf("Age: %d\n", p->age);
}
Output:

Explanation

  • A structure person is defined with two members: name and age.
  • The function printPerson takes a pointer to a person structure as a parameter.
  • Inside the main function, a pointer to a person structure is created and memory is allocated for the structure using the malloc function.
  • The values of the structure members are set using the pointer, and then the printPerson function is called with the pointer to the structure.
  • Inside the printPerson function, the members of the structure are accessed using the arrow operator ->.
  • Finally, the memory allocated for the structure is freed using the free function.