Hello,
1) What I mean't by function scope is simple. All it knows is what is happening within the function, and it doesn't have a clue as to what is happening in the 'int main' part of the program. This kind of scope is called 'local scope'. Whatever happens in the 'int main' is only visible to that section of the program. And on the other hand, anything that happens in the function 'test' is only visible to that part of the program, which in both cases is between the opening and closing curly braces.
2) That strikes me as well. Let me re-post my example using the
%p type condition instead of the
%0x%08x:
#include <stdio.h>
#include <stdlib.h>
void test(char **p) {
/* run test; view memory address */
printf("Function scope: %p\n", (void *)*p);
/* free memory */
free(*p);
/* set to NULL */
*p = NULL;
printf("Function scope - After NULL: %p\n", (void *)*p);
}
int main() {
char *ptr = malloc(25);
/* allocation may have failed */
if (ptr == NULL)
return 0;
printf("Before call: %p\n", (void *)ptr);
/* send memory address */
test(&ptr);
/* run some tests */
printf("After call: %p\n", (void *)ptr);
return 0;
} In this particular case, and for testing purposes only, this is what my output looked like:
Before call: 0xa0501b8
Function scope: 0xa0501b8
Function scope - After NULL: 0x0
After call: 0x0
As seen, the address of ptr is the same when in the scope of main, and dereferenced in the scope of test().
-
Stack
Overflow