+ 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?
4 ответов
+ 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.
+ 3
For a simple program, there's no use having x but for more complex programs there is.
+ 3
An interface pointer, x maybe needed to alternate or cycle between &y and other variable addresses like &z depending on the program.