2

I'm pretty much a beginner at C++. Just started learning it a few weeks ago. I'm really interested in improving my skills as a programmer, and there's something that's been confusing me in the last few days. It is pointers. Pointers and the reference operator. My question is, what exactly is the functionality of the pointers and reference operator? How will I know when to use them, and what are their purposes and common usages. Any examples consisting of common algorithms using dereference and reference will be greatly appreciated.

how can I use reference and dereference to become a better programmer, and improve my algorithms(and possibly make them simpler)?

Thanks :D

Lockhead
  • 2,423
  • 7
  • 35
  • 48
  • can someone mark this as closed. I think there are approximately 1.5 mega questions that are exactly the same. Also try the internets, they seem to have endless information on pointers and deferencing... – Marm0t Dec 01 '10 at 15:28
  • What exactly do you mean by "reference operator"? Do you mean something like `int& param` in the parameter list of a function, or more like `&var` in an expression? – fredoverflow Dec 01 '10 at 15:50
  • This is far too broad of a question to be adequately answered here. I recommend you continue reading your book/tutorial or whatever materials you are using to learn C++, as they will often explain and offer examples. Reading elsewhere will be helpful, but again, this seems far too broad to be answered here and will likely be closed as a question. http://en.wikipedia.org/wiki/Pointer_%28computing%29 http://en.wikipedia.org/wiki/Reference_%28C%2B%2B%29 – KevenK Dec 01 '10 at 15:24

4 Answers4

3

Definitely check this question out, the accepted answer explains pointers and common errors with them in a nice manner.

Update: a few words of my own

Pointers are bunches of bits, like any other kind of variable. We use them so much because they have several very convenient properties:

  • Their size (in bytes) is fixed, making it trivial to know how many bytes we need to read to get the value of a pointer.

When using other types of variables (e.g. objects), some mechanism needs to be in place so that the compiler knows how large each object is. This introduces various restrictions which vary among languages and compilers. Pointers have no such problems.

  • Their size is also small (typically 4 or 8 bytes), making it very fast to update their values.

This is very useful when you use the pointer as a token that points to a potentially large amount of information. Consider an example: we have a book with pictures of paintings. You need to describe a painting to me, so I can find it in the book. You can either sit down and paint an exact copy of it, show it to me, and let me search the book for it; or you can tell me "it's in page 25". This would be like using a pointer, and so much faster.

  • They can be used to implement polymorphism, which is one of the foundations of object-oriented-programming.

So, to find out how to use pointers: find cases where these properties will come in handy. :)

Community
  • 1
  • 1
Jon
  • 428,835
  • 81
  • 738
  • 806
  • Yeah, it's definitely a good answer there. Helped me understand pointers a bit more, but I still don't know how to actually practically and efficiently use them. – Lockhead Dec 01 '10 at 16:17
  • Updated the answer, check it out. – Jon Dec 01 '10 at 16:28
1

There's some things a programmer needs to understand before diving into pointers and C++ references.

First you must understand how a program works. When you write variables out, when you write statements, you need to understand what's happening at a lower level; it's important to know what happens from a computer stand-point.

Essentially your program becomes data in memory (a process) when you execute it. At this point you must have a simple way to reference spots of data - we call these variables. You can store things and read them, all from memory (the computers memory).

Now imagine having to pass some data to a function - you want this function to manipulate this data - you can either do this by passing the entire set of data, or you can do it by passing its address (the location of the data in memory). All the function really needs is the address of this data, it doesn't need the entire data itself.

So pointers are used exactly for this sort of task - when you need to pass address of data around - pointers in fact are just regular variables that contain an address.

C++ makes things a bit easier with references (int &var) but the concept is the same. It lets you skip the step of creating a pointer to store the address of some data, and it does it all automatically for you when passing data to a function.

This is just a simple introduction of how they work - you should read up on Google to search fo more detailed resources and all the cool things you can do with pointers/references.

Luca Matteis
  • 29,161
  • 19
  • 114
  • 169
0

Better name of the operator is "Address of" operator. Because it returns the address of the operand.

In C++ you will use pointers (and both reference/dereference operators) when dealing with dynamically allocated memory or when working with pointer arithmetic.

Pointers are also used to break down static bindings since they imply dynamic binding (through the address stored in the pointer, which can change dynamically).

For all other uses, it is usually better to use references instead of pointers.

Šimon Tóth
  • 35,456
  • 20
  • 106
  • 151
  • "Because it returns the address of the operand." -> technically incorrect, the standard clearly says "The result of the unary `&` operator is a **pointer** to its operand.". – fredoverflow Dec 01 '10 at 15:49
0

to be short: reference are some improvment of pointers that inherited from C to C++ its a bit safer because it helps you avoid using "*" in your functions and that cause you less segmentation faults. or like my frines say "avoid the starwars"

there is a lot to learn about it !!!!

look for the use of "&" for sending and receiving values by refrence understand the use of "&" for getting variable adress

its a very very big question, if you can be more specific it will be better.

Ran S
  • 105
  • 9