Solution for Exercise 01_03-vardata.c

01_03-vardata.c

#include <stdio.h>

int main()
{
	float v = 1.23456;
	float *vptr;    /* float pointer */

	/* always initialize a pointer! */
	vptr = &v;

	/* remember to use %f for floats! */
	printf("Value of 'v': %f\n",v);
	printf("Its address: %p\n",&v);
	printf("Its size: %zu\n",sizeof(v));
	printf("Value of 'vptr': %p\n",vptr);
	printf("Value of '*vptr': %f\n",*vptr);

	return 0;
}

Output

Value of 'v': 1.234560
Its address: 0x7fff94f68c28
Its size: 4
Value of 'vptr': 0x7fff94f68c28
Value of '*vptr': 1.234560

Notes

* I assign float variable v the value 1.23456 in its declaration: float v = 1.23456;

* The pointer must also be a float: float *vptr;

* The pointer operations work the same as in source code file 01_02-vardata.c. The placeholders are the same in the printf() statements. The operators are the same to fetch the various values.

* The key here is that both v and vptr must be float data types. A pointer's data type must always match the data it references in memory.