c Unit 9 Structure and Union Print

Embed Size (px)

Citation preview

  • 8/18/2019 c Unit 9 Structure and Union Print

    1/7

    Programming Language Structure and union Unit 9

    Structure:

    A structure is a collection of one or more variables, possibly of different types, groupedtogether un-der a single name for convenient handling. (Structures are called ``records'' insome languages, not-ably Pascal.) Structures help to organie complicated data, particularlyin large programs, because they permit a group of related variables to be treated as a unitinstead of as separate entities.!hey are also called user defined data type.

    "ne traditional e#ample of a structure is the payroll record$ an employee is described

    by a set of attributes such as name, address, social security number, salary, etc. Some of these in turn could be structures$ a name has several components, as does an address andeven a salary. Anothere#ample, more typical for %, comes from graphics$ a point is a pair of coordinate, a rectangle is a pair of points, and so on.

    Declaration of Structure:

    !he general synta# for declaring a structure is$struct structure&name data&type member

    data&type member*data&type member+

    data&type member

    /e can define a structure to hold the information of a student as follo0s$struct Student char name1*2int roll

    char sec floatmar3s

    Structure variable declaration: structStudent s, s*, s+/e can combine both template declaration and structure variable declaration in onestatement. 4g,struct Student char name1*2

    int rollchar secfloat mar3s

    s1, s2, s3;

    Accessing members of a structure:

    !here are t0o types of operators to access members of a structure. /hich are$5 6ember operator (dot operator or period operator (.))5 Structure pointer operator (-7).

    Structure initialization:

    8i3e any data type, a structure variable can be initialied as follo0s$struct Student char name1*92int roll

    char sec floatmar3s

    struct Student s:;, **, ?A@, .!he s is a structure variable of type Student, 0hose members are assigned initial values.!he first member (name1*91) is assigned the string ;, the second member (roll) isassigned the integer value **, the third member (sec) is assigned the character ?A@, and thefourth member (mar3s) is as-signed the float value ..

    Ashok Pandey Page 1 of 7

  • 8/18/2019 c Unit 9 Structure and Union Print

    2/7

    Programming Language Structure and union Unit 9

    Example: a program to assign some values to te member of structure and todispla! tem on te screen"

    #includeBstdio.h7struct Sample int #

    float y

    struct Sample svoid main() s.#:

    s.y:+9.printf(;!he value of # is$CdDn>, s.#)printf(;!he value of y is$Cd>, s.y)

    "utput$!he value of # is$ !he value of y is$ +9.

    Example: program illustrates te structure in $ic read member elements ofstructure and displa! tem"#includeBstdio.h7struct Student char name1*92

    int rollchar secfloat mar3s

    struct Student svoid main()

    printf(;4nter the name of a student>) gets(s.name)printf(;4nter the roll number of astudent>) scanf(;Cd>,Es.roll)printf(;4nter the section of a student>)scanf(;Cc>,Es.sec)printf(;4nter the mar3s obtained by the student>)scanf(;Cf>,Es.mar3s) FFdisplaying the recordsprintf(;ame:CsDn

  • 8/18/2019 c Unit 9 Structure and Union Print

    3/7

    Programming Language Structure and union Unit 9

    #includeBstdio.h7struct Student char name1*92

    int rollchar secfloat mar3s

    void main()

    struct Student s1+2int ifor(i:9iB+iII) printf(;4nter the name of a student>)

    gets(s1i2.name)printf(;4nter the roll number of a student>)scanf(;Cd>,Es1i2.roll)printf(;4nter the section of a student>)scanf(;Cc>,Es1i2.sec)printf(;4nter the mar3s obtained by the student>)scanf(;Cf>,Es1i2.mar3s)

     FFdisplaying the recordsfor(i:9iB+iII) printf(;ame:CsDn ,s.name, s.roll, s.sec, avg)

    Structures $itin Structures:

    Structures 0ithin structures mean nesting of structures. Study the follo0ing e#ample andunderstand the concepts.

    Ashok Pandey Page 3 of 7

  • 8/18/2019 c Unit 9 Structure and Union Print

    4/7

    Programming Language Structure and union Unit 9

    Example: te follo$ing example so$s a structure definition aving anoter

    structure as a member" %n tis example, person and students are t$o structures"&erson is used as a member of student"'person $itin Student(

    #includeBstdio.h7struct Person char name1*92

    int age

    struct Student int rollchar secstruct Person p

    void main() struct Student s

    printf(;4nter the name of a student>)gets(s.p.name)printf(;4nter age>)scanf(;Cd>,Es.p.age)

    printf(;4nter the roll number of a student>) scanf(;Cd>,Es.roll)printf(;4nter the section of a student>)scanf(;Cc>,Es.sec) FFdisplaying the recordsprintf(;ame:CsDn ,s.p.name, s.roll,s.p.age, s.sec)

    Structure and functions:

    % supports passing of structure as arguments to function. !here are t0o methods by

    0hich the values of a structure can be transferred from on function to another.5 Passing by value- Passing structure members to the function- Passing entire structures to function

    5 Passing by reference

    &assing b! value:

    Passing a copy of structure member or entire structure to the called function. Any change tothe structure members 0ithin the function is not reflected in the original structure.

    &assing structure member to functions:

    #includeBstdio.h7void display(int, int)struct sample int # float y void main() structsample s:++, .

    display(s.#, s.y)void display(int a, float b) printf(;CdDnCd>,a, b)

    &assing entire structures to functions:

    #includeBstdio.h7void display(struct student)struct student char name1*92 int age

    int roll char sec

    Ashok Pandey Page 4 of 7

    E)uivalent form of nested structure is:

    struct Student int roll

    char sec

    struct Person char name1*92

    int agep

  • 8/18/2019 c Unit 9 Structure and Union Print

    5/7

    Programming Language Structure and union Unit 9

    void main() struct student s

    int iprintf(;4nter the name of a student>)gets(s.name)printf(;4nter age>)scanf(;Cd>,Es.age)printf(;4nter the roll number of a student>)

     scanf(;Cd>,Es.roll)printf(;4nter the section of a student>)scanf(;Cc>,Es.sec)display(s) FFfunction call

    void display(struct student st) FFdisplaying the records

    printf(;ame:CsDn ,st.name, st.roll,st.age, st.sec)

    &assing b! reference:!his method uses the concept of pointer to pass structure as an argument. !he addresslocation of the structure is passed to the called functions.#includeBstdio.h7void display(struct studentJ)struct student char name1*92 int age

    int roll char secvoid main() struct student s

    intiprintf(;4nter the name of a student>)gets(s.name)printf(;4nter age>)scanf(;Cd>,Es.age)printf(;4nter the roll number of a student>)scanf(;Cd>,Es.roll)printf(;4nter the section of a student>)scanf(;Cc>,Es.sec)display(Es) FFfunction call

    void display(struct student Jst) FFdisplaying the records

    printf(;ame:CsDn ,st-7name,st-7roll, st-7age, st-7sec)

    *!pedef:

    !he typedef feature allo0 us to define ne0 data types. Hts purpose is to redefine the nameof an e#isting variable type. !he general form of typedef is$

    !ypedef data&type ne0&type+or example:

    typedef int numtypedef float realnum i, =real a, bhere, variables num and real acts as data types int and float respectively.

    Ashok Pandey Page 5 of 7

  • 8/18/2019 c Unit 9 Structure and Union Print

    6/7

    Programming Language Structure and union Unit 9

    Example: program so$ tat te use of t!pedef 

    #includeBstdio.h7void display(int, int)struct sample int #

    float yvoid main()

    typedef struct sample stvarstvar s:KK, LM.Ndisplay(s.#, s.y)

    void display(int a, float b) printf(;CdDnCd>,a, b)

    nions:

    A union is a variable that may hold (at different times) ob=ects of different types and sies,0ith the compiler 3eeping trac3 of sie and alignment reOuirements. nions provide a 0ay to

    manipulate different 3inds of data in a single area of storage, 0ithout embedding anymachine-dependent information in the program.Qoth structure  and unions are used to group a number of different variables together.Syntactically both structure and unions are e#actly same. !he main difference bet0een themis in storage. Hn structures, each member has its o0n memory location but all members of union use the same memory location 0hich is eOual to the greatest member@s sie.

    Declaration of union:

    !he general synta# for declaring a union is$union union&name data&type member

    data&type member*data&type member+data&type member

    /e can define a union to hold the information of a student asfollo0s$union Student char name1*2

    int rollchar secfloat mar3s

    union variable declaration:

    union Student s, s*, s+0e can combine both template declaration E union variable declaration in one statement.4g,union Student char name1*2

    int rollchar secfloat mar3s

    s1, s2, s3;

    Accessing members of a union:

    Ashok Pandey Page 6 of 7

  • 8/18/2019 c Unit 9 Structure and Union Print

    7/7

    Programming Language Structure and union Unit 9

    !here are t0o types of operators to access members of a union. /hich are$5 6ember operator (dot operator or period operator (.))5 union pointer operator (-7).

    union initialization:

    8i3e any data type, a union variable can be initialied as follo0s$union Student char name1*2

    int rollchar secfloat mar3s

    union Student s:;, **, ?A@, .!he s is a union variable of type Student, 0hose members are assigned initial values. !hefirst member (name1*91) is assigned the string ;, the second member (roll) is assignedthe integer value **, the third member (sec) is assigned the character ?A@, and the fourthmember (mar3s) is as-signed the float value ..

    Example: a program to assign some values to te member of union and to displa!

    tem on te screen"#includeBstdio.h7 union Sample int # float y

    unionSample svoid main() s.#:

    s.y:+9.printf(;!he value of # is$CdDn>, s.#)printf(;!he value of y is$Cd>, s.y)

    -utput:!he value of # is$ !he value of y is$ +9.

    Example: program illustrates te structure in $ic read member elements of

    union and displa! tem"#includeBstdio.h7 union Student char name1*92 int roll

    char sec float mar3sunion Student svoid main() printf(;4nter the name of a student>) gets(s.name)

    printf(;4nter the roll number of a student>)scanf(;Cd>,Es.roll)printf(;4nter the section of a student>)scanf(;Cc>,Es.sec)printf(;4nter the mar3s obtained by the student>)scanf(;Cf>,Es.mar3s) FFdisplaying the recordsprintf(;ame:CsDn