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>
// Concise syntax (popular)
// Syntax #3 of 3
typedef struct {
int x;
int y;
char* name;
} Point; // <<<<< DO NOT FORGET THE SEMICOLON <<<<<
// typedef struct { «fields…» } «new_type_name»
// └> creates a struct type called Point
Point make_point(int x, int y, char* name) {
Point new_point = { .x = x, .y = y, .name = name }; // <-- named initializer
return new_point;
}
void print_point(Point p) {
printf("%s: .x == %d .y == %d\n", p.name, p.x, p.y);
}
int main(int argc, char* argv[]) {
Point p1 = make_point(5, 7, "here");
print_point(p1);
Point p2 = make_point(6, 8, "there");
print_point(p2);
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.