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
#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

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
    };

    printf("item name:     %s\n",    best.name);
    printf("item price:    %.02f\n", best.price);
    printf("item quantity: %d\n",    best.quantity);

    best.price = 3.0;

    return 0;
}

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