Forums
New posts
Search forums
Members
Current visitors
Log in
Register
What's new
Search
Search
Search titles only
By:
New posts
Search forums
Menu
Log in
Register
Install the app
Install
Forums
Archive
Archive
C Programming
A little rusty on pointers to pointers
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an
alternative browser
.
Reply to thread
Message
[QUOTE="Keith Thompson, post: 3638806"] As you probably know, but I'll emphasize it anyway, all function arguments are passed by value. A parameter is a local variable, containing (at least initially) a copy of the argument value. Modifying the parameter object (name, buff, or length in this case) only modifies this local variable, which ceases to exist when the function returns. Consider a call to this function: void *buff; int length; LoadFileIntoMemory("foo.dat", &buff, &length); The function passes back two pieces of information to the caller: the location where it stored the data, and the length of the data. The location of the data is represented as a void*; the length of the data is represented as an int. So the caller has to pass in *a pointer to* a void*, and *a pointer to* an int, so that the function knows where to store the void* and int values that the caller needs. Suppose the arguments were just a void* and an int: void LoadFileIntoMemory(..., void **buff, int length); { length = ftell(fp); ... buff = malloc(length); } Then the values of length and buff would be lost. As for terminology, "pointer to pointer to void" is fine (it's not a pointer to pointer to buff; buff *is* the pointer). The term "double pointer" should be avoided, unless you're talking about a double* (i.e., a pointer to a floating-point object). As for the function itself: There is no error checking, and no mechanism for the function to report an error to its caller. What if fopen() fails for any of a variety of reasons? What if fseek() fails because you passed it the name of the keyboard device? What if malloc fails because you've run out of memory? Likewise for fread and fclose. [/QUOTE]
Verification
Post reply
Forums
Archive
Archive
C Programming
A little rusty on pointers to pointers
Top