I'm working with pointers for the first time in C. I tried to declare, initialize, and assign a memory address to 3 pointers, then print the addresses and values of each pointer and variable, then assign a value to the pointer so that the value of the data which has the memory address is changed. Here is the code:
int i = 5;
float f = 7.77;
char c = 'a';
int iNumber = 2;
float fNumber = 5.55;
char cCharacter = 'c';
int *iPtr;
float *fPtr;
char *cPtr;
iPtr = &i;
fPtr = &f;
cPtr = &c;
printf("\nThe current values are: ");
printf("%d %f %c", i, f, c);
printf("\nThe addresses of each pointer are: ");
printf("%d %f %c", iPtr, fPtr, cPtr);
iNumber = *iPtr;
fNumber = *fPtr;
cCharacter = *cPtr;
printf("\nThe modified values are: ");
printf("%d %f %c", i, f, c);
printf("\nThe addresses of each pointer are: ");
printf("%d %f %c", iPtr, fPtr, cPtr);
return 0;
is when I change the values of i
, f
, and c
.
However, when I run the program the memory addresses for the int
variables are not in hexadecimal format, they're in random numbers that change every time I run the program. The addresses for the char
ones don't appear. Also, when I modifiy the values in the pointers the variables they're pointing to don't change. I thought this was how you referenced by value, i'm really confused.