Structures in C Programming with Example

    0 Votes


You have learned to store similar kind of data in C language using array. But in real life programming, sometimes you need a method to store dissimilar data. In that case, C provides you structures to deal with this.

Structures in C

Using structures, you can define your own data type which can store multiple dissimilar primitive data types. Actually this type of data types is called user defined data types. For example, you can represent any real life object properties like student –

  • roll number – integer
  • name – character array
  • class – integer
  • section – character
  • last year marks in percentile – float
  • school name – character array

Here you can see that there are different data types which represent a particular student. So, using structure you can do this very easily in C.

Structure Declarations

In C, to define a structure there is a keyword “struct” is used. The syntax of the statement is like below.

struct 
{
 member_name;
 member_name;
 member_name;
...
} <one or more structure variables>;

For example, the above mentioned student declaration would be like this

struct student
{
int roll_number;
char name[50];
int class;
char section;
float marks;
char school_name[50];
} student_1;

Here, student_1 is the structure variable. You can also declare structure variable like below:

struct student student_2;

There is another good way to declare structure and its variables, using typedef keyword. The typedef keyword, actually defines a new data type by which you can use a data type the same way like other primitive data types.

typedef struct student
{
int roll_number;
char name[50];
int class;
char section;
float marks;
char school_name[50];
} student;

Here, using typedef we are defining a new data type called student. Now we can use this data type just like any other data types we know like int float etc. for variable declarations.

student student_1,student_2;

You can also initialize the structure variables also to zero like this:

student student_1 = {0};

Member Variables

All the subfields i.e the primitive data type variables in structures are called member variables. We can access these member variables using dot(.) operator. We can set and retrieve the member variable of student structure as shown below.

student student_1 = {0};
student_1.roll_number = 20;
strcpy(student_1.name,”Denis Ritchie”);
student_1.class = 12;
student_1.section = ‘A’;
student_1.marks = 96.9;
strcpy(student_1.school_name,”a4academics.com”);

Also Read - Top C Interview Questions and Answers for Freshers & Advanced C Interview Questions and Answers

Pointers to Structures

Just like other primitive data types you know, you can also define pointers to the structure too. However, accessing member variables using pointer to structure would be different. In this case, instead of dot(.) operator you have to use arrow(->) operator which is also called reference operator. Here is the example given below:

student student_1 = {0} , *pstudent = NULL;
pstudent = &student_1;
pstudent->roll_number = 20;
strcpy(pstudent->name,” Denis Ritchie”);
pstudent->class = 12;
pstudent->section = ‘A’;
pstudent->marks = 96.9;
strcpy(student_1->school_name,”a4academics.com”);

printf(“Student’s Roll Number : %d\n”, pstudent->roll_number);
printf(“Student’s Name : %s\n”, pstudent->name);
printf(“Student ‘s Class : %d\n”, pstudent->class);
printf(“Student’s Section : %c\n”, pstudent->section);
printf(“Student’s Marks : %f\n”, pstudent->marks);
printf(“Student’s School Name : %s\n”, pstudent->school_name);

Output :

Student’s Roll Number 20
Student’s Name Denis Ritchie
Student ‘s Class 12
Student’s Section A
Student’s Marks 96.9
Student’s School Name a4academics.com

Another way to store structure is by allocating memory dynamically. The only thing you need to remember here is that you have to free that memory when you don’t need them. When you allocate memory using malloc, you have to typecast to the structure that you are going to use in a program. Rest of the stuff is same as above.

pstudent =(student*) malloc(sizeof(student));

/* Your Code*/
free(pstudent);

Also Read - Top Embedded C Interview Questions and Answers & C++ Interview Questions and Answers

Array of Structures

When we have to declare similar kind of user defined data types in C, we can use array of structures. As you know have already seen that, arrays are used to store similar kinds of data. In all aspects, the array of structures is same as the other arrays i.e. arrays of integers, float etc. Only you have to use dot(.) operator or reference operator(->) to access structure members.

For example, we can define an array to store 100 student details like below.

student array_of_student[100];

Now, to access the member variables, we have to use array index number and the dot operator. For example, to set and print the value of 2'nd student name, roll number would be like this:

strcpy(array_of_student[1].name,”Denis Ritchie”);
array_of_student[1].roll_number = 20;
printf(“Student’s Name : %s\n array_of_student[1].name);
printf(“Student’s Roll Number : %d\n”, array_of_student[1].roll_number);

Related Articles

C Overview and Basic Syntax
Data Types in C Language
C Qualifiers - Constant and Volatile type Qualifier
C Type Casting with examples - float to int / int to char / int to long
C Constants and Literals
C Operators - Arithmetic, Assignment, Relational and Logical
C Operators - Bitwise Operators and Conditional Operators
C Operator Precedence and Associativity Table with Examples
C Input and Output Statements
C Format Specifiers for printf and scanf
C Decision Making Control Structures - 'if', 'if - else', nested 'if', 'switch'
Loops in C - for, while and do...while Loops
C 'break', 'continue' and 'goto' Statements to Control Program Flow
C Preprocessor Directives and Macros
C Functions Tutorial with Examples
C Arrays - Dynamic, 2d and Multidimensional with Examples
C Strings - Internal Working and Functions with example
C Pointers basics explained with examples
Unions in c programming with example
C Storage Class Specifiers - auto, register, static and extern

Popular Videos

How to Overcome Shyness in Communication

How to Overcome Shyness in Communication using 4 Simple Steps.

Got a tip or Question?
Let us know