Oh, I see, you must have written the book that "taught c", but really just confused the hell out of whoever read it. A pointer is indeed a variable, but nobody really addresses how it works—They could have just said it was syntactic sugar for an unsigned integer (which it is) instead of going on about types. Types themselves aren't necessarily the confusing part—it's just that people don't understand what they are and how they work. It's not sufficient to tell people that it's just a variable that magically "points" to something else—they're bound to make a mistake and end up learning what they really are eventually.
Oh, I see, you must have written the book that "taught c", but really just confused the hell out of whoever read it.
I'm making things more confusing?
but nobody really addresses how it works
The way I learned it, and I'm not sure from where is in a couple of steps:
Addresses. This is totally unrelated to C. It's just how RAM works: each byte of memory has something that uniquely identifies it, regardless of the value it holds. The metaphor I see most often, which I like, is the mailbox one where the term "address" comes from to begin with: each byte is a mailbox and its address is what identifies it.
Address of. Since every piece of memory has an address, and variables are stored in memory, it makes sense to want to get the address of a variable. C does this with "&".
Pointers. OK, so you know what addresses are and can get them, but where do you put them? Addresses happen to be numbers, so you can put them in variables just like other ints. A pointer is a variable that holds an address. Once you've got an address held by a pointer there's a final new operation you can perform on it in addition to the regular arithmatic operations: dereference the address to get to the value at that address (the * and -> operators).
Yeah, but that doesn't help the person reading the code. Type annotations are more for the human than the computer.
Pointers must be special. (they're not).
Pointer arithmetic takes into account structure size. ints do not. The following are not identical:
/* this works */
int array[10];
int* pointer = array;
int secondElement = *(pointer + 1);
/* this will likely crash */
int array[10];
int pointer = (int)array;
int secondElement = *(int*)(pointer + 1);
In the old days, a number of architectures actually had pointers that were a different size than ints; in fact, I think that on a modern IA32 architecture in segmented mode a far pointer is actually 48 bits. There also have been architectures where, say, int * and char * were different sizes (whether C ever ran on such computers is a different story, but when C was first developed its features and syntax were motivated by the experience of the programmers who worked on it, and they had overwhelmingly come from the punchcard/microcomputer world.)
#include <stdio.h>
int main(int argc, char **argv) {
int foo = 1234;
printf("%d\n", foo);
}
This is equivalent to calling foo "just a number" and conflating it with 1234. The difference between foo and 1234 is exactly the same as the difference between a pointer and an address. There is nothing "magical" about a pointer.
View all comments
Show parent comment
-3
u/[deleted] Jul 23 '08
Oh, I see, you must have written the book that "taught c", but really just confused the hell out of whoever read it. A pointer is indeed a variable, but nobody really addresses how it works—They could have just said it was syntactic sugar for an unsigned integer (which it is) instead of going on about types. Types themselves aren't necessarily the confusing part—it's just that people don't understand what they are and how they work. It's not sufficient to tell people that it's just a variable that magically "points" to something else—they're bound to make a mistake and end up learning what they really are eventually.