Every time someone tries offering a simplified explanation of pointers, I've countered with the old Buddhist saying that, "The pointing finger is not the moon," followed by a brief foray into syntax and operators, e.g., moon* finger = &luna; As often as not, enlightenment occurs.
void* finger = &luna; Don't force the finger to only point to the moon.
The 5-minute Guide to C Pointers
61–70 of 75 posts
Re: The 5-minute Guide to C Pointers
#62Re: The 5-minute Guide to C Pointers
#63Earlier quoted context omitted.
I experienced the same frustration. The exercise that gave me the "ah-ha!" moment I needed was prepending to a linked list via the head instead of the tail: You need to pass the address of `head` into the function so that the new head is reflected in the calling environment when you say `head = np`.
... or have the list operation return the new list head, which can make it way cleaner. See glib's list APIs, for instance.
Re: The 5-minute Guide to C Pointers
#64So... What do you use them for?
There are many uses. It greatly helps to understand the difference between the heap and the stack, and to understand the difference between static and dynamic allocation. Functions in C are "pass by value" rather than "pass by reference". To simulate "pass by reference", the "value" you would pass is the memory address of the data in question, ie the pointer. Also, if you put data on the heap, you must keep a referen…
Re: The 5-minute Guide to C Pointers
#65Earlier quoted context omitted.
There are many uses. It greatly helps to understand the difference between the heap and the stack, and to understand the difference between static and dynamic allocation. Functions in C are "pass by value" rather than "pass by reference". To simulate "pass by reference", the "value" you would pass is the memory address of the data in question, ie the pointer. Also, if you put data on the heap, you must keep a referen…
Correct me if I'm wrong, but I'm pretty sure C doesn't use the 'new' keyword, it uses 'malloc'.
Re: The 5-minute Guide to C Pointers
#66Earlier quoted context omitted.
> On lines 15-16 we assign our void pointer back to our castptr int pointer. Notice the explicit cast needed. The explicit cast is not needed in C.
Correct. This is also why you do not need to cast the result of malloc(). An assignment of a pointer to void to another pointer initiates an implicit conversion.
If you forget to include stdlib.h the cast hides the error and malloc will be assume to be a function which returns int. makes for interesting runtime errors.
Never cast the return value of malloc in C, and don't write redundant code. C is not C++.
Re: The 5-minute Guide to C Pointers
#67thanks, also recommend to read "What do people find difficult about C pointers?" http://stackoverflow.com/questions/4025768/what-do-people-fi...
I never really had much of a problem with the syntax, but the point at which the behavior of pointers really clicked for me (after a night of many segfaults of course) was when I realized that when you pass a pointer to a function, you're sending a copy of that pointer, just like with any other primitive type. E.g: void foo(int *p) { /* assignment won't be persistent after foo() returns; need to send **p */ p = (int…
Re: The 5-minute Guide to C Pointers
#68Nothing on pointer pointers :( so sad.
Re: The 5-minute Guide to C Pointers
#69Earlier quoted context omitted.
Some thoughts: >> A pointer is a variable that holds, literally points to, a memory address What is a memory address? How does a pointer literally point to a memory address?
It's the guide to pointers, not the guide to memory addresses—I think readers are expected to google unfamiliar terms.
Re: The 5-minute Guide to C Pointers
#70Earlier quoted context omitted.
Well for a quick sum up: An array is like a constant pointer, it's always pointing at the first element of the the array. To try to point it at something else is an error. array[2] is short for *(array + 2*sizeof( )) which takes the address of the first element and adds the appropriate number of bytes to it in order to access the requested element, then it dereferences the "pointer" and you get the value of the eleme…
Just as with pointer arithmetic (because that's what this actually is) you don't multiply by the size of its elements. This: array[2] is the same as this: *(array + 2) The compiler knows what the type of data the pointer refers to and can produce the byte offset itself. Also: "...it's always pointing at the first element of the the array" Eh... an array can degrade into a pointer when needed, but what does the follow…
> ??? x = &arr;
Does this compile? I thought an array was treated like a constant pointer, inexistent in memory so you cannot take its address, increment it, or attribute it another value. Although the point made by RegEx about sizeof, which I didn't remember, convinced me that an array is not actually a constant pointer.To make my thoughts clear, if arr is equivalent to &arr[0], then wouldn't &arr be equivalent to &&arr[0]?