View Single Post
Old 07-14-2012, 10:18 PM   #4
geekmaster
Carpe diem, c'est la vie.
geekmaster ought to be getting tired of karma fortunes by now.geekmaster ought to be getting tired of karma fortunes by now.geekmaster ought to be getting tired of karma fortunes by now.geekmaster ought to be getting tired of karma fortunes by now.geekmaster ought to be getting tired of karma fortunes by now.geekmaster ought to be getting tired of karma fortunes by now.geekmaster ought to be getting tired of karma fortunes by now.geekmaster ought to be getting tired of karma fortunes by now.geekmaster ought to be getting tired of karma fortunes by now.geekmaster ought to be getting tired of karma fortunes by now.geekmaster ought to be getting tired of karma fortunes by now.
 
geekmaster's Avatar
 
Posts: 6,433
Karma: 10773670
Join Date: Nov 2011
Location: Multiverse 6627A
Device: K1 to PW3
Arrow C pointer "micro-tutorial"

One thing to remember in calling C functions is that the CALLED function receives a COPY of everything that was passed to it. Because a function can only return a single value (which is often just an error code), returning MORE stuff requires that the CALLER pass an address of a CALLER's variable or structure in which the CALLED function can place the extra return value(s).

Addresses are passed in pointer variables (or with the address of a non-pointer var). If the CALLER wants to pass the ADDRESS of a var "int myVar", then it would pass &myVar in the call (in place of a pointer var parameter "int *pVar). The CALLED function would get a COPY of that address in pVar (which still points at the same address), and it would write to that address with *pVar=666. That value is immediately stored into the CALLER's myVar variable (even before returning to it).

To return a lot of data, the CALLER can pass the address of a structure instead. It can set myVar inside myStruct like this: myStruct.myVar=666. Then it can pass &myStruct in a parameter in place of struct *pStruct. The CALLED function gets a copy of pStruct (which points to the CALLERS myStruct). To access data using a structure pointer (address of a structure), do this:
pStruct->myVar=666.

C++ has other funny (non-C) stuff involving &vars inside functions, which is just shorthand for noobs to avoid all those pesky pointer dereferences.

Clear as mud, right? The way to learn it is to DO it. Write some tiny little proggies that futz with pointers of various types and see what they do.

Last edited by geekmaster; 07-14-2012 at 10:25 PM.
geekmaster is offline   Reply With Quote