Why can't we store the address of variable in a normal variable wich is not a pointer? Why the pointer is preferred to | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Why can't we store the address of variable in a normal variable wich is not a pointer? Why the pointer is preferred to

11th Mar 2020, 2:56 AM
Lagishetti Venkatesh
6 Answers
+ 1
Not just access the content, but also make changes to the content. So you are actually changing the value of a variable using another variable.
11th Mar 2020, 5:01 AM
XXX
XXX - avatar
11th Mar 2020, 5:32 AM
Sonic
Sonic - avatar
+ 1
Just like you can't store an integer in a string, you can't store a pointer in an integer. Even though the value in a pointer is an integer, but the difference is that a pointer allows you to access the memory and make changes directly to it, but an int only allows you store and access the memory. For example, in the following program let's consider that the memory address of x is 123456 int x = 10; int* y = &x; int z = 123456; So in the above program, both y and z store the memory address of x. But you can make changes in the value of x using y but not z. This works and changes the value of x to 100 *y = 100; But this doesn't work *z = 123456
11th Mar 2020, 4:51 AM
XXX
XXX - avatar
+ 1
Thanks for the reply So to store address we prefer pointer which is unique and only can be used to store the address data type and access the contents using dereference operator.
11th Mar 2020, 4:59 AM
Lagishetti Venkatesh
0
Yeah I got it now
11th Mar 2020, 5:10 AM
Lagishetti Venkatesh
0
Yes. I saw the difference too for address the the data is in hexadecimal form for int it is in integer form thank you for responding
11th Mar 2020, 6:41 AM
Lagishetti Venkatesh