r/programming • • Jul 23 '08

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

12 Upvotes

188 comments sorted by

View all comments

Show parent comment

1

u/[deleted] Jul 23 '08

no, the pointer literally is the address. That's what confused me; I thought that a pointer was a built in data structure or something. It's not. It's just another integer (uintptr_t, in fact).

2

u/yesimahuman Jul 23 '08

We could all add to the detail war going on...but if you understand what a pointer is, whatever way you think about them helps you write the best code with them should be fine. Unless you're writing bad code with your idea of a pointer...

-1

u/[deleted] Jul 23 '08

Excellently worded. Anyway, how can I write bad code with it? I suppose I could use an integer as an intermediary holder...

3

u/munificent Jul 23 '08

The reason I'm pedantic about this is that pointers are notoriously hard in C, especially for new programmers. It took me a long time when I was a kid to really get it and I still run into cases where C trips me up.

The least we can do for the next generation is be clear about the terminology. It's really hard to teach people the underlying mechanics when the words we have to use are themselves fuzzy.

3

u/sad_panda Jul 23 '08

pointers are notoriously hard in C, especially for new programmers

Learning Assembly first solves this neatly.

1

u/munificent Jul 24 '08

Hmm, maybe. But to me that sounds a lot like:

"Learning to kill a man with your bare hands is hard."

"Learning to kill a man with your pinky first solves this neatly."

1

u/sad_panda Jul 24 '08

Assembly is simpler.

1

u/ohxten Jul 24 '08

Very, very true. I understood C quite a bit better after learning assembly. I don't code often enough in assembly to be fluent in it, but I use it for a few things and it's really, really useful. My favorite language. :)

Anyway, learned VB.NET at 12; I wrote a program to automatically take screenshots and upload them to FTP. Funny thing is, the program would create a batch file which told the Windows FTP program to upload the screenshot. How hackish is that? Anyway, it was buggy (my first real program) but I recently rewrote it in C (free plug: AutoScreen). Of course, no more dynamically generated batch files. :D

When programming in VB I didn't really get programming. Then I learned C a year later and then my programming really took off. You can pretty much do anything, on any OS (during this time I dumped Windows and moved to *nix).

I learned x86 assembly maybe a year after that and it helped me know a lot more about how C worked behind the scenes. Plus, it's useful in a lot of situations, especially where extreme speed is crucial. Simple and clean, yet complex in it's simplicity. I think a lot of software these days could use some good ol' assembly optimizations. :)

I'm a big fan of older technologies and not much a big fan of new-fangled languages. I have a tremendous amount of respect for the older programming generation who wrote most of their code in assembly/C, etc. They had to make their software small and fast, and that's what I strive to do when I write software.

5

u/munificent Jul 23 '08 edited Jul 23 '08

no, the pointer literally is the address.

Whoa whoa whoa. I think you are still confused. A pointer is not an address. A pointer has an address, and a pointer points to or contains an address, but a pointer is not itself an address.

By example:

int foo = 4;         /* some random int */
int* pFoo;           /* this var is a pointer */

pFoo = &foo;         /* the value of pFoo is now the address in memory where the value of foo is stored */

printf("%d", &pFoo); /* print its address (who knows what) */
printf("%d", pFoo);  /* print the address it contains (the address of foo) */
printf("%d", *pFoo); /* print the value at the address being pointed to (foo's value, 4) */

I thought that a pointer was a built in data structure or something. It's not.

It's not a structure, but it is a built in data type. (OK, it's a class of types, but the idea still holds.) A pointer is a data type that holds an address. (More specifically, a pointer of a given type is a data type that holds the address of an value of the given data type.)

It's just another integer (uintptr_t, in fact).

You're confusing the storage requirements of a variable with it's type. int, void* and char[4] are all stored in 32 consecutive bits (on a 32-bit system, etc.). They are not the same type. Remember, on a computer everything is stored as ints, so saying "it's just an int" is meaningless.

As a statically typed language, C lets you specify types more precisely than simply the storage requirements so that the compiler can catch errors for you. For example:

/* declare two types with the same storage */
typedef foo int;
typedef bar int;

void fn(foo x) { /* do something */ }

void call_fn()
{
    bar b;
    fn(b); /* compile error! wrong arg type */
}

7

u/twanvl Jul 23 '08

First of all, you have the typedefs the wrong way around. The old type comes first,

typedef foo int;
typedef bar int;

Secondly, in C(++) typedef creates an alias, not a new type. So both foo and bar would be ''exactly the same'' as integers, and the compiler will not report an error.

→ More replies (4)

-5

u/[deleted] Jul 23 '08

Wrong. A pointer is an address.

#include <stdio.h>

int main(int argc, char **argv) {
  char *foo = "hello, there.";
  printf("0x%X\n", foo);
}

Run it. It prints an address.

5

u/munificent Jul 23 '08

A pointer is an address.

If that's true, then that implies reflexivity. Are you saying an address is a pointer?

It prints an address.

Of course. It prints the value of the pointer, which is an address.

0

u/[deleted] Jul 23 '08

[deleted] — view deleted comment

5

u/munificent Jul 23 '08

Are you saying an address is one kind of pointer then? That makes even less sense.

0

u/[deleted] Jul 23 '08

[deleted] — view deleted comment

4

u/munificent Jul 23 '08 edited Jul 23 '08

You could say that a pointer is one kind of address.

I guess so, but I was being rhetorical. Both statements are wrong. Neither pointers nor addresses are kinds of each other. They're closely related, but not in that way.

Instead, it's exactly the way "int" (the C type) is related to integers.

-2

u/[deleted] Jul 23 '08

If that's true, then that implies reflexivity. Are you saying an address is a pointer?

Yes.

Of course. It prints the value of the pointer, which is an address.

So then it's just a name. If the only attribute a pointer has is the address, then what's the difference? What distinguishes a pointer from an address?

8

u/munificent Jul 23 '08 edited 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 edited 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).

0

u/[deleted] Jul 23 '08

Yes, but you can store addresses in integers, so why use pointers? Pointers must be special. (they're not).

3

u/munificent Jul 23 '08 edited Jul 23 '08

Yes, but you can store addresses in integers

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);
→ More replies

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.

-1

u/[deleted] Jul 24 '08

well, an 'int' is just an integer, the exact same way as a pointer is.

→ More replies (13)

1

u/HaMMeReD Jul 24 '08 edited Jul 24 '08

There are 2 addresses at work.

char * blah = "string";

blah = Address of the "s" in "string"

&blah = Address of the pointer "blah"

So as you can see, the string itself is located in memory, but that pointer to the string data itself is also in memory and you can point to it too if you wanted, because they are 2 seperate memory blocks, one for the pointer to the data (8/16/32/64bits depending on arch) and 1 for the string data itself.

When you are dealing with objects and arrays, they always store the data away from the variable itself, which is always just a reference.

Oh and uintptr_t is just a fancy way of saying void *, well, it's actually a fancy way of saying unsigned int *.