Structure in C | How to Use Structure

structure in c

In this post, we are going to discuss a special data type which is called Structure in C and its applications. We will discuss every bit of it.

We are already aware of ‘Array’ data structures where we can store similar types of data elements.

In our daily life, we have to deal with situations where we need to store different types of data under one roof.

Let’s take a very common example,

Suppose you want to store aadhar details of people in your computer. Aadhar card details comprises Aadhar card number, name of the person, Father’s name, DOB etc.

Now how will you store these data in your computer, since they are of different types. You can use array data structures for storing only Aadhar card number, then another array for storing names and all.

But this method will not be effective when there are redundancies and things will mess up and you will not want it since Aadhar details are important.

Here Structures come to play.

Technical Definition

Structure in C/C++ is a collection of variables of different data types under a single name. It is similar to a class in C++. Both hold a collection of data of different data types.

How structure variables in C/C++ are declared ..?

struct aadhar_details {
       int aadhar_no;
       char name[20];
       int pincode;
};

This is how the structure variable is declared. We give a name to a structure variable, here we named it as “aadhar_details” and we define other data types inside it which are required.

How to declare structure variables .. ?

Structure variables are declared in the same as we declare primitive data types like int, char, etc.

Here ‘p1’ is a variable of structure type which stores the aadhar_details of Person p1.

NOTE the size of a structure variable is the total size of the elements present in it.

For p1, size is 4 (aadhar_no) + 20 (name) + 4 (pincode).
Total Size of the variable p1 is 28.

How to access members of a structure variable .. ?

Since the structure variable comprises other primitive data types, accessing its member is a bit different from others.

Let’s see an example code,

#include<stdio.h>

struct aadhar_details {
	int aadhar_no;
	char name[20];
	int pincode;
};

int main()
{
	struct aadhar_details p1;
	
        p1.aadhar_no = 12345;
	p1.name = “George”;
	p1.pincode = 100006;

        struct aadhar_details p2;
        scanf("%d %s %d", &p2.aadhar_no, p2,name, &p2.pincode);

        return 0;
}

Structure variables in C/C++ are accessed by a dot(.) operator.

In the above code, we have seen two examples. In the first example, we are initializing structure variable p1 and in the second example, we are taking input from the user for the details of variable p2.

These are the ways to access the structure variable.

NOTE that we cannot initialize structure data while the declaration of a structure.

struct P {
     int x = 0;
     string str = "xyz";
};

This will generate an error. So instead of initializing it while declaration, we should create a variable and we should initialize it there.

Memory Allocation of structure variable

As we have discussed total size of a structure variable is the sum of the sizes of datatype present as elements of the structure.

Let take the same example of structure “aadhar_details” . Memory allocation will be,

Structure and Array

In computer science, there are two terms related to structure and array. One is the Structure of arrays and the other is an Array of structures.

Structure of arrays

As the name says, In structure of arrays we have to declare a structure with array as element. Let’s see an example,

struct Point {
       int x[10];
       int y[10];
};

The above structure comprises two arrays, one for storing x-coordinates and other for storing y-coordinates.

Declaring and accessing Structure of arrays

Structure of arrays are accessed through structure variable in the same way we access normal structure variable.

#include<stdio.h>

struct Point {
       int x[10];
       int y[10];
};

int main()
{
	struct Point p;
        for(int i = 0; i < 10; i++)
        {
             scanf("%d%d", &p.x[i], &p.y[i]);
        }
        return 0;
}

Array of structures

In array of structures, we create an array of a structure type. Let’s see an example. We will take the same example,

Yes, we have already discussed declaration of structure variable. The same way we can even declare an array of structure type.

Let’s see its implementation,

#include<stdio.h>

struct aadhar_details 
{
        int aadhar_no;
	char name[20];
	int pincode;
};

int main()
{
	struct aadhar_details p1[10];
	
        for(int i = 0; i < 10; i++)
        {
               scanf("%d %s %d", p1[i].aadhar_no, p1[i].name, p1[i].pincode);

        return 0;
}

Hope this part is clear, let’s move ahead.

Structure and Functions

We have already discussed about structure and array, now let’s discuss Structure and Function.

Passing a structure through a function

Passing a structure variable through a function is same as passing a primitive datatype variable through a function.

This is how a Function prototype is declared with structure variable as a parameter.

Structure as a return type of a function

We can even return structure variable from a function, it only requires “struct” as a return type.

#include <stdio.h>
using namespace std;

struct Person {
    int age;
    char name[50];
    float salary;
};

struct Person getData(struct Person);  

int main()
{

    struct Person p;
    p = getData(p);   
  
    printf("Name : %s\nAge: %d\nSalary : %f",p.name, p.age, p.salary);
    return 0;
}

struct Person getData(struct Person p) {
    scanf("%s", p.name);
    scanf("%d", p.age);
    scanf("%f", p.salary);

    return p;
}

As we can see in the above program, we returned a structure variable.

NOTE that return type should be specified clearly with struct keyword as well structure name.

Structure and Pointers

We can even create a pointer variable of data type Structure like the same we create pointer variable of other primitive data types.

Since Pointer is used to store address of a variable of the appropriate datatype, it can also store the address of structure datatype.

NOTE the size of pointer variable of type structure is same as pointer variable of any primitive type. So don’t be confuse with its size.

Pointer variable stores address so its size is 4 bytes irrespective of its type.

Pointer variable inside a structure in C/C++

We can even define a pointer variable while defining a structure. Let’s see an example,

struct Node 
{
      int data;
      struct Node *next;
}

This is the famous structure of a Linklist where we define a structure variable which stores a integer value and a pointer variable of type Node for storing the address of next Node.

Don’t worry we will cover this more clearly in Linklist part but as of now understand that we can even define a pointer variable in a structure.

Structure is used in Data Structures like Linklist, Tree, Graph etc. It has many uses.

Hope Structure is clear to you.
That’s all Folks..!!!

Tagged :

Leave a Reply

Your email address will not be published. Required fields are marked *