structures, headers, arrays, functions, strings, pointers in c

Upload: kchandra48

Post on 14-Apr-2018

233 views

Category:

Documents


0 download

TRANSCRIPT

  • 7/30/2019 Structures, Headers, Arrays, Functions, Strings, Pointers in C

    1/12

    structures

    header filesarrays

    functionsstrings

    pointers

    dynamic variables

    in C

  • 7/30/2019 Structures, Headers, Arrays, Functions, Strings, Pointers in C

    2/12

    variables, arrays, atructures

    Variables Arrays Structures

    int sum; int arr[7]; struct CItem{

    char name[21];

    float cost;};sum arr[0]

    arr[1]

    arr[2]

    arr[3]arr[4]

    arr[5]

    arr[6]

    name

    cost

  • 7/30/2019 Structures, Headers, Arrays, Functions, Strings, Pointers in C

    3/12

    application

    Representing shopping items as structures

    (records)

    Difference between types and variables

    If int is a type then (after int x;) x is a variable

    We can define a structure type and then we

    can define variables of that type Type definitions are like blueprints and

    variables are like houses

  • 7/30/2019 Structures, Headers, Arrays, Functions, Strings, Pointers in C

    4/12

    header files

    #define LONGEST_NAME 20+1

    struct CItem

    {

    char name[LONGEST_NAME];float cost;

    };

    void displayItem(struct CItem item)

    {printf("%-20s: %7.2f", item.name, item.cost);

    }

  • 7/30/2019 Structures, Headers, Arrays, Functions, Strings, Pointers in C

    5/12

    main function

    void main(void)

    {

    struct CItem p1;

    strcpy(p1.name, "USB Cable");

    p1.cost = 29.95f;displayItem(p1);

    printf("\n");

    printf("sizeof(struct CItem) = %d bytes\n", sizeof(struct CItem));

    printf("sizeof(p1) = %d bytes\n", sizeof(p1));

    printf("sizeof(p1.name) = %d bytes\n", sizeof(p1.name));

    printf("sizeof(p1.cost) = %d bytes\n", sizeof(p1.cost));

    getchar();

    }

  • 7/30/2019 Structures, Headers, Arrays, Functions, Strings, Pointers in C

    6/12

    output

  • 7/30/2019 Structures, Headers, Arrays, Functions, Strings, Pointers in C

    7/12

    populate function

    void populate(struct CItem items[], int n)

    {

    strcpy(items[0].name, "Tripod");

    items[0].cost = 12.95f;

    strcpy(items[1].name, "Charger");items[1].cost = 22.95f;

    strcpy(items[2].name, "HDMI Cable");

    items[2].cost = 29.95f;

    strcpy(items[3].name, "Ethernet Cable");items[3].cost = 9.95f;

    strcpy(items[4].name, "Surge Protector");

    items[4].cost = 25.95f;

    }

  • 7/30/2019 Structures, Headers, Arrays, Functions, Strings, Pointers in C

    8/12

    displayList function

    void displayList(struct CItem items[], int n)

    {

    int i;

    printf("%-20s: %7s\n\n", "Name", "Cost");for (i=0; i

  • 7/30/2019 Structures, Headers, Arrays, Functions, Strings, Pointers in C

    9/12

    main function

    struct CItem list[5];

    populate(list, 5);

    displayList(list, 5);

  • 7/30/2019 Structures, Headers, Arrays, Functions, Strings, Pointers in C

    10/12

    output

  • 7/30/2019 Structures, Headers, Arrays, Functions, Strings, Pointers in C

    11/12

  • 7/30/2019 Structures, Headers, Arrays, Functions, Strings, Pointers in C

    12/12

    output