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

typedef struct {
    int x;
    int y;
} Point;

#define log_point(p) printf("(%s.x,%s.y) == (%d,%d)\n", #p, #p, (p).x, (p).y)

void multiply_point(Point* a_p, int multiplier) {
    a_p->x *= multiplier;
    a_p->y *= multiplier;
}

int main(int argc, char* argv[]) {
    Point p1 = { .x = 5, .y = 7 };
    log_point(p1);

    Point* a_p1 = &p1;
    log_point(*a_p1);
    // kinda gross syntax, need parentheiss for order of operations
    (*a_p1).x = 6;
    // nicer syntax with arrow operator
    a_p1->y = 8;
    log_point(*a_p1);
    
    printf("%d\n", a_p1->x);

    Point p2 = { .x = 1, .y = 2 };
    log_point(p2);
    p2 = (Point){ .x = 8, .y = 5};
    log_point(p2);

    // assignment requires both sides to be the same
    *a_p1 = (Point){.x = 1, .y = 3};    
    log_point(*a_p1);
    
    *a_p1 = p2;
    log_point(*a_p1);
    log_point(p1);

    multiply_point(&p2, 10);
    log_point(p2);

    return EXIT_SUCCESS;
}
/* vim: set tabstop=4 shiftwidth=4 fileencoding=utf-8 noexpandtab: */

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