C Programming - SPLessons
SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

Structure in C

Structure in C

shape Introduction

In C-programming, a variable consists of single data and single data type. An array consists of multiple data and single data type. Continuing in advance, Structure in C consists of multiple data and multiple data types, sometimes called records in other languages. Structure in C can be defined as group of variables with same name and different data types which can be created, pointed to, copied, and refer to conveniently and easily. Unlike in C++, the names of structures are declared in a separate name space in C. .

Defining a structure:

shape Description

Defining a structure can be done two ways.

shape Syntax

struct structure_name { //statements; };

shape Example

[c] #include<stdio.h> #include<conio.h> struct student { char name[20]; int age; float percentage; }file; int main() { strcpy(file.name, "John"); file.age=25; file.percentage=78-9; printf("Name is %s\n",file.name); printf("age is %d\n",file.age); printf("percentage is %f\n", file.percentage); }[/c] Output: [c] Name is John age is 25 percentage is 69.000000 [/c]

shape More Info

Every member of structure is called as Structure Member or an Element.In the above example, John, age,percentage are called as tags which are of different data types.

Retrieving a structure

shape Description

The structures can be accessed using the dot . operator. Structure can also be accessed by using ->. As using arrow is bit complex, dot operator is used.

Declaration and Initialization of structure: 

shape Description

Any structure is initialized in order to access it and allocate a memory to it. Declaring will be same as defining.Coming to defining a structure, assign values to variables at the end of structure block( after the braces) by giving a semicolon.

shape Example

[c] struct student { char name[20]; int age; float percentage; } student1={"John",15,77.5}; student2{"Mike",16,78};[/c]

shape More Info

When there are no values to declare, structure should be declared as null.Otherwise, there will be no memory allocation for the variable.

Struct as a header file

shape Description

There is also another way for defining a structure by using "header files".
  • Create the file with .h extension and write the structure code as follows.Now write the structure code in it and save.
  • Open other file with extension .c and include the header section created before and use it in the program. Eg: #include "structure.h".

shape Example

[c] #include<stdio.h> #include<conio.h> #include"structure.h" int main() { strcpy(file.name, "splessons"); file.age=25; file.percentage=78-9; printf("Name is %s\n",file.name); printf("age is %d\n",file.age); printf("percentage is %f\n", file.percentage); }[/c]

Arrays in structures:

shape Description

An Array is the collection of similar type of elements stored in continuous memory address whose range is defined before the declaration.The elements in array can be selectively updated.

shape Example

[c] #include <stdio.h> #include <string.h> struct student { int id; char name[30]; float percentage; }; int main() { int i; struct student record[2]; // 1st student's record record[0].id=1; strcpy(record[0].name, "John"); record[0].percentage = 86.5; // 2nd student's record record[1].id=2; strcpy(record[1].name, "Mike"); record[1].percentage = 90.5; // 3rd student's record record[2].id=3; strcpy(record[2].name, "Anderson"); record[2].percentage = 81.5; for(i=0; i<3; i++) { printf("Records of STUDENT : %d \n", i+1); printf("Id is: %d \n", record[i].id); printf("Name is: %s \n", record[i].name); printf("Percentage is: %f\n\n",record[i].percentage); } return 0; }[/c] Output: [c] Records of STUDENT : 1 Id is: 1 Name is: John Percentage is: 86.500000 Records of STUDENT : 2 Id is: 2 Name is: Mike Percentage is: 90.500000 Records of STUDENT : 3 Id is: 3 Name is: Anderson Percentage is: 81.500000 [/c]

Structures as function arguments by using pointers

shape Description

The information is passed to the functions by using arguments.Basically, a function consists of fixed number of parameters and for every parameter passing of an argument is needed. The memory address given to a particular variable is called as a Pointer.

shape Example

[c] #include <stdio.h> #include <string.h> struct student { int id; char name[30]; float percentage; }; int main() { int i; struct student record1 = {1, "William", 90.5}; struct student *ptr; ptr = &record1; printf("Records of STUDENT1: \n"); printf(" Id is: %d \n", ptr->id); printf(" Name is: %s \n", ptr->name); printf(" Percentage is: %f \n\n", ptr->percentage); return 0; }[/c] Output: [c] Records of STUDENT1: Id is: 1 Name is: William Percentage is: 90.500000 [/c]

Summary

shape Key Points

  • Variables of different data type and same name in structures can be accessed with dot operator.
  • Every structure member are given different memory space and can be accessed individually.

shape Programming Tips

  • The structure declaration type must end with a semicolon( ; ).
  • Basically, structure in C is written in global declaration section.That can also be done in main section.
  • Initialization of all the members of the structure is not necessary.