O'Reilly logo

Learn C the Hard Way: A Clear & Direct Introduction To Modern C Programming by Zed A. Shaw

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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        ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required