8 ms·
Fortunately, 18 years ago, I began learning C from the book The C Programming Language, 2nd ed. written by Brian Kernighan and Dennis Ritchie. The subject of p
by susam 6y ago
Fortunately, 18 years ago, I began learning C from the book The C Programming Language, 2nd ed. written by Brian Kernighan and Dennis Ritchie.
The subject of pointers was generally believed to be scary among fellow students and many of them bought pretty fat books that were dedicated solely to the topic of pointers.
However, when I reached Chapter 5 of the The C Programming Language (K&R), I found that it did a wonderful job at teaching pointers in merely 31 pages. The chapter opens with this sentence:
> A pointer is a variable that contains the address of a variable.
The exact point at which the whole topic of pointers became crystal clear was when I encountered this sentence in § 5.3 Pointers and Arrays:
> Rather more surprising, at first sight, is the fact that a reference to a[i] can also be written as * (a+i).
Indeed, it was easy to confirm that by compiling and running the following program:
#include <stdio.h>
int main() {
int a[] = {2, 3, 5, 7, 11};
printf("%d\n", *(a + 2));
printf("%d\n", a[2]);
printf("%d\n", 2[a]);
return 0;
}
The output is:
5
5
5
C was the first serious programming language I was learning back then and at that time, I don't think I could have come across a better book than K&R to learn this subject. Like many others, I too feel that this book is a model for technical writing. I wish more technical books were written like this with clear presentation and concise treatment.
- vanviegen 6y agoIt is a good book. But to be fair, not many modern languages are as simple (low number of concepts) as C.
- Yajirobe 6y ago> A pointer is a variable that contains the address of a variable. Doesn't it technically also contain its own address? So it contains the address of another variable and the address of itself. Pointers are hard...
- nnq 6y ago> Doesn't it technically also contain its own address Huh? Then you probably don't understand the meaning of "contains" here. Or what variables are in general. Variables are hard...
- Yajirobe 6y agoWell, the way I see it is this: you, as a person, contain your thoughts and possessions and so on - call it your value. But you also exist in space and time, you have a defined location. You don't carry it in your pocket, it's something you implicitly contain.
- deleted 6y ago[deleted]
- jstimpfle 6y agoA variable "contains" only its (current) value. That's the only use of "contains" that makes sense to me. You can "get" the address of a variable (or more generally, of any l-value) with the & operator. That operator is nothing else but an "escape" character that says "Dear C, I don't want you to load the current value of that variable. Give me just the address where it lives so I can load it later". (Same applies to storing instead of loading)
- pps43 6y agoNo, that would be a pointer to a pointer.
- phaemon 6y agoNope. And they're not really. Imagine a simple computer with, say, 32 bytes of ram. All our bytes of ram are numbered. We read a value from byte number 8 and that value is 17. "Byte number 8" is a pain to remember or calculate so we give it a label and let the computer keep track. That's a "variable". What does that 17 mean though? Is it a value we want to use later on? It might be. Or it could be referring to Byte 17 in our ram. Byte 17 happens to have the value 42 so if we use Byte 8 as a pointer that's the value we end up with. But it's entirely our choice. It's just a number and we can treat it as a value or a pointer to another memory address as we like.
- eru 6y ago> A pointer is a variable that contains the address of a variable. Given the way the C-standard is relatively carefully written, that explanation is a useful lie, but not necessarily true in every implementation of C.
- enriquto 6y agoSure. When you study physics you first learn Newtonian gravitation; and later, once you really master it, you learn that it is actually false and proceed to learn general relativity. In a similar way, it is very useful for people who learn C to start with the idea that "a pointer is an int". Then, they can discover that pointers are not really ints, but not before they have mastered working with pointers as if they were ints.
- jhardy54 6y agoCould you explain (or link to a good resource about) what pointers really are? I'm surprised that they aren't just ints that point to the address of a variable.
- enriquto 6y agoIn all modern implementations that I know of, pointers are effectively ints. But the formal definition of the C language takes great care in not saying that, allowing for implementation to chose how to implement pointers. In some older systems, for example in 16-bit MS-DOS with "high memory", pointers were not really ints, but pairs of ints (called the segment and the offset, if I recall correctly) whose mapping to memory positions was overlapping, i.e., non unique, you could have very different representations for the same memory position. I guess the gcc port to MSDOS by DJ Delorie had to deal with these issues somehow. But it is possible to imagine completely exotic CPU architectures, where there is a separate type of register that can only be used for pointers and it is impossible to convert to int; you could still write C compilers for that architecture and be 100% conforming to the standard.
- 6y ago
- reader_1000 6y ago>> A pointer is a variable that contains the address of a variable. Well, it is a little bit incomplete I think. A pointer contains both the address of a variable and a type. That is why pointer arithmetic works. Without type of pointer, p++ is not calculatable.
- csboyer 6y agoThe pointer variable does not contain the type - it only stores the address. Sizeof will return whatever the machine address width is. The type is implicitly stored as the pointers type. Casting a pointer can change a piece of memory to be interpreted as any type you want. Although the cast might not make logical sense or may even caused undefined behavior in C. Pointer arithmetic works off of whatever the current type is, as the compiler needs to know how to the memory offsets.
- reader_1000 6y agoI actually meant that pointer type is also important to understand the concept. I may not have stated it well. Yes, it does not store the type of the variable it points to but type of the pointer matters and it is significant because many operations depends on it.
- cassepipe 6y agoC beginner here. I think you stated it well : "and a type" The post you responded to on the contrary misses your point that in memory a pointer is a variable that stores both an address and type information to what it points to.
- js2 6y agoNo, it does not. The compiler knows the pointer type so that it can generate correct assembly, but the pointer type is not something stored in memory past the compilation phase.
- disown 6y agoK&R is easily one of the best CS/programming books. Short, concise and to the point. Another way to learn pointers is via assembly. Either learn assembly or just look at the disassembled code. Look under the hood and it'll become clearer. Of course the best way is to write your own C compiler... but that may be overkill for most.
- microtherion 6y ago> Rather more surprising, at first sight, is the fact that a reference to a[i] can also be written as * (a+i) I did not find that all that surprising, but the first time I saw that a[i] can also be written as i[a], I was very irritated.
- diehunde 6y agoI don't know. I read the pointers chapter at least 3 times, and I still get confused when I see pointer stuff in real code. The other day, I read the famous regex post by Russ Cox[1], and it took me a couple of hours to understand the pointer parts of the code even though the algorithm behind it was relatively simple. I guess pointers are just one of those things that for some people make a lot of sense, whereas others struggle with. [1] https://swtch.com/~rsc/regexp/regexp1.html https://swtch.com/~rsc/regexp/regexp1.html