1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <stdio.h>

struct Item {  // create a new type (e.g., int, double, char) called "struct Item"
    char* name;      // <-- just like variable declarations
    float price;     // This struct type has 3 fields (aka attributes)
    int   quantity;
};  // SEMICOLON     // <-- REMEMBER YOUR SEMICOLON

void print_item(struct Item* item) {   // "item" is an instance of "struct Item"
    printf("item name:     %s\n",    item -> name);
    printf("item price:    %.02f\n", item -> price);
    printf("item quantity: %d\n",    item -> quantity);
}

int main(int argc, const char *argv[])
{
    // declare and initialize a "struct object" (aka "object")
    struct Item best = {
        .name     = "apples",
        .price    = 12.00,
        .quantity = 10
    };
    print_item(&best);
    best.price = 3.0;
    print_item(&best);

    return 0;
}

© Copyright 2016 Alexander J. Quinn         This content is protected and may not be shared, uploaded, or distributed.