Sunteți pe pagina 1din 2

NOTES ON STRUCTURES (CLASS XI D) Array is used to represent a group of data items that belongs to same data-type, such as int

or float. However, if we want to represent a collection of data items of different data-types using a single name, then we cannot use an array. For that we use structure. A structure is a user-defined data type, which contains group of related variables of different data types 1. STRUCTURE DEFINITION Struct tag_name datatype element_!"####. $;// structure termi ate! "it# semi c$%$ int rollno" char name%!&&'" char address%!&&'" $"((N$ mem$ry e.g. struct student reser&ati$

student s!,s%)'" 'em$ry reser&e! ((s! is structure variables * s is an array of structures of si+e ). ,hen we !e(i e structure $utsi!e a%% (u cti$ bodies it becomes )%$*a% and can be used by any other function, whereas when !e(i e! i si!e becomes %$ca% and only that function can access that structure. Ta) ame is $+ti$ a% but if you omit then only variables can be created at the time of structure definition only. -.g. struct int admno" char name%.&'" $ s1,s-,s.; // &aria*%es ca *e create! $ %y #ere as structure ta) is missi ) .. ACCESSIN/ STRUCTURE ELE'ENTS /0 12345 'E'0ER O1ERATOR 2.3 -6ample 7 student s!" s1.r$%% $ 8 !"strcpy 9s1. ame, 2hantilal:"strcpy 9s1.a!!ress, ooty:" .. INITIALI4ATION OF STRUCTURE5 6 ,e can initiali+e structure as li;e as array by specifying list of values in curly brac;et. stu!e t s18 !,2hantilal, Ahmedabad $" /ut we ca e.g. $t initiali+e the structure element during structure definition. (( /i&es compilation err$r$ struct student int rollno8&"

<. ARRA7 OF STRUCTURE5 6 student s%!&'" ,e can initiali+e the array of structure as li;e as the two dimensional array7 e.g. student s%.' 8 !, 2hantilal, Ahmedabad$, ., =ul>i,/hadara$$"

8. Neste! Structures A structure having another structure as its member element. stuct a!!r int houseno" char area%.?'" $" struct em+

int empno" char name%.?'" a!!r !1" How to access nested structure members e!. empno" e!.name e1.!1.#$use $

$e1"

((structure variable of emp structure

e1.!1.area

9. Array :it#i struct student int rollno"

Structure

char name%.!'"((character array of si+e .! float mar;s%@'" ((array of @ floats $" stu!e ts %ear er;

;. 1assi ) Structures t$ Fu cti$ s 3t can be achieved in two ways 7 9i: *y +assi ) i !i&i!ua% structure e%eme ts , and 9ii: *y +assi ) t#e e tire structure . /oth the ways can be achieved by A:+ass *y &a%ue as well as by b: +ass *y re(ere ce . Aassing by value is useful when the original values are not to be changed and passing by reference is useful when original values are to be changed.

<. Use $( ty+e!e( ($r !ec%ari ) structures Bhe typedef function is used to declare the alias name for the structure type. typedef struct results ######. $ progress" resu%ts report!" +r$)ress report. in this above program segment the usage of typedef defines a structure results and an alias name of the same structure as progress.

=. Structure C$+y Cne structure variable can be assigned to another structure variable where member by member copy will be done i( a ! $ %y i( the two structure variables *e%$ ) t$ t#e same structure. -6. struct student int admno"char name%.&'"$" student s!8 !,abc$, s." s->s1; //structure c$+y where member by member s! will be copied onto s.

S-ar putea să vă placă și