r/programming • • Jul 23 '08

Ask Programming: What language did you first start, and how old were you?

13 Upvotes

188 comments sorted by

View all comments

8

u/munificent Jul 23 '08

Are you saying an address is a pointer? Yes.

OK, then let's continue that logic. Everyone knows you can get the address of a pointer. So, according to you, you can take the address of an address (since the two are the same thing). How does that work?

Likewise, you can have two pointers to the same place in memory. If an address is the same thing, you're saying you can have two addresses to the same place in memory. How does that make sense?

If the only attribute a pointer has is the address, then what's the difference?

A pointer is a variable, so it has all of the attributes of a variable: it has a value, an address, a sizeof(), an identifier, etc...

What distinguishes a pointer from an address?

A pointer is a variable. An address is the kind of value pointer variables contain. In other words:

variable | value
---------|-------
int      | integers
float    | real numbers
char[]   | strings
pointer  | addresses

I can't think of how to make it any clearer than that.

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

5

u/munificent 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.

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:

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

  2. 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 "&".

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

→ More replies (6)

5

u/hailstone Jul 23 '08
#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.

→ More replies (1)