how to set a pointer as a parameter?

Started by
19 comments, last by fireking 20 years, 5 months ago
i dunno if this will help the situation any, or if anyone has already said this since i didn''t read every word of every post, but...

pointers are just memory addresses. in fact, they are unsigned integers that happen to represent a place in memory. if u have something like float * floatptr = &myfloat, then floatptr holds the memory address of myfloat, which in this case is memory on the stack. why do pointers have types (i.e. why are there float *''s and int *''s and char *''s)? so the compiler can know what value to add to a pointer when u are indexing it.

suppose u have char * arrayptr that has the value 0x00aabbcc (11189196 in base 10). since the pointer is a char *, and a char data type is 1 byte, if u specify ''arrayptr[1]'' the compiler will fetch the 1-byte value at 0x00aabbcc + 1, or 0x00aabbcd.

or, if u have a pointer to a structure that holds, say 3 floats (so, twelve bytes since each float is 4 bytes), then multiples of twelve will get added to whatever number u use to index the pointer. structptr[3] is actually adding 3 * 12 to whatever value structptr is.

u can use any integral value as a pointer if u type cast it as one. conversely, u can use any pointer as an integral value. they are one and the same.

getting back to the original question, i believe ready4dis had the simplest idea - just have the function take the address of the pointer (i.e. a pointer to a pointer), and set the pointer either by dereferencing or indexing (i.e. *ptr or ptr[0]).

This topic is closed to new replies.

Advertisement