0

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.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
c_and_me1
  • 5
  • 4
  • Please show how you print the addresses. [Address space layout randomization](https://en.wikipedia.org/wiki/Address_space_layout_randomization) ensures they aren't the same each run. – Weather Vane Feb 19 '23 at 21:42
  • `printf("\nThe addresses of each pointer are: "); printf("%d %f %c", iPtr, fPtr, cPtr);` – c_and_me1 Feb 19 '23 at 21:43
  • Welcome: please edit the question to provide further information. – Weather Vane Feb 19 '23 at 21:44
  • 3
    Oh, you are using the wrong format specifiers: `printf("%p %p %p", (void*)iPtr, (void*)fPtr, (void*)cPtr);` – Weather Vane Feb 19 '23 at 21:45
  • You aren't changing `i` etc. You are changing `iNumber`. To change `i` by using the pointer, you'd do `*iPtr = 8`. Then `i` will be 8. – Emanuel P Feb 19 '23 at 21:46
  • 1
    The values printed are the original source values, not the ones you changed. That is why they are still the same. You need `printf("%d %f %c", iNumber, fNumber, cCharacter);` – Weather Vane Feb 19 '23 at 21:50
  • `printf("%d %f %c", *iPtr, *fPtr, *cPtr);` to print values held in objects referenced by the pointers – 0___________ Feb 19 '23 at 22:15

2 Answers2

0

This (second) call of printf

printf("\nThe addresses of each pointer are: ");
printf("%d %f %c", iPtr, fPtr, cPtr);

is wrong. You are trying to output pointers as objects of the type int, float and char that does not make sense.

If you want to output stored addresses in the pointers you need to write

printf("\nThe addresses of each pointer are: ");
printf("%p %p %p\n", ( void * )iPtr, ( void * )fPtr, ( void * )cPtr);

(do not forgot to place the new line character '\n' in the format string)

Also in this call of printf

printf("\nThe modified values are:  ");
printf("%d %f %c", i, f, c);

you are not outputting modified values. The values of the variables i, f, and c were not changed. It seems you mean instead

*iPtr = iNumber;
*fPtr = fNumber;
*cPtr cCharacter;

printf("\nThe modified values are:  ");
printf("%d %f %c\n", i, f, c);
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
0

Changing the value using Pointer


I think ultimately you want to change the value of i from 5 to the value of the variable iNumber, which is 2 using pointer.

Your code :

iNumber = *iPtr;
fNumber = *fPtr;
cCharacter = *cPtr;

What you've done here is, assigned the value of each pointer variable to the iNumber, fNumber, cCharacter variables.

So now if you print those variables you could see that the values of iNumber, fNumber, cCharacter is changed to 5, 7.77, a accordingly. Because, those pointer variables are pointing to the address of the first 3 variables, that are &i, &f and &c as you've might guessed.

Solution :

*iPtr = iNumber;
*fPtr = fNumber;
*cPtr = cCharacter;

As you can see in the above snippet, you need to assign the new values/variable to the pointer variable instead of doing the opposite.


Printing the Address of a Variable


Your code :

printf("\nThe addresses of each pointer are: ");
printf("%d %f %c", iPtr, fPtr, cPtr);

To print the memory addresses of variables in C, you need to use the %p format specifier in printf() instead of %d or %f. The %p format specifier is specifically used for printing memory addresses in hexadecimal format.

Also, when you're printing the memory address of a variable, you have to use the address-of operator (&), which is used to get the address of a variable.

Solution :

printf("\nThe addresses of each pointer are: ");
printf("%p %p %p", &iPtr, &fPtr, &cPtr);   // hexadecimal

// not the standard way to represent memory addresses
printf("%u %u %u", &iPtr, &fPtr, &cPtr);   // decimal

Tip : You can print the address in Decimal instead of Hexadecimal for ease of mind, just by replacing %p to %u. (the adreess-of operator remains intact while printing memory addresses)


Why do the memory addresses change on each run?


When you run your C program, the computer assigns a memory address to each variable in your program. The memory address is typically represented as a large number, usually in hexadecimal format. The memory address is important because it allows the program to access and manipulate the value of the variable stored at that location in memory.

However, the memory address of a variable can change from one program run to another, for a number of reasons, such as randomization of the memory space, allocation of memory on the heap or stack, or optimization of the program code by the compiler or linker.

But as a beginner, you don't have to worry about it.

SOBHAN
  • 1
  • 2