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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <assert.h>
#include "clog.h"

struct City {  // Type name is `struct City`
    int longitude;  // `longitude` is a field within a struct type called `struct City`.
    int latitude;   // `latitude`  "  " "     "      " "      "    "       "      "
    char* name;     // `name`      "  " "     "      " "      "    "       "      "
    char* reason;   // `reason`    "  " "     "      " "      "    "       "      "
}  // OOPS!!! Forgot the semicolon.   (Never do this!)

struct City get_a_hometown(bool is_remote) {
// Return type of this function is `struct City`.
    if(is_remote) {
        struct City hometown = { .longitude = 71,
                                 .latitude  = 42,
                                 .name      = "Boston",
                                 .reason    = "it has a rich history"};
        return hometown;
    }
    else {
        struct City hometown = { .longitude = 77,
                                 .latitude  = 39,
                                 .name      = "Washington DC",
                                 .reason    = "it is diverse" };
        return hometown;
    }
}

int main(int argc, char* argv[]) {
    struct City hometown = get_a_hometown(true);
    printf("REMOTE: My favorite city is %s (at %d°/%d°) because %s.\n\n",
            hometown.name,
            hometown.longitude,
            hometown.latitude,
            hometown.reason);

    hometown = get_a_hometown(false);
    printf("IN PERSON: My favorite city is %s (at %d°/%d°) because %s.\n\n",
            hometown.name,
            hometown.longitude,
            hometown.latitude,
            hometown.reason);

    return EXIT_SUCCESS;
}

/*
struct_05_forget_semicolon_in_type_definition.c:14:1: error: expected ‘;’, identifier or ‘(’ before ‘struct’
 struct City get_a_hometown(bool is_remote) {
 ^~~~~~
 */
/* vim: set tabstop=4 shiftwidth=4 fileencoding=utf-8 noexpandtab: */

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