Chapter 8 Structure in C

Embed Size (px)

Citation preview

  • 7/29/2019 Chapter 8 Structure in C

    1/29

    Structure in C

  • 7/29/2019 Chapter 8 Structure in C

    2/29

    Important Question

    Explain Structure with example

    Explain Union with example.

    State diff between Array and Structure

    State diff between Structure and Union

  • 7/29/2019 Chapter 8 Structure in C

    3/29

    Topic Covered

    Introduction

    Declaration

    Accessing

    Initialization

    Copying & Comparing

    typedef and its use in Structure Declaration

    Nesting of Structure

  • 7/29/2019 Chapter 8 Structure in C

    4/29

    Topic Covered

    Arrays of Structure

    Initializing Arrays of Structure

    Arrays within Structure

    Structures and Pointers

    Structures & Functions

  • 7/29/2019 Chapter 8 Structure in C

    5/29

    Introduction

    Arrays require that all elements be of the same datatype.

    Many times it is necessary to group information ofdifferent data types.

    An example is a materials list for a product. The listtypically includes a name for each item, a part number,dimensions, weight, and cost.

    C and C++ support data structures that can store

    combinations of character, integer floating point andenumerated type data. They are called a structs.

  • 7/29/2019 Chapter 8 Structure in C

    6/29

    Structure

    Structure is user defined data type which is used to store

    heterogeneous data under unique name.

    A structure is a collection of variables under a single

    name. These variables can be of different types, and eachhas a name which is used to select it from the structure.

    A structure is a convenient way of grouping together

    pieces of related information.

    Keyword 'struct' is used to declare structure.

    A single structwould store the data for one object. An

    array ofstructs would store the data for several objects.

    The variables which are declared inside the structure are

    called as 'members of structure'.

  • 7/29/2019 Chapter 8 Structure in C

    7/29

    General Form

    Struct

  • 7/29/2019 Chapter 8 Structure in C

    8/29

    Explanation

    The structure_tag_name is the name of the structure

    The structure_variables are the list of variable names

    separated by commas

    Each of these structure_variable name is a structure oftype structure_tag_name

    Each member_name declared within the braces is called a

    member or structure element.

  • 7/29/2019 Chapter 8 Structure in C

    9/29

    Declaration

    There are three different ways to declare

    and/or define structure

    Variable structure

    Tagged structure

    Type-defined structure

  • 7/29/2019 Chapter 8 Structure in C

    10/29

    Variable Structure

    struct

    {

    member_list}variable_identifie;

    struct

    {

    int x;int y;

    }a;

  • 7/29/2019 Chapter 8 Structure in C

    11/29

    Tagged Structure

    The tag name is referred to in subsequent declarations of

    variables of the type so defined.

    Each such declaration MUST include the keyword struct

    AND the name of the user-defined structure type AND the

    variable name(s).

    struct tag_name

    { member_list

    }variable_identifie;

    struct s1

    {int x;

    int y;

    }a;

  • 7/29/2019 Chapter 8 Structure in C

    12/29

    Where Declare???

    The proper place for structure declarations is in the

    global area of the program before main()

    This puts them within the scope of the entire program

    and is mandatory if the structure is to shared by function.

  • 7/29/2019 Chapter 8 Structure in C

    13/29

    Accessing

    In C program for accessing the elements of structure

    use dot operator('.').

    For accessing element you need to the structure

    variable.The general format is :

    .;

    It has the same precedence as () and [], which is higher

    than that of any unary and binary operator. Like () and [], it associates from Left to Right.

  • 7/29/2019 Chapter 8 Structure in C

    14/29

    Demo

  • 7/29/2019 Chapter 8 Structure in C

    15/29

    Initialization of Structure

    A structure can be initialized in much the

    same way as any other data type.

    This consists of assigning some constants to

    the members of the structures.

    Default Value

    For integer and float : 0 (zero)

    For char and string type : \0

  • 7/29/2019 Chapter 8 Structure in C

    16/29

    General Intializing Structre

    struct

    {

    ;

    ;

    .

    .

    }={constant1, constant2,..>

    Or

    struct

    = {constant1, constant2,..}

  • 7/29/2019 Chapter 8 Structure in C

    17/29

    DEMO

  • 7/29/2019 Chapter 8 Structure in C

    18/29

    ARRAY VS STRUCTURE

  • 7/29/2019 Chapter 8 Structure in C

    19/29

  • 7/29/2019 Chapter 8 Structure in C

    20/29

    Union

    Union is user defined data type used to stored data under

    unique variable name at single memory location.

    Union is similar to that of stucture.

    Syntax of union is similar to stucture. But the major difference between structure and union is

    'storage.'

    In structures, each member has its own storage location,

    whereas all the members of union use the same location. Union contains many members of different types, it can

    handle only one member at a time.

    To declare union data type, 'union' keyword is used.

  • 7/29/2019 Chapter 8 Structure in C

    21/29

    Declaration

    Syntax:

    union union_name

    {

    element 1;

    element 2; element 3;

    }union_variable;

    Example:

    union techno

    {int comp_id;

    char nm;

    float sal;

    }tch;

  • 7/29/2019 Chapter 8 Structure in C

    22/29

    Memory Allocation for Union

  • 7/29/2019 Chapter 8 Structure in C

    23/29

    Accessing

  • 7/29/2019 Chapter 8 Structure in C

    24/29

    DEMO

  • 7/29/2019 Chapter 8 Structure in C

    25/29

    Output

  • 7/29/2019 Chapter 8 Structure in C

    26/29

    Difference Between Union and Structure

  • 7/29/2019 Chapter 8 Structure in C

    27/29

    Memory Required for Structure

    There is difference in memory allocation between union

    and structure as suggested in above example.

    The amount of memory required to store a structure

    variables is the sum of memory size of all members.

  • 7/29/2019 Chapter 8 Structure in C

    28/29

    Memory Required for Union

    But, the memory required to store a union variable is the

    memory required for largest element of an union.

  • 7/29/2019 Chapter 8 Structure in C

    29/29

    Structure Vs Union