+ 7

What is the usefulness of pointers?

I was just doing the lessons in c++ and was womdering what the usefulness of the pointer class of definitions was. Why create a variable with the memory address and then use the pointer over just using the variable itself? https://code.sololearn.com/cYUw3Mc6xjHm/?ref=app

30th Mar 2018, 10:59 PM
Nathan Jones
Nathan Jones - avatar
12 Respuestas
+ 17
Pointers allow you to point to an address instead of a variable. Usefulness? You can have a function that modifies a variable permanently. You can access multiple variables with a single pointer. You can directly access memory locations on embedded devices. You can manipulate arrays or segments thereof more easily. You can reduce copying overhead for large variables when passing a pointer to a function rather than have a copy of it... The list goes on. It's something you learn by experience. You just realise, "I'll need a pointer there." when the time comes.
30th Mar 2018, 11:44 PM
non
+ 6
- In function arguments where you need access the real variable not the copied value - Data structures like linked list - Memory management Ect... Only 3 uses but you get the point
30th Mar 2018, 11:44 PM
TurtleShell
TurtleShell - avatar
+ 6
1st Apr 2018, 3:42 PM
David Carroll
David Carroll - avatar
+ 3
very helpful answer nonzyro 👍
1st Apr 2018, 4:07 PM
Morpheus
Morpheus - avatar
+ 3
That is a very good question, if you can do the following int x = 4 ; int * ptr = &x; *ptr = *ptr +10 ; and if that was the only reason to use pointers then why brother using them. There are good reasons to use pointers , local variables in functions don't exist after the called function is returned. Neither do variables passed by value to functions ; example : void func (int x) { x = x + 4 // x is now 10 return ; // go back to // calling function } // // But after called function //returns, the variable // passed by copy to function no longer exists because only a copy of the data is passed to the parameter of the function called func. // And all local variables // in that function // also don't exist after // called function returns. // Therefore only a copy // of the value of num // which is 6 is passed // to parameter // variable called x in func. // But after return, variable // x now no longer exists. // // Variables x and num // are two separate // variables or memory // locations. they are not // the same variable. // When passing by copy or value, // think of the parameter x like // the following // int x = num ; // we have two completely // different variables of the // same data type int main () { int num = 6 ; func (num) ; // after func returns , // num is still 6. // There is no change. cout << num } ////////////////// //To solve this problem // use pointers // We are passing by // reference, that is the actual address of the variable called x is passed // to px in func. // That is we are now using // the actual variable. // think of parameter ptr // as, int *ptr = &x. void func (int * px) { *px = *px + 4; // *px is now 10, and so is // the actual variable x // is changed. return ; } int main () { int x = 6; // pass address of variable // to func func (&x) ; cout << x ; // x is now 10 ; // because we are dealing // with only one memory // location. // *px and x both refer to the same // memory location. }
5th Apr 2018, 7:14 AM
Rick Zalman
+ 1
Pointer is the first adress node of an array. It represents the first memory location of an array. You can declare more no. Of variable using a single pointer but you have to write the base address of that variable...
5th Apr 2018, 4:30 PM
Ashish Kumar Sahoo
Ashish Kumar Sahoo - avatar
+ 1
Pointers help us to make certain programming tasks easier. Moreover,dynamic memory allocation extensively uses pointers.
20th Apr 2018, 2:42 PM
DEBAYAN BHAUMIK
DEBAYAN BHAUMIK - avatar
+ 1
Pointer are the fundamental part of C++ .if u can't use pointer properly then u will basically lost all the power and flexibility in c++ and the pointer are used in c++ to manipulate and access the address........
27th Jun 2018, 8:00 AM
Lipsa Mohanty
Lipsa Mohanty - avatar
0
Yes you can do that.
4th Apr 2018, 11:26 AM
Ashish Kumar Sahoo
Ashish Kumar Sahoo - avatar
5th Apr 2018, 4:05 PM
Nguyen Huy (Meo)
0
pointers its the most powerful tool of C/C++, its maybe the greatest java lack. can also *use function pointers *pass values directly by reference: type func(int & x) {…} func(x); //not "&x" here, func param isnt ptr with pointers u practicly become god, low level control, closely to the machine! pointer arythmetic its a must
11th Aug 2019, 11:54 AM
BinaryEden
BinaryEden - avatar
0
It is useful really. Pointers are used to store and manage the addresses of dynamically allocated blocks of memory. Such blocks are used to store data objects or arrays of objects. Features of Pointers: Pointers save memory space. Execution time with pointers is faster because data are manipulated with the address, that is, direct access to memory location. Memory is accessed efficiently with the pointers. The pointer assigns and releases the memory as well. Hence it can be said the Memory of pointers is dynamically allocated. Pointers are used with data structures. They are useful for representing two-dimensional and multi-dimensional arrays. An array, of any type can be accessed with the help of pointers, without considering its subscript range. Pointers are used for file handling. Pointers are used to allocate memory dynamically. In C++, a pointer declared to a base class could access the object of a derived class. However, a pointer to a derived class cannot access the object of a base class.
6th May 2020, 5:23 PM
꧁༒☬Bad☬Boy☬༒꧂
꧁༒☬Bad☬Boy☬༒꧂ - avatar