9 ms·
C Pointers Explained, Really (2012)
- greenyoda 12y agoNote that "%x" is not the correct way of displaying the value of a pointer with printf(), since "%x" assumes that it's printing an unsigned int and a pointer may be longer than an int (e.g., on a 64-bit architecture a pointer would be 8 bytes and an int would typically be 4 bytes). The correct format to use to print a pointer in hex is "%p", which knows how long a pointer is on the current system. For more details on printf() formats, see: https://en.wikipedia.org/wiki/Printf_format_string https://en.wikipedia.org/wiki/Printf_format_string
- unwind 12y agoActually %p can only be used to print values of type void * . Since there is no requirement that other pointer types be converted to void * when calling a varargs function, you need to cast: int x = 4711; printf("x is at %p\n", (void *) &x); Of course the conversion will be a null operation on many modern systems, where all data pointer types are implemented identically, but this is what the language standard requires.
- simias 12y agoInteresting, I thought pointer representation could only be different for function pointers. Has there ever been an architecture where data pointers could have a different representations?
- exDM69 12y ago> Interesting, I thought pointer representation could only be different for function pointers. Yes, this is the case, e.g. in micro controllers, where the program is stored in EEPROM. You can store data in program memory (e.g. constant strings) but you need to access it in a special manner. > Has there ever been an architecture where data pointers could have a different representations? At least conceptually, this was the case in early GPUs, where there were different memories for different access patterns. Memory area for constants, another one for global read only memory, and a third area for thread-specific read-write -memory and a fourth that was shared between groups of threads. Whether they were actually different physical memories and whether they had different address spaces is another issue (they at least partially did, some stuff was on chip, some was external DRAM), but this was how it was conceptually seen by the programmer and the way early GPGPU APIs worked (early versions of CUDA and OpenCL).
- nuxi 12y agoYes, different representations as well as non-zero null pointers. More from comp.lang.c FAQ at http://c-faq.com/null/machexamp.html http://c-faq.com/null/machexamp.html.
- simias 12y agoAh, very interesting, thanks!
- kazinator 12y agoIs this really from 2012? Examples are in 1978 "K&R1" C: func(p) int *p; { body ... } No, memory is not one big array, sorry. Objects have boundaries even though there are valid addresses below their base and above their last byte. Not everything that looks sensible from a machine language point of view is well-defined. Comment below article: > Marin Todinov said... > Dear Programming Guru > You are an absolute legend, ive been programming for 4 years and i have a masters in computer science, your explanation of pointers has helped me increase my efficiency in recursive functions and made a map in my breain of how these basic fundamental structers. Lol, what the heck? Troll or astro-turfed? Where can you get a CS Master's Degree and on only four years of programming?
- vt240 12y agoIt says 1990 in the original message.
- Coincoin 12y ago> Lol, what the heck? Troll or astro-turfed? Where can you get a CS Master's Degree and on only four years of programming? ... and still don't totally grasp pointers apparently. But seriously, what's so confusing about pointers?
- gcb0 12y ago> what's so confusing about pointers? as with any constructor, you can make it confusing. just do pointer arithmetic with different types and you get yourself a confusing mess.
- bigiain 12y agoMessage 1956 (8 left): Thu Jan 25 1990 2:44am (that's the first line of the 3rd paragraph, maybe a dozen whole lines before the part you quoted...)
- chrisseaton 12y agoJust for interest: I got my masters in CS in four years, and this is pretty typical in the UK.
- simula67 12y agoWhen used within the context of pointers, it helps to think of '*' as the 'at address' operator and '&' as the 'address of' operator. Especially when reading code.
- jeffreyrogers 12y agoWhen I was learning C I had heard about how terrifying pointers were and so I expected them to be super difficult to learn. Maybe, my experience is atypical, but I found them pretty intuitive.
- lelandbatey 12y agoYep. Once you understand how memory works, it's very straightforward how pointers work (the value of a pointer is actually the address of another "unit" of memory).
- wmt 12y agoI'm not sure why pointers have such bad rep, they're a pretty simple concept of storing and using memory addresses. References in dynamically typed languages however often make me nervous, as I'm never entirely sure when different types in different languages get copied or not. If I the function argument p gets modified here, did the original variable in the caller get modified? Do I have to write an elobarate check for its mutability?
- emcrazyone 12y agoagree and in the article when he gives advise to his friend, he mentions he has a leg up due to his ASM background. I first learned ASM on a Commodore64 and so I had a very close relationship with the hardware such as indexed addressing and indirect index addressing, etc... By the time I learned C some 10 years later, I already new what authors were talking about on the subject of pointers. Over the years, I wondered how I would have learned this stuff if I didn't have the ASM (assembly language background) and a knack for electronics. Now a days, things are more complicated with MMUs and virtual memory concepts but I think the examples in the article are a good basis for anyone wanting to know what happens under the hood.
- coldtea 12y agoTrue. Never had much problem with pointers in my (limited I admit) C experience. But I did have a problem with remembering to manage memory clear.
- cyphax 12y ago
- saosebastiao 12y agoJust came to say I think it is impressive this guy has an email from 1990 backed up.