I'm a beginner to C and have been reading a book called "Understanding and Using C Pointers" and ended up running into a problem with using the printf function.
When using debug_p, it turns out that the address of the pointer num_p is different but exact to num in debug, but it returns the correct value when attempting a printf within main().
My question is, why is this so? Is there an appropriate way to do a printf within a function and have a correct address of a pointer?
#include <stdio.h>
void debug(int num)
{
printf("Address of num: %i Value: %i\n", &num, num);
}
void debug_p(int *num_p)
{
printf("Address of num_p: %i Value %i\n", &num_p, num_p);
}
int main()
{
int num=11;
int *num_p=#
printf("Address of num: %i Value: %i\n", &num, num);
printf("Address of num_p: %i Value: %i\n\n", &num_p, num_p);
debug(num);
debug_p(num_p);
return 0;
}
Output from main():
Address of num: 2358860 Value: 11
Address of num_p: 2358848 Value: 2358860
Output from debug, then debug_p:
Address of num: 2358816 Value: 11
Address of num_p: 2358816 Value 2358860
%pto print variable's address.numandnum_pinside the functions are copies, passed by value, and not the same as the variables defined outside the function.