C Structure

C  Structure:

C Structure is a group of different data types which are grouped together and each element in a C structure is called member.
• To access structure members in C, structure variable should be declared.
• Many structure variables can be declared for same structure and memory will be allocated for each separately.
• while declaring we can able to initialize a structure to null. if we don’t assign any values to structure members.

DISTINGUISH BETWEEN C ARRAY , C VARIABLE, AND C STRUCTURE:

• A normal C variable can hold only one data of one data type at a time.
• An array is a collection of data of same data type.
• A structure is a collection of data of different data types and D

C Structure:

Syntax of structure in C Syntax of structure in Cstruct studentclass

{

int a1;

char b1[10];

}

Example a1 = 10;b1 = “Hello”;

C Variable:

int Syntax: int a1;Example: a1 = 20;
char Syntax: char b1;Example: b1’Z’;

C Array:

int Syntax:int a1[3];Example:a1[0] = 10;a1[1] = 20;

a1[2] = 30;

a1[3] = ‘\0’;

char Syntax: char b1[10];Example: b1=”Hello”;

BELOW TABLE EXPLAINS FOLLOWING CONCEPTS IN C STRUCTURE.

1. How to declare a C structure?
2. How to initialize a C structure?
3. How to access the members of a C structure?

Using normal variable Using pointer variable
Syntax:struct tag_name1{ data type var_name1;data type var_name2;

data type var_name3; };

Syntax:struct tag_name1{ data type var_name1; data type var_name2;

data type var_name3; };

 Example:struct student1{ int mark1;char name1[10];

float average1; };

Example:struct student1{ int mark1;char name1[10];

float average1; };

Declaring structure using normal variable in Cstruct student report1; Declaring structure using pointer variable:struct student *report, rep1;
Initializing structure using normal variable:struct student report1 = {100, “Mani”, 99.5}; Initializing structure using pointer variable in C:struct student rep1 = {100, “Mani”, 99.5}; report = &rep1;
Accessing structure members using normal variable:report.mark1;report.name1;report.average1; Accessing structure members using pointer variable:report -> mark1;report -> name1;report -> average1;

 

 

 

 

 

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *