Structures in C Programming

A structure is a user-defined datatype that can store related information of different datatype together. Therefore, a structure is a collection of variables under a common name.

  • The major difference between a structure and an array is that, an array contains related information of the same datatype.

Declaration of a structure

</>
Copy
 struct structname {
 	datatype varname1;
 	datatype varname2;
 	. . .
 };
  • Each variable declared with in a structure is called a member of the structure.
  • Structure declaration does’t allocate any memory
  • Memory is allocated for the structure only when we declare a variable of the structure type.
  • When we declare variables of a structure type, separate memory is allocated for each variable.

Example

Following is structure that represents a blueprint for student.

</>
Copy
struct student {
	int rollno;
	char name[20];
	char course[20];
	float fee;
};

student structure contains variables like roll number, name, course and fee that belong to different datatypes.

To declare a variable for the above student structure, the code snippet looks like the following :

struct student stud1;

Initialization of structure

Initializing a structure means assigning some constants to the members of the structure.If the user doen’t explicitly initialize the structure,the C will do that automatically like for int and float members the values will be initialized with 0 and for char and string members the values will be initialized with ‘\0’ by default.

  • When all the members of a structure are not initialized ,it is called partial initialization.In this,some of the members are initialized by the user and remaining are assigned with default values.
  • Structure member variable is generally accessed using a ‘.’ (dot) operator.
</>
Copy
struct structname {
	datatype varname1;
	datatype varname2;
	. . .
} structvar={constant1,constant2, . .};

Example – C Structure

</>
Copy
struct student {
	int rollno;
	char name[20];
	char course[20];
	float fee;
} stud1={02,"john","CSE",4500};

stud1 is a variable of type student structure. stud1 is initialized with rollno=02, name="john", course = "CSE" and fee=4500.

Example – C Structure Declaration & Initialization

In the following C example program, a student structure is declared, a variable of type student structure is created, values are read from console and assigned to the members of the structure and finally those member values are printed to the console.

C Program

</>
Copy
#include<stdio.h>

int main() {

	struct student{
		int rollno;
		char name[20];
		float fee;
		char dob[30];
	}stud;

	printf("\n Enter roll number:");
	scanf("%d",&stud.rollno);

	printf("\n Enter name:");
	scanf("%s",stud.name);

	printf("\n Enter fee:");
	scanf("%f",&stud.fee);

	printf("\n Enter DOB:");
	scanf("%s",stud.dob);

	printf("\n *******DETAILS**********");
	printf("\n Rollno=%d",stud.rollno);
	printf("\n Name=%s",stud.name);
	printf("\n Fee=%f",stud.fee);
	printf("\n DOB=%s",stud.dob);

	return 0;
}

When the above program is compiles and run, following would be the output.

Output

Enter roll number:01
Enter name:peter
Enter fee:10000
Enter DOB:31-08-1990

*******DETAILS**********
Rollno=1
Name=peter
Fee=10000.000000
DOB=31-08-1990

Pointer to a structure

If the structure is quite large, it is more efficient to create a pointer to the structure and pass it for function calls as arguments, instead of the structure itself.

In the following example, we will use pointer to a structure.

C Program

</>
Copy
#include<stdio.h>

struct student{
	int rollno;
	char name[20];
	float fee;
	char dob[30];
}stud;

int main() {
	struct student *ptr;
	ptr=&stud;

	printf("\n Enter roll number:");
	scanf("%d",&ptr->rollno);

	printf("\n Enter name:");
	scanf("%s",ptr->name);

	printf("\n Enter fee:");
	scanf("%f",&ptr->fee);

	printf("\n Enter DOB:");
	scanf("%s",ptr->dob);

	printf("\n\n *******DETAILS**********");

	printf("\n Rollno=%d",ptr->rollno);
	printf("\n Name=%s",ptr->name);
	printf("\n Fee=%f",ptr->fee);
	printf("\n DOB=%s",ptr->dob);

	return 0;
}

Compile and run the above C program. Following would be the output.

Output

Enter roll number:02
Enter name:john
Enter fee:20000
Enter DOB:12-03-1990

*******DETAILS**********
Rollno=2
Name=john
Fee=20000.000000
DOB=12-03-1990

Example – C Program demonstrating passing of a structure pointer to a function as argument

The given program passes a pointer for a structure to a function .

  • In this,the address of the structure variable is passed as an actual argument to a function.
  • That argument must be a structure type pointer variable .

Note : Any changes made to a the members in the called function are directly reflected in the calling function. Passing structures directly to a function could use up a lot of stack space if they are large. However simply using pointers would use up the memory, which can be freed easily.

C Program

</>
Copy
#include<stdio.h>
#include<malloc.h>

typedef struct student{
	int rollno;
	char name[20];
	float fee;
	char dob[30];
};

void display(struct student *);

int main() {
	struct student *ptr;

	ptr=(struct student *)malloc(sizeof(struct student));

	printf("\n Enter roll number:");
	scanf("%d",&ptr->rollno);

	printf("\n Enter name:");
	scanf("%s",ptr->name);

	printf("\n Enter fee:");
	scanf("%f",&ptr->fee);

	printf("\n Enter DOB:");
	scanf("%s",ptr->dob);

	display(ptr);

	return 0;
}

void display(struct student *ptr) {
	printf("\n *******DETAILS**********");
	printf("\n Rollno=%d",ptr->rollno);
	printf("\n Name=%s",ptr->name);
	printf("\n Fee=%f",ptr->fee);
	printf("\n DOB=%s",ptr->dob);
}

Output

Enter roll number:05
Enter name:ram
Enter fee:45000
Enter DOB:12-10-1980

*******DETAILS**********
Rollno=5
Name=ram
Fee=45000.000000
DOB=12-10-1980

C Nested Structure

A structure can be placed within another structure i.e.,a structure may contain another structure as its member.A structure that contains another structure as its member is called a nested structure.

Following is an example C program to illustrate C Nested Structures.

C Program

</>
Copy
typedef struct NAME {
	char firstname[20];
	char lastname[20];
};

typedef  struct DATE {
	int dd;
	int mm;
	int yr;
};

typedef struct STUDENT {
	int roll;
	NAME name;
	DATE dob;
	float fee;
};

C Self Referential Structure

These are the structures that contains reference to data of its same type i.e in addition to other data,it contains a pointer to a data that is  of same type as that of the structure.

  • Self referential structure is the foundation of data structures such as linked lists,trees,graphs etc.

Example

</>
Copy
struct node {
    int value;
    struct node *next;
};

In the above example next is a pointer to a structure of type node. Hence, the structure node is a self-referential structure with next as the referencing pointer.

Example (doubly linkedlist)

</>
Copy
struct node {
    int value;
    struct node *next; //self referential
    struct node *prev;
};

prev and next are the pointers to the structure of type node. Here prev pointer contains the address of the previous node and next pointer contains the address of the next node.

Conclusion

In this C Tutorial, we have learnt about C Structures, their syntax with examples. Also we have learnt to use structures in conjunction with other C programming concepts and data structures like linked lists.