> What is the use of pointers in C++?

What is the use of pointers in C++?

Posted at: 2014-12-18 
You are quite right that there are sometimes several ways to solve a problem, but wrong not to do it in the way that has been requested of you. If you can solve simple problems using pointers you will have a better chance of using pointers properly in more difficult situations.

What sort of situations may they be? Bear in mind that any time you use an array what you are actually using is a pointer to the start of an array. Where the explicit use of pointers is essential is when a programmer must use areas of memory dynamically (creating, extending, shrinking and destroying memory blocks), which comes in handy when writing simulators. You will also find later that you can create pointers to functions, passing those pointers as parameters to other functions.

For the moment i suggest you be patient, and it will become clearer later on.

There are few important operations, which we will do with the pointers very frequently. (a) we define a pointer variables (b) assign the address of a variable to a pointer and (c) finally access the value at the address available in the pointer variable. This is done by using unary operator * that returns the value of the variable located at the address specified by its operand.

pointers are used to target an object in memory.

pointers are used to target an object in memory.

you generally want to use them if you

a) don't want to create a new copy of an object every time you enter a function (performance)

b) you want to give a function access to modify several objects without returning all of them

--- eg: instead of:

--- int a1 (object 1, object 2, object 3)

--- int a2 (object 1, object 2, object 3)

--- int a2 (object 1, object 2, object 3)

--- do

--- void a (object* 1, object* 2, object* 3)

I recommend you read this -> http://www.cplusplus.com/doc/tutorial/po...

Also pointers are heart of C++, you may not need them now because you only studied the very basics of C++.

So far all of the assignment problems that were given to me wants pointers. However, what I find is that I can go on and program these without the use of them. What exactly is the point of using pointers? How do I know when to use them when I am writing my own stuff?