+ 1

Use of pointer?

In, int y=5; int *x; x=&y; cout<<*x<<endl; If y=*x, what's the use of pointer? Why not use y directly?

10th Feb 2021, 6:15 PM
Yash Mayank
Yash Mayank - avatar
4 Respuestas
+ 5
so here y represents the value... and *x also represents the value... and they are equal based on your definition.... one memory location can have many pointers and many aliases.... here x is just a address that points to that memory location whereas y is an alias for that location.... pointers are useful when u want update the value in functions... they don't get duplicated into the function scope rather stay same.
10th Feb 2021, 6:25 PM
NaSaPaKri
NaSaPaKri - avatar
+ 3
For a simple program, there's no use having x but for more complex programs there is.
10th Feb 2021, 7:48 PM
Sonic
Sonic - avatar
+ 3
An interface pointer, x maybe needed to alternate or cycle between &y and other variable addresses like &z depending on the program.
10th Feb 2021, 7:49 PM
Sonic
Sonic - avatar