男：...and of course, Mommy never lets me stay out after 6 p.m.
女：Thank heavens my boyfriend variable isn’t in read-only memory.
If you really want to kick butt with C, you need to understand how C handles memory.
The C language gives you a lot more control over how your program uses the computer’s memory. In this chapter, you’ll strip back the covers and see exactly what happens when you read and write variables. You’ll learn how arrays work, how to avoid some nasty memory SNAFUs, and most of all, you’ll see how mastering pointers and memory addressing is key to becoming a kick-ass C programmer.
To best understand pointers, go slowly.
C code includes pointers
Pointers are one of the most fundamental things to understand in the C programming language. So what’s a pointer? A pointer is just the address of a piece of data in memory.
Pointers are used in C for a couple of reasons.
1.Instead of passing around a whole copy of the data, you can just pass a pointer.
2.You might want two pieces of code to work on the same piece of data rather than a separate copy.
Pointers help you do both these things: avoid copies and share data. But if pointers are just addresses, why do some people find them confusing？ Because they’re a form of indirection. If you’re not careful, you can quickly get lost chasing pointers through memory. The trick to learning how to use C pointers is to go slowly.
Don’t try to rush this chapter.
Pointers are a simple idea, but you need to take your time and understand everything. Take frequent breaks, drink plenty of water, and if you really get stuck, take a nice long bath.
get stuck 上当；卡住；被困住
Digging into memory
To understand what pointers are, you’ll need to dig into the memory of the computer.
Every time you declare a variable, the computer creates space for it somewhere in memory. If you declare a variable inside a function like main(), the computer will store it in a section of memory called the stack. If a variable is declared outside any function, it will be stored in the globals section of memory.
The computer might allocate, say, memory location 4,100,000 in the stack for the x variable. If you assign the number 4 to the variable, the computer will store 4 at location 4,100,000.
If you want to find out the memory address of the variable, you can use the & operator:
printf("x is stored at %p\n", &x);
The address of the variable tells you where to find the variable in memory. That’s why an address is also called a pointer, because it points to the variable in memory.