Exercise 16. Structs And Pointers to Them

In this exercise, you’ll learn how to make a struct, point a pointer at it, and use it to make sense of internal memory structures. We’ll also apply the knowledge of pointers from the last exercise, and then get you constructing these structures from raw memory using malloc.

As usual, here’s the program we’ll talk about, so type it in and make it work.

ex16.c

 1   #include <stdio.h>  2   #include <assert.h>  3   #include <stdlib.h>  4   #include <string.h>  5  6   struct Person {  7       char *name;  8       int age;  9       int height; 10       int weight; 11   }; 12 13   struct Person *Person_create(char *name, int age, int height, 14           int weight) 15   { 16        ...

Get Learn C the Hard Way: A Clear & Direct Introduction To Modern C Programming now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.