structure in c


Arrays  can  be used to represent  group of data item  that  Arrays  can  be used to represent  group of data item  that
belong  to the same name same datatype  such  as int, char or  float.If  we want  to  represent  group  of  data  items  with  Different  Data type using single name then we cannot use an array.C   supports to mark new data type known as structure using keyword struct and union , A structure  can be used to   group all related information
(different data) that belong to the same name  and different datatype.  Structure should be declare before main function or as a  header file .it used as template .

Syntax  of   Structure :-

struct  tag_ name

{

data type  member_1;

data type  member_2;

data type  member _ n ;

};

struct  student

{

char name[15];

int no;

float marks ;

} s1, s2 ;

Where struct is a keyword. Tag_name  is a new data type. name rule as identifier like variable , function, array.template s terminated with semicolon.Each member is declared independently for its name and type
in a separate statement inside the template. Declaring  structure variable A structure variable is declaration is similar to the declaration of variables of any other data type.

it includes the following elements:

1. The keyword struct.

2. The structure tag_name(new user define datatype. like above example student is new type )

3. list of variable names separated by commas.

4. A terminating semi colon ;

struct  student

{

char name[25];

int no; Structure Member

float marks ;

}  s1, s2 ;      Structure Global variable

void main( )

{

struct student s3, s4 , s5 ;  //  Structure Local variable

}

The members of structure  variable do not occupy any memory until they are associated with the structure variable such as s1,s2,s3…compiler comes across a declaration statement , it reserve memory space for the struct variable. It is also allowed to combine the structure definition and variable declaration as global or local in single statement  Accessing structure members The link between the member and a  variable is established using the member selection operator ‘.’ which is also known as “dot operator” or “period operator” . It is also allowed to combine the structure definition and variable declaration as global or local in single statement.

To access the member of structure using . dot operator

structure_variable.structure_member

like as   s1.name; s2.no;  s3.mark;

Example Simple Structure Program :

Create structure  student to store student name and id_no of two students  print which student get maximum marks.

Source Code

Output of Structure of Student

Enter Name and Number
Mango
45

Record of Maximum Marks
Name=Apple
Number=123

(Visited 61 times, 1 visits today)

Leave a Reply

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


CAPTCHA Image
Reload Image