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

8

u/[deleted] Jul 23 '08 edited Jul 23 '08

C at the age of twelve; I struggled with the concept of pointers (I didn't understand the abstraction; I still think it's stupid to call it a pointer rather than an address.) Then I moved to ruby, and now I'm on python and c with dabblings in other languages (I get payed to write perl now.)

6

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

Pointers were a big challenge for me too. There's still stuff about C that trips me up (pointers to arrays inside structures, etc.).

I still think it's stupid to call it a pointer rather than an address.

A pointer is a variable. An address is the value that variable holds. The difference between pointers and addresses is the difference between "int" and "1234".

You can think of addresses like mailbox addresses. It identifies a place in memory. A pointer is like a postcard with an address written on it. With the postcard, you can send stuff to that address.

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.

→ More replies (16)
→ 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 *.

-3

u/Gotebe Jul 23 '08

The difference between pointers and addresses is the difference between "int" and "1234".

Nah. People don't go around saying "this pointer contains address X", just like they don't say "this int contains number Y". Both just have a value. Address == pointer if you ask me. You reached for artificial superficial distinction ;-)

6

u/munificent Jul 23 '08

People don't go around saying "this pointer contains address X"

As far as I know, they say "it points to X".

Address == pointer if you ask me.

No, that's definitely not the case:

  1. You can have a pointer to a pointer. You cannot take the address of an address anymore than you can take the address of 1234.
  2. You can have three pointers point to the same memory. You definitely cannot have three addresses for the same memory.

3

u/wildeye Jul 23 '08

It's not stupid to call it a pointer rather than an address. Pointers are addresses, in one sense, as you've said, but they are more abstract.

Consider pointer arithmetic. Given "short *t", "t+1" will add 2 to the address in t. Non-abstract pointers, as found e.g. in assembly language, don't have any such extra abstract features.

I believe also that historically, when the notion of "pointer" was invented in the 1960s (before C, note), they were considered an abstract notion, like an arrow drawn on the white board between two boxes representing data structures, and the question of how they were implemented was very much secondary.

So in some non-C language in the 1960s, a pointer might actually be, say, an index into an array, or some kind of complicated record structure on a Burroughs mainframe.

It's only since the 1970s in C and C-influenced/derived languages that things settled down to "pointers are addresses (plus some abstraction)."

And even at that, they have in some languages continued to be non-addresses, although as often as not people then change the name ("handles" in the Windows line of operating systems traditionally were in fact almost-but-not-quite addresses [they were incremented to be illegal addresses], and Java swears it has no pointers -- but it does; its "references" are in fact pointers/addresses, and there is a common event called "null pointer exception").

Anyway...

1

u/808140 Jul 24 '08

Given "short *t", "t+1" will add 2 to the address in t.

Don't count on it.

1

u/wildeye Jul 24 '08 edited Jul 24 '08

I was giving an example of how pointers are mildly more abstract than raw machine addresses. As an illustrative example of that, I believe that it is a reasonable thing to say, and that any counter-example you have in mind that may appear in the C standards document will be something that only occurs in some rare circumstance or rare architecture.

I'm assuming you are not talking about machines that don't have byte-pointers in the first place, where adding 1 increments e.g. by 32 bits, because that would be tiresome and tedious indeed. The point is about the abstraction of pointers, and to be more strictly correct, yes, I'd have to switch to an example using an array of structs, but WTF, I'm not getting paid for this, now am I?

In short, I think you are nitpicking for no reasonable reason, given the context.

The fact that you didn't bother to provide the counterexample that you claim to have in mind confirms this.

1

u/808140 Jul 24 '08 edited Jul 24 '08

Whoa, don't get your panties in a bunch there sweetheart. I was just pointing out that you can't count on the sizeof(short) being 2, that's all. Nothing whatsoever to do with pointers or how you understand them to function. The short type is guaranteed to hold at least 16 bits but may hold more, and many compilers will store a short in 4 bytes rather than two when optimizing, since they're aligning them on a 4-byte boundary anyway.

1

u/wildeye Jul 25 '08

I appreciate your clarification; my tone arose out of "dying the death of a thousand cuts" from various other recent reddit nitpicks lacking in any explanation, with yours originally another such added atop the pile.

(Doubtless all of us have had the experience of feeling attacked suddenly from all sides, at one time or another; it's uncomfortable.)

If you had originally said "Don't count on it...you can't count on sizeof short being 2[...etc...]" and went on with your explanation, then I would've understood that you were simply being helpful, even though it was simply an aside from what had previously been the central point.

I would encourage you to be a bit more explanatory and a bit less terse in the future, since it turns out you did have helpful clarifications in mind from the beginning.

Thanks.

3

u/HaMMeReD Jul 24 '08

It's called a pointer because it is a variable that point's to an address, if you called it a address, it would be confused with an actual address that being the location in memory. It would become difficult to explain things.

"Address Reference" is a good name for it if you want to be verbose, but it's easier to just call it a pointer.

4

u/oilytheotter Jul 23 '08

Pointers stopped me dead in my tracks when I was trying to learn C++ when I was about 14. I didn't fully understand them until my first programming class in college when I was 17.

1

u/[deleted] Jul 23 '08

As you can see from the replies to this thread, nobody seems to be exactly clear on what pointers are ;-) (Though most of the discussion is about mere terminology; a concept which sometimes seems to cause 99% of all communication problems).