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
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <assert.h>

// Cannonical syntax

// Define a struct type called 'struct Point'.
struct Point {
    int x;
    int y;
};  // <<<<< DO NOT FORGET THE SEMICOLON <<<<<

// Function that returns a 'struct Point' object
struct Point make_point(int x, int y) {
    struct Point new_point = { .x = x, .y = y };
    return new_point;
}

int main(int argc, char* argv[]) {
    
    // Declare an instance of 'struct Point' (aka "a struct Point object") and
    // initialize its fields.
    struct Point p1 = make_point(5, 7);

    printf("p1.x == %d   p1.y == %d\n", p1.x, p1.y);

    struct Point p2 = p1;  // create a copy of the entire struct object 'p'.
    p2.x += 100;
    p2.y += 500;
    printf("p2.x == %d   p2.y == %d\n", p2.x, p2.y);
    // You can copy a struct object all at once with a simple assignment, but not an array.
    
    return EXIT_SUCCESS;
}
/* vim: set tabstop=4 shiftwidth=4 fileencoding=utf-8 noexpandtab: */

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