C Structure is the group of heterogeneous data structure which can store data of different data types or Structure is a collection of variables of different types under a single name.
In the case of array, we can store the data of same data type but what if we have to save data of different data type in structure. Can use single structure name for more type of data in structure and union. Storage area is not reserved by the compiler to structure definition.
- Keyword
struct
is used for creating a structure.
- Structure is a user defined data structure.
C Structure Syntax
struct tag_filed_name
{
data_type_1 member_1;
data_type_2 member_2
.
.
.
data_type_N member_N
}
How to create a structure?
‘struct
’ keyword is used to create a structure. Following is an example.
struct address
{
char name[50];
char street[100];
char city[50];
char state[20];
int pin;
};
Rules
- Use declare semicolon };at end of structure. (Don’t forget the semicolon in the ending line.)
- Referencing structure members :
struct_var-name . member_variable
- A structure variable can be assigned to other using
=
, but cannot be compared with other using ==
- In Structure bit cant set in float variable.
- Bit-fields are only part of structs or unions because that’s what the C standard allows
static members can’t allowed in structure
- In C,
struct
and union
types, we cannot have static
members.
- In C++,
struct
types are allowed to have static
members, but in union
cannot have static
members in C++ also.
- The Below C program output is compilation error.
#include<stdio.h>
struct st
{
int x;
static int y;
};
int main()
{
printf("%d", sizeof(struct st));
return 0;
}
How to declare structure variables?
Way 1:
struct struct_name
{
data_type member_1;
data_type member_2;
}var_1, var_2;
Way 2 : struct struct_name var_1, var_2;
struct struct_name
var_1, var_2;
How to initialize structure members?
Note: Structure elements cannot be initialized. When we declare a structure or union, we actually declare a new data type suitable for our purpose. By default, structure member init with a NULL value. It means Zero.
static struct structure_name var-name =
{
var_1, var_2,...var_n
};
So we cannot initialize values as it is not a variable declaration but a data type declaration. The below program output is Compilation error.
#include‹stdio.h›
int main()
{
struct site
{
char name[] = "ArunEworld";
int no_of_pages = 20;
};
struct site *ptr;
printf("%d ", ptr->no_of_pages);
printf("%s", ptr->name);
getchar();
return 0;
}
In a school, we need to store data like name, roll no, address and percentage. But we are able to store only one data type at a time. So, C programming provides a structure data type that can store data of different types such as integer, float, character, etc. In the case of the above school, we can initialize structure as (refer below example)
struct info
{
char name [10];
int roll_no;
char address [10];
float per;
};
Read/write in Structure variables
- . (colon) is the member of a structure
- -> (arrow) is the member of a POINTED TO structure
x -> y
and (*x).y
are both the same
When to user -> (arrow) operator.?
- When the structure/union variable is a pointer variable, we use the arrow operator to access structure/union elements.
What is a self-referential structure?
- We call a structure containing the same structure pointer variable as its element a self-referential structure.
Nested structure [structure with structure]
- We refer to a structure containing an element of another structure as its member.
Example : School Data Base
The below example explains many things about the structure
- This below code is the best example for structure array.
/*********************************
Date: 2016-06-04
Authur: Arun
********************************/
#include <stdio.h>
#include <stdint.h>
//The structure used to store the school student details
typedef struct student_Details
{
uint8_t Roll_no;
const char* studnt_Name;
uint8_t Tamil;
uint8_t English;
uint8_t Maths;
uint8_t Science;
uint8_t SocialScience;
}student_Details;
// Already declared structure can initialize, throw here.
static student_Details Entry[]=
{
{1, "Arun", 0, 0, 0, 0, 0},
{2, "Jhon", 0, 0, 0, 0, 0},
{3, "Raja", 0, 0, 0, 0, 0},
{4, "Gaurav", 0, 0, 0, 0, 0},
{5, "Smith", 0, 0, 0, 0, 0},
{6, "Jothi", 0, 0, 0, 0, 0},
};
//Printing the structure array
void printStudentDetails()
{
printf("\n\r Total No of stundets : %ld", sizeof(Entry)/sizeof(student_Details));
uint8_t i=0, No_of_students = sizeof(Entry)/sizeof(student_Details);
printf("\n _________________________________________________________");
printf("\n | ID | Name \t | S1 \t | S2 \t| S3 \t| S4 \t| S5 \t |");
printf("\n _________________________________________________________");
while(No_of_students != i)
{
printf("\n\r | %d | %s \t | %d \t | %d \t | %d \t | %d \t | %d \t | ",
Entry[i].Roll_no,
Entry[i].studnt_Name,
Entry[i].Tamil,
Entry[i].English,
Entry[i].Maths,
Entry[i].Science,
Entry[i].SocialScience);
i++;
}
printf("\n _________________________________________________________");
}
void UpdateStudentRecord(int Rn,int Sb, int Mrk)
{
if(Sb == 1) Entry[Rn-1].Tamil= Mrk;
else if(Sb == 2) Entry[Rn-1].English= Mrk;
else if(Sb == 3) Entry[Rn-1].Maths= Mrk;
else if(Sb == 4) Entry[Rn-1].Science= Mrk;
else if(Sb == 5) Entry[Rn-1].SocialScience= Mrk;
}
void PrintHelperMenu()
{
printf("\n helper Menu ");
printf("\n 1 - Print Student record");
printf("\n 2 - Update the Student Only One subject mark Mark");
}
void helperMenu()
{
int scanValue;
PrintHelperMenu();
while(1)
{
printf("\n");
scanf("%d",&scanValue);
if(scanValue == 2 )
{
int Rn,Sb,Mrk;
printf("\n 1 - Tamil, 2 - English, 3 - Maths, 4 - Science, 5 - Social Science.");
printf("\n Enter the Roll No, Subject and Mark : ");
scanf("%d%d%d",&Rn, &Sb, &Mrk);
UpdateStudentRecord(Rn,Sb,Mrk);
printStudentDetails();
PrintHelperMenu();
}
else if( scanValue == 1)
{
printStudentDetails();
PrintHelperMenu();
}
}
}
int main()
{
helperMenu();
return 0;
}
Structure Padding
During struct memory allocation, the system adds empty bytes based on the controller/processor type (8/16/32/64 bit), which can lead to memory issues when working with large arrays of data.
Structure Packing
Reduce the amount of memory that your application requires by packing data into structures. This is especially important if you need to store and access large arrays of data.
pack() function can be use to packing the structure memory.
- Syntax: #pragma pack(parameters)
- Parameters: 1 – Aligns structure members on 1-byte boundaries, or on their natural alignment boundary, whichever is less.
- Parameters: 2 – Aligns structure members on 2-byte boundaries, or on their natural alignment boundary, whichever is less.
- Parameters: 4 – Aligns structure members on 4-byte boundaries, or on their natural alignment boundary, whichever is less.
- Parameters: 8 – Reserved for possible future use.
- Parameters: 16 – Reserved for possible future use.
- Pack() function : Reference Link
- Reference Link
Hits (since 2024-Jan-26) - 847