Linux C Programming Tutorial Part 26 - Structures and Functions

In one of our previous command line tutorials, we touched upon the concept of Structures. Using easy to understand examples, we discussed basic stuff like what are structures and why are they required. Expanding upon that, in this tutorial, we will discuss how structures and functions can be used together.

Structures and Functions in C Programming Language

Before we begin, let's quickly refresh how a structure is declared. Here's an example:

struct emp_details {

int emp_code;
int emp_age;

};

So here, the struct keyword - which is mandatory if you're defining a structure in C - signifies the beginning of the declaration. It is followed by a tag, or you can say the name of the structure. Then, inside brackets, you have two integer variables, which are grouped together as part of this structure.

To use this structure, you first need to define its instance or object. You can do that in the following way:

emp_details obj;

And then the structure members can be accessed in the following way:

obj.emp_code
obj.emp_age

Now, coming to functions, a function can return structures as well as accept structures in form of arguments. Here's an example:

#include <stdio.h>

struct emp_details {

int emp_code;
int emp_age;

};

struct emp_details fill(int code, int age)
{
struct emp_details obj;
obj.emp_code = code;
obj.emp_age = age;

return obj;
}

int main()
{
int x,y;

printf("Enter employee code: ");
scanf("%d", &x);

printf("\n Enter employee age: ");
scanf("%d", &y);

struct emp_details new_obj;

new_obj = fill(x,y);

printf("\n The employee code and age you entered are: %d and %d", new_obj.emp_code, new_obj.emp_age);

return 0;
}

So here, in this example, we have a function 'fill' that accepts two integers, treats them as code and age, fills up a structure based on this information, and returns the structure by value to the caller of the function.

Now, as I mentioned earlier in a statement above, structures can also be passed as function arguments. Following is an example, where the function 'fill' accepts an 'emp_details' structure as an argument.

#include <stdio.h>

struct emp_details {

int emp_code;
int emp_age;

};

void fill(struct emp_details obj)
{
printf("\n The employee code and age you entered are: %d and %d", obj.emp_code, obj.emp_age);
}

int main()
{
int x,y;

printf("Enter employee code: ");
scanf("%d", &x);

printf("\n Enter employee age: ");
scanf("%d", &y);

struct emp_details new_obj;
new_obj.emp_code = x;
new_obj.emp_age = y;

fill(new_obj);

return 0;
}

And here's the output in my case:

Enter employee code: 36 
Enter employee age: 29

The employee code and age you entered are: 36 and 29

Moving on, like normal variables, arrays, and more, there can be pointers to structures as well. Here's an example:

struct emp_details *ptr;

As always, pointers come in handy in case the structure size is large, and you are sending it as an argument to a function. Ideally, here's how you can access structure variables through pointer objects:

(*ptr).emp_code
(*ptr).emp_age

But for the sake for simplicity, C allows you omit * and . and use '->' instead. Following is an example:

ptr->emp_code
ptr->emp_age 

Here's an example that makes use of structure pointer:

#include <stdio.h>

struct emp_details {

int emp_code;
int emp_age;

};

void fill(struct emp_details *obj)
{
printf("\n The employee code and age you entered are: %d and %d", obj->emp_code, obj->emp_age);
}

int main()
{
int x,y;

printf("Enter employee code: ");
scanf("%d", &x);

printf("\n Enter employee age: ");
scanf("%d", &y);

struct emp_details new_obj;
new_obj.emp_code = x;
new_obj.emp_age = y;

fill(&new_obj);

return 0;
}

While this is the same example we used previously, the changes - as we are using a structure pointer now - are highlighted in bold.

Conclusion

In this tutorial, we expanded upon our understanding of structures by discussing how functions and structures can be used together. You are encouraged to try out the examples we used here on your local system. In case of any doubt or query, leave a comment below.

Share this page:

0 Comment(s)