C –Structure

tutorial and example
tutorial and example

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:

Note: The given above example in which id,name,class, mob are the members of structure, struct is a keyword and student is structure tag.

Example 2:

Array 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;

}

C –structure using Pointer

Dot(.) operator is used to access the data using normal structure variable and arrow( ->) is used to access the data using pointer variable.

We can access C structure in two ways.

  1. Using normal variable

Using pointer variable