From 05b2e3b8776f2709d3af6337c49de8301ce1ea11 Mon Sep 17 00:00:00 2001 From: talksik Date: Sat, 26 Sep 2026 09:09:01 -0700 Subject: [PATCH] play with structs and pointers --- ch6/main.c | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 ch6/main.c diff --git a/ch6/main.c b/ch6/main.c new file mode 100644 index 0000000..b2e2935 --- /dev/null +++ b/ch6/main.c @@ -0,0 +1,29 @@ +#include + +typedef int Length; + +#define ARRAY_SIZE(x) (sizeof(x) / sizeof(x[0])) + +void test() { + // struct point p1; // COMPILER ERROR: unknown storage size because struct not declared in this scope + struct point *p1; // works because it's a forward declaration for an opaque pointer, and incomplete type, but cannot do pointer arithmetic + + printf("struct pointer size: %d\n", sizeof(struct point *)); + printf("int * size: %d\n", sizeof(int *)); + printf("char * size: %d\n", sizeof(char *)); +} + +int main(int argc, char *argv[]) { + // you can declare the struct template along with a declaration and definition of a variable, + // and then you can still reuse the struct template again later, however, probably not in another function or file scope + struct point { + int x; + int y; + } p = {100, 100}; + + struct point another_point = {0, 3}; + + printf("struct size: %d\n", sizeof(struct point)); + + test(); +}