c structure

c structure

Talk about Array the array is a collection of same data types of elements but in the real life application, we may need different types of data. if if talk about real life student data, we want to create the record of student that contains the name, age, rollno. and course of that student here we can not use the array because all here all elements are of different datatypes. so to store these type of fields of the different data types we can use structure. which is capable to storing heterogeneous data. And the data elements of the structure are referred as members.

Declaration a structure type

Declaration of the structure create a template that describes the characterstics of this members. All the variable or members declare in the structure type, will take the form of this template.so The general structure declaration syntax is

struct tagname
{
datatype member1;
datatype member2;
datatype member3;
datatype member4;
datatype member5;
datatype memberN;
};

in the above example struct is a keyword member1,member2,member3……memberN are members of the structure and are declare with in the curly braces. And there should be a semicolon at the end of curly braces. These all members can be of any type or data type such as int, float, char or pointer etc And the tagname is the name of the structure. and tagname will be used in the program to declare variable of type structure type Declaration of the structure provided one more data type in addition to build in data types.

Note: It is important to note that declaration of the structure template does not reserve any space in the memory for members. space in the memory are reserve only when members are declared of that structure type. The members name inside the two different structure can also be same

Let us take example to declare the structure

struct student
{
char name[15];
flaot marks;
int roll;
};

in the above example the student is the structure tag name and there are three members of structure student these are name, marks, roll, so the structure template can be declare globally or locally, it can be declare before all functions in the program.

if the structure template is globally then all functions can be used to this template or it can be used by all functions in the program and if the structure template is locally then function containing it can use it.

Declaration of structure variable

For declaration structure we have only created template or format the real use of structure is when we will declare variables based on this format. there are two ways to declare structure variable

1. with structure declaration 2. using structure tag

with structure declaration

struct Employee
{
char name[15];
int empid;
float salary;
}
stu1,stu2,stu3;

in the above example stu1,stu2,stu3 are the variable of type struct employee. so when we declare the variable during when declaration the structure,The tagname is optional so we can declare the structure without tagname such as

struct
{
char name[15];
int empid;
float salary;
}
stu1,stu2,stu3;

using structure tag

so we can also declare the structure with tag name. This can be written as

struct Employee
{
char name[15];
float salary;
int Empid;
};
struct Employee emp1,emp2;
struct Employee emp3;

in the above example emp1,emp2 and emp3 are variables that are declared using the structure tag employee. so when declaration a structure variable it reserve space in memory, so each structure variable of type struct Employee and has three members name,salary,Empid, The compiler has reserve memory for each variable to hold all the members.

Initialization of structure variables

After understand the structure let us understand how to initialize structure variable syntax of initialization of structure variable is similar to the arrays, so all the values are given in the curly braces and the order and the type of these value should be same as in the structure template defination. so let us take the example

struct Employee
{
char name[20];
float salary;
int Empid;
}
Emp1={“Rakesh”,3000.90,086};
struct employee emp2={“Ram”,309.67,890};

in the above example values of members of Emp1 will be “Rakesh” for variable name, 3000.90 for variable salary and 086 for Empid And the values of members of emp2 will be “Ram” for variable name, 309.67 value for vraible salary and 890 for Empid.

Note: We can not initialize members when define the structure

struct Employee
{
char name[10];
float salary;
int empid=990; /* invalid */
}
emp;

In the above example one statement is invalid because there is no variable name empid and no memory is allocated while declaration of structure type.
Note : If number of initializers are less then the number of members then remaining members will initialize with zero value let us take example

struct Employee emp={“Ram”};
here salary and empid members of emp will be initialize with zero. so after Initialization it is equivalent to

struct Employee emp={“Ram”,0,0};