- Learn C The Hard Way
- Preface
- Introduction: The Cartesian Dream Of C
- Exercise 0: The Setup
- Exercise 1: Dust Off That Compiler
- Exercise 2: Make Is Your Python Now
- Exercise 3: Formatted Printing
- Exercise 4: Introducing Valgrind
- Exercise 5: The Structure Of A C Program
- Exercise 6: Types Of Variables
- Exercise 7: More Variables, Some Math
- Exercise 8: Sizes And Arrays
- Exercise 9: Arrays And Strings
- Exercise 10: Arrays Of Strings, Looping
- Exercise 11: While-Loop And Boolean Expressions
- Exercise 12: If, Else-If, Else
- Exercise 13: Switch Statement
- Exercise 14: Writing And Using Functions
- Exercise 15: Pointers Dreaded Pointers
- Exercise 16: Structs And Pointers To Them
- Exercise 17: Heap And Stack Memory Allocation
- Exercise 18: Pointers To Functions
- Exercise 19: A Simple Object System
- Exercise 20: Zed's Awesome Debug Macros
- Exercise 21: Advanced Data Types And Flow Control
- Exercise 22: The Stack, Scope, And Globals
- Exercise 23: Meet Duff's Device
- Exercise 24: Input, Output, Files
- Exercise 25: Variable Argument Functions
- Exercise 26: Write A First Real Program
- Exercise 27: Creative And Defensive Programming
- Exercise 28: Intermediate Makefiles
- Exercise 29: Libraries And Linking
- Exercise 30: Automated Testing
- Exercise 31: Debugging Code
- Exercise 32: Double Linked Lists
- Exercise 33: Linked List Algorithms
- Exercise 34: Dynamic Array
- Exercise 35: Sorting And Searching
- Exercise 36: Safer Strings
- Exercise 37: Hashmaps
- Exercise 38: Hashmap Algorithms
- Exercise 39: String Algorithms
- Exercise 40: Binary Search Trees
- Exercise 41: Using Cachegrind And Callgrind For Performance Tuning
- Exercise 42: Stacks and Queues
- Exercise 43: A Simple Statistics Engine
- Exercise 44: Ring Buffer
- Exercise 45: A Simple TCP/IP Client
- Exercise 46: Ternary Search Tree
- Exercise 47: A Fast URL Router
- Exercise 48: A Tiny Virtual Machine Part 1
- Exercise 48: A Tiny Virtual Machine Part 2
- Exercise 50: A Tiny Virtual Machine Part 3
- Exercise 51: A Tiny Virtual Machine Part 4
- Exercise 52: A Tiny Virtual Machine Part 5
- Next Steps
- Deconstructing K & RC Is Dead
Exercise 16: Structs And Pointers To Them
In this exercise you'll learn how to make a struct
, point a pointer at them, and use them to make sense of internal memory structures. I'll also apply the knowledge of pointers from the last exercise and 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:
#include <stdio.h>
#include <assert.h>
#include <stdlib.h>
#include <string.h>
struct Person {
char *name;
int age;
int height;
int weight;
};
struct Person *Person_create(char *name, int age, int height, int weight)
{
struct Person *who = malloc(sizeof(struct Person));
assert(who != NULL);
who->name = strdup(name);
who->age = age;
who->height = height;
who->weight = weight;
return who;
}
void Person_destroy(struct Person *who)
{
assert(who != NULL);
free(who->name);
free(who);
}
void Person_print(struct Person *who)
{
printf("Name: %s\n", who->name);
printf("\tAge: %d\n", who->age);
printf("\tHeight: %d\n", who->height);
printf("\tWeight: %d\n", who->weight);
}
int main(int argc, char *argv[])
{
// make two people structures
struct Person *joe = Person_create(
"Joe Alex", 32, 64, 140);
struct Person *frank = Person_create(
"Frank Blank", 20, 72, 180);
// print them out and where they are in memory
printf("Joe is at memory location %p:\n", joe);
Person_print(joe);
printf("Frank is at memory location %p:\n", frank);
Person_print(frank);
// make everyone age 20 years and print them again
joe->age += 20;
joe->height -= 2;
joe->weight += 40;
Person_print(joe);
frank->age += 20;
frank->weight += 20;
Person_print(frank);
// destroy them both so we clean up
Person_destroy(joe);
Person_destroy(frank);
return 0;
}
To describe this program, I'm going to use a different approach than before. I'm not going to give you a line-by-line breakdown of the program, but I'm going to make you write it. I'm going to give you a guide through the program based on the parts it contains, and your job is to write out what each line does.
includes
I include some new header files here to gain access to some new functions. What does each give you?
struct Person
This is where I'm creating a structure that has 4 elements to describe a person. The final result is a new compound type that lets me reference these elements all as one, or each piece by name. It's similar to a row of a database table or a class in an OOP language.
function Person_create
I need a way to create these structures so I've made a function to do that. Here's the important things this function is doing:
- I use
malloc
for "memory allocate" to ask the OS to give me a piece of raw memory. - I pass to
malloc
thesizeof(struct Person)
which calculates the total size of the struct, given all the fields inside it. - I use
assert
to make sure that I have a valid piece of memory back from malloc. There's a special constant calledNULL
that you use to mean "unset or invalid pointer". Thisassert
is basically checking that malloc didn't return a NULL invalid pointer. - I initialize each field of
struct Person
using thex->y
syntax, to say what part of the struct I want to set. - I use the
strdup
function to duplicate the string for the name, just to make sure that this structure actually owns it. Thestrdup
actually is likemalloc
and it also copies the original string into the memory it creates.
function Person_destroy
If I have a create, then I always need a destroy function, and this is what destroys Person
structs. I again use assert
to make sure I'm not getting bad input. Then I use the function free
to return the memory I got with malloc
and strdup
. If you don't do this you get a "memory leak".
function Person_print
I then need a way to print out people, which is all this function does. It uses the same x->y
syntax to get the field from the struct to print it.
function main
In the main function I use all the previous functions and the struct Person
to do the following:
- Create two people,
joe
andfrank
. - Print them out, but notice I'm using the
%p
format so you can see where the program has actually put your struct in memory. - Age both of them by 20 years, with changes to their body too.
- Print each one after aging them.
- Finally destroy the structures so we can clean up correctly.
Go through this description carefully, and do the following:
- Look up every function and header file you don't know about. Remember that you can usually do
man 2 function
orman 3 function
and it'll tell you about it. You can also search online for the information. - Write a comment above each and every single line saying what the line does in English.
- Trace through each function call and variable so you know where it comes from in the program.
- Look up any symbols you don't know as well.
What You Should See
After you augment the program with your description comments, make sure it really runs and produces this output:
$ make ex16
cc -Wall -g ex16.c -o ex16
$ ./ex16
Joe is at memory location 0xeba010:
Name: Joe Alex
Age: 32
Height: 64
Weight: 140
Frank is at memory location 0xeba050:
Name: Frank Blank
Age: 20
Height: 72
Weight: 180
Name: Joe Alex
Age: 52
Height: 62
Weight: 180
Name: Frank Blank
Age: 40
Height: 72
Weight: 200
Explaining Structures
If you've done the work I asked you then structures should be making sense, but let me explain them explicitly just to make sure you've understood it.
A structure in C is a collection of other data types (variables) that are stored in one block of memory but let you access each variable independently by name. They are similar to a record in a database table, or a very simplistic class in an object oriented language. We can break one down this way:
- In the above code, you make a
struct
that has the fields you'd expect for a person: name, age, weight, height. - Each of those fields has a type, like
int
. - C then packs those together so they can all be contained in one single
struct
. - The
struct Person
is now a compound data type , which means you can now refer tostruct Person
in the same kinds of expressions you would other data types. - This lets you pass the whole cohesive grouping to other functions, as you did with
Person_print
. - You can then access the individual parts of a
struct
by their names usingx->y
if you're dealing with a pointer. - There's also a way to make a struct that doesn't need a pointer, and you use the
x.y
(period) syntax to work with it. You'll do this in the Extra Credit.
If you didn't have struct
you'd need to figure out the size, packing, and location of pieces of memory with contents like this. In fact, in most early assembler code (and even some now) this is what you do. With C you can let C handle the memory structuring of these compound data types and then focus on what you do with them.
How To Break It
With this program the ways to break it involve how you use the pointers and the malloc
system:
- Try passing
NULL
toPerson_destroy
to see what it does. If it doesn't abort then you must not have the-g
option in your Makefile'sCFLAGS
. - Forget to call
Person_destroy
at the end, then run it underValgrind
to see it report that you forgot to free the memory. Figure out the options you need to pass toValgrind
to get it to print how you leaked this memory. - Forget to free
who->name
inPerson_destroy
and compare the output. Again, use the right options to see howValgrind
tells you exactly where you messed up. - This time, pass
NULL
toPerson_print
and see whatValgrind
thinks of that. - You should be figuring out that
NULL
is a quick way to crash your program.
Extra Credit
In this exercise I want you to attempt something difficult for the extra credit: Convert this program to not use pointers and malloc
. This will be hard, so you'll want to research the following:
- How to create a
struct
on the stack , which means just like you've been making any other variable. - How to initialize it using the
x.y
(period) character instead of thex->y
syntax. - How to pass a structure to other functions without using a pointer.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论