structures and union

Upload: nayemuddin-ahmed

Post on 06-Apr-2018

217 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/3/2019 Structures and Union

    1/14

    Structures and Union

    To represent a collection of data

    items of different types using asingle name.

  • 8/3/2019 Structures and Union

    2/14

    Why Structure

    We have seen that arrays can be used torepresent a group of data items that belong tothe same type,such as int or float.

    However we can not use an array if we want torepresent a collection of data items of differenttypes using a single name.

    That can be done using structure.

  • 8/3/2019 Structures and Union

    3/14

    What is Structure

    A structure in C is a heterogeneous userdefined data type.It groups variables into asingle entity.

    A structure is a convenient tool for handlinga group of logically related data items.

  • 8/3/2019 Structures and Union

    4/14

    What is Structure

    Arrays stores homgenous data wherestructuer can hold heterogeneous data.

    Unlike arrays, structures must be definedfirst fo their format that may be used laterto declare structure variables.

  • 8/3/2019 Structures and Union

    5/14

    Structure Declaration

    struct student{

    char name[25];int age ;

    float marks;};

    Above declaration groups name,age and marks into a singleentity student.

    struct is a reserved word.student is called structure tag or simply tag.This is the nameof the structure

    NB: NO MEMORY IS ALLOCATED SO FAR.

  • 8/3/2019 Structures and Union

    6/14

    A new Type is defined by theProgrammer

    student is a new type defined by the user,which canhold heterogenous data.

    To use this structure student in a program we must create a

    variable of type student as follows:

    struct student x;Always remember to give the keyword struct before thestructure name while creating a variable.

    x is a variable of type student.just like int z; z is a variable of type integer.

  • 8/3/2019 Structures and Union

    7/14

    Using structure variable

    #include#includemain()

    {

    struct student{

    char name[25];int age;float marks;

    };

    struct student x;

  • 8/3/2019 Structures and Union

    8/14

    Using structure variable

    clrscr();printf("Enter name: ");scanf("%s",x.name);

    printf("Enter age : ");scanf("%d",&x.age);printf("Enter marks:");scanf("%f",&x.marks);

    printf(" \n\nName :%s \n Age :%d \nMarks:%.2f",x.name,x.age,x.marks);getch();

    }

  • 8/3/2019 Structures and Union

    9/14

    Array of Structures

    To maintain information about 10students,declare an array of studentstructure.

    struct student x[10];

    To store age of first student we writex[0].age=27;

  • 8/3/2019 Structures and Union

    10/14

  • 8/3/2019 Structures and Union

    11/14

    Pointers to structure

    struct student x,*ptr;

    ptr=&x;

    ptr->age=27;

  • 8/3/2019 Structures and Union

    12/14

    A Program

  • 8/3/2019 Structures and Union

    13/14

    Unions

    A union is a user defined data type in C whichallows the overlay of more than one variable inthe same memory area.

    For example if a string of 200 bytes called filenameis required by first 50 lines of the code only andanother string called output of 400 bytes is

    required by the rest of the code(i.e both stringsnot needed simultaneouly),it would be waste oftime to declare separate arrays of 200 and 400bytes

  • 8/3/2019 Structures and Union

    14/14