C –Structure
C structure is a collection of different types of data that is grouped together. It is used to represent records.Structkeyword is used to create a structure. Each element of a structure is called a member. Syntax:
structstructure name{ data_type member1; data_type member2; data_type member3; ------------------------- ------------------------- data_type member;Example1:
struct student{ int id; char name[30]; char class[30]; int mob; }
Note: The given above example in which id, name, class, mob are the members of the structure, struct is a keyword and student is structure tag.Example 2:
#include<stdio.h> struct abc{ // create structure using 'struct' keywrod int x, y; }; int main(){ struct abc p1 = {0, 1}; p1.x = 25; printf("Example of Structure"); printf ("x = %d \n y = %d", p1.x, p1.y); return 0; }Output
Example of Structurex = 25 y = 1Array of structure: Example
#include<stdio.h> struct abc{ // create structure using 'struct' keywrod int x, y,z; }; int main(){ struct abc arr[10]; // create an array of structures arr[0].x=10; arr[0].y= 20; arr[0].z = 30; printf("arr[0].x: %d \narr[0].y: %d \n arr[0].z: %d", arr[0].x, arr[0].y, arr[0].z); return 0; }Output
arr[0].x: 10 arr[0].y: 20 arr[0].z: 30C –structure using Pointer Dot(.) operator is used to access the data using normal structure variable and arrow( ->) is used to access the data using a pointer variable. We can access the C structure in two ways.
- Using a normal variable
- Using pointer variable