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 | #include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <assert.h>
#include "log_macros.h"
// struct type with CANNONICAL syntax (aka "Basic syntax")
//
// Initialize fields inidividually.
// Reminder: Do not do this unless there is no other choice. Use named initializer.
// Define a struct type called 'struct Point' (2 words) containing two int fields.
struct Point {
int x; // field
int y; // field
char* name; // field
};
// Defining a type does not allocate any memory or create any variable.
void print_point(struct Point point) {
log_int(point.x);
log_int(point.y);
log_str(point.name);
}
int main(int argc, char* argv[]) {
// Create Point object and initialize with the field values .x=5 and .y=7.
// struct Point p = { .x = 5, .y = 7, .name = "Lou" };
// // This syntax is called a named initializer. Use this wherever possible.
// Do not do this.
struct Point p;
p.x = 5;
p.y = 7;
// p.name = "Lou"; // OOPS!!!
// Print the Point object
print_point(p);
return EXIT_SUCCESS;
}
/* vim: set tabstop=4 shiftwidth=4 fileencoding=utf-8 noexpandtab: */
|
© Copyright 2022 Alexander J. Quinn This content is protected and may not be shared, uploaded, or distributed.