![]() |
|
#1
|
|||
|
|||
Understanding how pointers are passed in a function.I have been trying to understand how pointers are passed in a function which unfortunately the book that I am referring to doesn't completely explain for a dummie like me. Could you please help me simplify the things.
Here is a part of the code that will be of concern: .....codes... bool str_in(char **); /* Function prototype for str_in */ .....codes.... char *pS[NUM_P]; /* Array of string pointers */ int count = 0; /* Number of strings read */ 1) if(!str_in(&pS[count])) /* Read a string */ .....codes..... 2) bool str_in(char **pString){ ....codes.. *pString = (char*)malloc(strlen(buffer) + 1); ...codes... } So how are 1 and 2 linked? 1 passes the address but what does 2 recieve as argument? (... just relating *pString(recieves an address) = pS(which has the address) *(*pString) = recieves what? ) isn't &pS[count] the same as &*pS(count) the same as pS(count)?? This whole thing is really confusing. The more i think the more i get confused. lol Any genius please help. Thanks! |
|||
|
#2
|
|||
|
|||
Re: Understanding how pointers are passed in a function.Quote:
Consider the following: CPP / C++ / C Code:
Assuming NUM_P is #defined to be an integer constant, pS is an array of pointers to char. The array contains NUM_P members. Each member of the array is a variable whose data type is "pointer to char." Suppose NUM_P is #defined to be 10, then The variable pS[0] is a pointer to char The variable pS[1] is a pointer to char . . . The variable pS[9] is a pointer to char Now, suppose the variable count has a value of 1 then the value of &pS[1] is the address of the variable pS[1]. That is to say, &pS[1] is a pointer to pS[1] and so the data type of &pS[1] is "pointer to pointer to char" I hate to blow the punch line before the end, but here goes: By passing the address of pS[1] to the function, the function can change the value of pS[1]. Now, let's see what the heck is the point of all of this. The function is defined to take a parameter that is "pointer to pointer to char", so the calling program can use &pS[count] as an argument. Inside the function, since pString is a "pointer to pointer to char" we know that *pString is a pointer to char. In fact, if the function were called with &pS[1] as its argument, then inside the function, the value of *pString is equal to pS[1]. So the function is setting pS[1] to the value returned by malloc. That way, it can be used to hold the array of chars that you copy into that memory. And that will be seen as such back in the calling function Bottom line: In the C language, all arguments are passed by value. That is to say, a function is given its own copy of that variable to work with. Nothing that is done to that parameter in the function can change the value of that variable back in the main program. That's right: nothing. In C, if you want a function to change the value of a variable back in the calling function corresponding to the function argument, you must pass a pointer to that variable. CPP / C++ / C Code:
Output: Code:
Now, if you want a function to change the value of a pointer back in the calling function, you pass a pointer to the pointer. I'll do it with integers but the same applies to pointers to chars: CPP / C++ / C Code:
Output (using GNU compiler) Code:
Other compilers may print a zero value instead of the (nil) for the NULL pointer, but the results are the same. Summary A function can not change the value of an argument in the calling function, but if you pass a pointer, the function can change whatever it points to. Regards, Dave |
|
#3
|
|||
|
|||
Re: Understanding how pointers are passed in a function.Quote:
Just the other day you were beginning with pointers and array referencing. Somhow you're now jumping way ahead to passing some pretty sophisticated references. No wonder you'r cornfused... Just remember that your referring to addresses i memory. Requesting the address of an object , passing that address , requesting a value found at that address , etc. Understanding memory, storage and refencing is key to being a good programmer. You can learn a lot from playing around with simple 1 byte referencing examples like this: CPP / C++ / C Code:
CPP / C++ / C Code:
See if you don't get a better sense of your place memory and in the universe... Here is a very good tutorial which helped me a bunch: A TUTORIAL ON POINTERS AND ARRAYS IN C by Ted Jensen Last edited by Howard_L : 14-Jul-2009 at 00:17.
|
|
#4
|
|||
|
|||
Re: Understanding how pointers are passed in a function.A big bunch of thanks to Dave and Howard
That is a great help. |
Recent GIDBlog
Programming ebook direct download available by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| run script command on ns2.26 | newbie06 | Computer Software Forum - Linux | 65 | 19-Aug-2009 08:50 |
| Problem executing nam-1.13 | RodolfoAlvizu | Computer Software Forum - Linux | 20 | 28-Feb-2009 16:23 |
| [Tutorial] Pointers in C (Part II) | Stack Overflow | C Programming Language | 0 | 27-Apr-2005 18:36 |
| [Tutorial] Pointers in C (Part I) | Stack Overflow | C Programming Language | 1 | 08-Apr-2005 19:35 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The