I am having a difficult time understanding pointers and how to use them. As part of my learning process, I like to see what happens when I change things- but I then need to print them out to see what (if any) change has occurred.
Here's a very basic little program as an example:
float x; /* x is of float type */
float *fp; /* fp is a pointer to a float */
x = 6.5; /* x now contains the value 6.5 */
/* print contents and address of x */
printf("Value of x is %f, address of x %ld\n", x, &x);
fp = &x; /* fp now points to location of x */
/* print value and contents of fp */
printf("Value in memory location fp is %f\n", *fp);
printf("address of fp (should be same as address of x) is %ld\n", fp);
Here's the compiler output:
tst.c: In function ‘main’:
tst.c:11:5: warning: format ‘%ld’ expects argument of type ‘long int’, but argument 3 has type ‘float *’ [-Wformat]
tst.c:17:5: warning: format ‘%ld’ expects argument of type ‘long int’, but argument 2 has type ‘float *’ [-Wformat]
And here's the output from running the program:
Value of x is 6.500000, address of x 140737165116276
Value in memory location fp is 6.500000
address of fp (should be same as address of x) is 140737165116276
I've got a lot of warning flags for the compiler turned on, and I realize that programs can _work_ while not completely conforming to all the rules, but I'd like to learn how to do things correctly, and it will help me understand how things work.
Can you tell me what conversion specifier to use when printing out memory addresses (the value inside the pointer)? I haven't found a conversion specifier for doubles, and %f doesn't work (without errors) either.