
Structure in C is a user-defined data type that the user defines according to his needs. These are collection of various types of data. Members of a structure can be of any type, including variables, pointers, arrays, and structures.
For example, you may want to retain and process student information such as name, class, marks, grade. For such purposes you can declare a structure “StudentInfo” with required fields.
struct StudentInfo { char studName[20]; float marks; char studClass[10]; char grade; } stud;
We declare a structure with the struct keyword. Structure, in terms of memory, takes up space in memory in proportion to its size.
Table of Contents
Syntax for a struct declaration in C
struct [structure-tag-name] { // Structure member 1 // Structure member 2 // Structure member 3 ... } [structure variable(s)];
struct StudentDetails { char name[20]; char address[100]; int age; };
Accessing Structure Members in C
We access any member of the structure using the dot (.) operator and structure variables. We refer the dot (.) operator as the member access operator.
Structure Variables Definition and Declaration in C
Structure variables are similar to regular variables in that they allow us to quickly access members of a structure. We can declare variables of a struct during the definition of the struct or after definition.
Declaring Structure variables separately
We declare structure variables in the main() function using this technique.
struct StudentDetails { // Structure definition }; int main() { struct StudentDetails struct_var1, struct_var2; return 0; }
Declaring Structure variables with structure definition
We declare the structure variable in this way at the end of the structure definition, immediately before the end of the structure.
struct StudentDetails { // Structure definition } struct_var1, struct_var2;
Nested Structure in C Programming
We can define a structure inside of another structure. This type of structure is called a nested structure. To put it another way, a nested structure is one that is a member of another structure. Structures can be nested mainly in the following two ways.
By using separate Structure
We create many structures in this form of nested structure and write the dependent structure as a structure member inside the primary structure.
struct Address { char city[10]; char state[10]; }; struct Employee { char name[20]; struct Address add; } emp1, emp2;
By using embedded Structure
We declare the structure within the main structure as a structure member in this type of nested structure.
struct Employee { char name[20]; struct Address { char city[10]; char state[10]; } add; } emp1, emp2;
Arrays of Structures in C
We can store multiple structures in an array. An array of structures is actually a collection of many structures. If you have a Student structure that requires you to keep 5 student records, you may use structure in this scenario.
struct Student { int roll; char name[10]; }; int main(){ struct student stud[5]; }
Pointers to structures in C language
A pointer to a structure is defined as a pointer to the address of a memory block containing the structure. To put it another way, a pointer will contain the address of structure.
Click here to learn pointer in C programming
#include<stdio.h> #include<conio.h> struct Emp { int id; float salary; }; void main() { struct Emp emp,*ptr; printf("Input values : \n"); ptr = &emp; scanf("\n%d%f",&ptr->id, &ptr->salary); printf("\nEmployee Record"); printf("\n%d\t%f",ptr->id, ptr->salary); }
Output: Input values : 10001 50000 Employee Record 10001 50000.000000
Passing Structure to function in C Programming
Functions can take structures as parameters, and functions can return structures. We can pass a structure to a function either as a member or as a variable.
You can learn functions in c programming from this link.
#include<stdio.h> #include<conio.h> #include<string.h> struct Website { char type[30]; char title[30]; }; void display(struct Website w); void main() { struct Website w; strcpy(w.type, "Educational Website"); strcpy(w.title, "C Programming for Geeks"); display(w); } void display(struct Website w) { printf("%s", w.type); printf("\n%s", w.title); }
Output: Educational Website C Programming for Geeks
Points to Remember About Structure in C
- Members of the structure are assigned memory locations in a sequential order.
- You can define a structure of arrays or an array of structures, and you can use dot operator to refer the members.
- A function can return a structure.
- It is possible to modify the members of a structure by passing the structure as an argument.
Recommended Articles
- Storage classes and its types
- Pointers in c programming with examples
- DBMS Concepts And Architecture Introduction
- Class and Structure difference
- What is the difference between internet and intranet?