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 | #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 <<<<<
// A struct Point object will occupy (at least) 8 bytes.
// Notes: A struct TYPE does not exist in memory.
// It is not a variable or object.
// It just creates a type that you can refer to, to tell the compiler how you
// want to organize your data.
// An instance of a struct type will occupy (at least) as much space as the sum
// of the space required for each of the field.
int main(int argc, char* argv[]) {
// Declare an instance of 'struct Point' (aka "a struct Point object") and
// initialize its fields.
struct Point p = { .x = 5, .y = 7 };
printf("p.x == %d p.y == %d\n", p.x, p.y);
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.