C - STRUCTURE

 STRUCTURE

Structure is a user defined data type that can store and process data items of different data types. convenient tool for handling a A structure is a group of logically related data items. For example, it can be used to represent a set of attributes, such as student name, roll number and marks.

SYNTAX

struct<structure name>

{

datatype identifier1;

datatype identifier2;

-----------------------

-----------------------

datatype identifiern;

};

Here , struct is a keyword.

 EXAMPLE

Struct student

{

char name[10];

int rollno, marks;

};

Here, student is name of structure or tag name and name rollno , marks are called elements or members.

 

DECLARATION OF STRUCTURE VARIABLES

In order to be able to use the structure we have to declare the variable of structure type.

For Example, variables of structure student can be declaring as:

struct student s1 , s2;

Here, s1 and s2 are called structure variables. Structure variables can also be declared as

struct student

{

char name[10];

int rollno , marks;

} s1, s2;

INITIALIZATION OF STRUCTURE VARIABLES

Structure variables can be initializing as

struct student s1= { “GEETA”,104,15 };

 

ACCESSING THE MEMBERS OF THE STRUCTURE

To access the members of the structure variables , we use member operator ‘.’ which is also known as ‘dot operator’ or ‘period operator’.

SINTAX

Structure variable.member name;

EXAMPLE

s1.marks=18;

s1.rollno=150;

strcpy(s1.name,”abc”);

 

Nested Structure

Nesting of structure means having one structure within another structure. By using this facility complex data types can be created. An innermost member in a nested structure can be accessed by chaining all structure variables (from outermost to innermost) with the member using dot operator. An inner structure can have more than one variable.

Example

Struct dob

{

int dd.mm.yy;

}date;

struct student

{

int rollno;

char name[10];

struct dob date;

};

main()

{

struct student s1;

printf(“ Enter rollno:” );

scanf(“%d”,& s1.rollno);

printf(“Enter name”);

gets(s1.name);

printf(“Enter date of birth:”);

scanf(“%d”%d%d”,&s1.date.dd,&s1.date.mm,&s1.date.yy);

printf(“\nname is:”);

printf(s1.name);

puts(s1.name);

printf(“rollno is:%d”,s1.rollno);

printf(“date of birth is %d-%d-%d”,s1.date.dd,s1.date.mm,s1.date.yy);

getch();

}

 OUTPUT

Enter rollno:102

Enter name: abc

Enter date of birth: 20 10 82

name is : abc

rollno is : 102

date of birth is : 20-10-82

 

ARRAY OF STRUCTURE

Like other data types, we can also create array of structure.

For example-

Let there is a structure storing information about student.

struct student

{

int rollno;marks[3];

char name[20];

};

Now. If there are 50 students in a class, we may declare all the students as structure variable. In such cases, we may declare an array of structures.

struct student st[50];

Example

struct student

{

int rollno,marks[3];

char name[20];

};

main()

{

struct student st[3];

int i,,j;

static int total[5];

clrscr();

for (i=0;i<5;i++)

{

printf (“Enter record of student %d”,i+1);

printf (“Enter rollno : “);

scanf (“%d”,&st[i].rollno);

printf (“Enter name : “);

gets (st[i].name);

for (j=0;j<3;j++)

{

printf (“Enter marks %d : “,j+1);

scanf (“%d”,&st[i].marks[j]);

total[i]+=st[i].marks[j];

}

}

for (i=0;i<5;i++)

{

printf (“Records of student %d :”, i+1);

printf (“Rollno is %d”,st[i].rollno);

printf (“Name is : “);

puts (st[i].name);

printf(“Total marks : %d”,total[i]);

}

getch();

}

 

STRUCTURE ASSIGNMENT

A structure variable can be assigned to another structure variables only if both variables are of same structure type.

FOR EXAMPLE

struct student

{

int rollno,marks;

char name[10];

};

struct student s1={101,20,”abc”}

struct student s2;

Now to assign values of s1 to s2, we can write

s2=s1;

values of s1 is assigned to s2

 

STRUCTURE AS FUNCTION ARGUMENT

Structure values can be passed as arguments to functions. The first method is to pass each member of the structure as an actual argument of the function call. The actual arguments behave like ordinary variables in the function. But, this method is not efficient when the structure size is large.

Another method involves passing entire structure to the called function.

syntax for passing structure to the called function is:

function_name(structure_variable_name);

The called function is defined as:

return_type function_name(struct structure_name structure_variable)

{

……………………

……………………

return (expression);

}

 Example

struct student

{

float marks1,marks2;

char name[10];

};

main()

{

float myfun(struct student),arg;

struct student st={50,80,”bca”);

avg=myfun(st);

printf(“\naverage=%f”,avg);

getch();

}

float myfun(struct student s1)

{

float a;

a=s1.mark s1+s1.marks2;

return(a);

}

 

FUNCTION THAT RETURN STRUCTURE

A function can also return back structure to calling function.

SYNTAX

struct structure name function name (struct structure name)

{

……………………..

……………………..

return (structure name );

};

Comments

Popular posts from this blog

Courses After 12th Science

MS Office Versions List

Computer - Network Topologies