Functions in Structures in C

C language – Function in Structures

To pass a struct variable as an argument to a function in C, you need to include the name of the structure type in the function prototype and the function call. This page is all about Functions in Structures in C.

In the C programming language, a structure is a user-defined data type that groups together related data elements and allows them to be treated as a single entity.Structures are often used to represent real-world objects, such as a point in 2D space, a customer record in a database, or a complex number.

Function in structure in C

Functions in Structures in C

In C, you can define functions inside a structure as member functions. These functions have access to the member variables of the structure and can be used to manipulate them.

Here’s an example of a structure with member functions in C:

Run

#include <stdio.h>
struct fruit {
   char name[50];
   char color[50];
};

// function prototype
void display(struct fruit s);

int main() {
   struct fruit s1;

   printf("Enter name: ");

   // read string input from the user until \n is entered
   // \n is discarded
   scanf("%[^\n]%*c", s1.name);

   printf("Enter color: ");
   scanf("%[^\n]%*c", s1.color);

   display(s1); // passing struct as an argument

   return 0;
}

void display(struct fruit s) {
   printf("\nDisplaying information\n");
   printf("Name: %s", s.name);
   printf("\nColor: %s", s.color);
}

Output

Enter name: grapes
Enter color: green
Displaying information
Name: grapes
Color: green

There are several ways to pass structure members to functions in C programming. Here are two common approaches:

Pass the structure as an argument to the function:

Run
#include<stdio.h>  

struct Point {
    int x;
    int y;
};

void printPoint(struct Point p) {
    printf("(%d, %d)\n", p.x, p.y);
}

int main() {
    struct Point p = {1, 2};
    printPoint(p);
    return 0;
}

Output

(1, 2)

In this example, the printPoint function takes a struct Point argument, which allows it to access the x and y members of the structure.

Return struct from a function

To return a struct from a function in C, you can simply use the return statement and specify the struct as the return value. Here’s an example:

Run

#include <stdio.h> 

struct Point {
    int x;
    int y;
};

struct Point createPoint(int x, int y) {
    struct Point p = {x, y};
    return p;
}

int main() {
    struct Point p = createPoint(1, 2);
    printf("(%d, %d)\n", p.x, p.y);
    return 0;
}

Output

(1, 2)

In this example, the createPoint function takes two int arguments and returns a struct Point value that is initialized with those values. The returned struct is then assigned to a local variable p in the main function, which is then printed to the console.

Note – It’s important to note that when a struct is returned from a function, it is typically copied. This means that any changes made to the struct within the function will not be reflected in the original struct. If you want to modify a struct in a function and have those changes persist, you will need to pass a pointer to the struct to the function and modify the original struct through the pointer.

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription