0

Possible Duplicate:
Why use pointers?

I know what the C++ & does. but what can it be used for?

6
  • 7
    I know what the + operator does, but what can it be used for?
    – user395760
    Feb 19, 2011 at 16:20
  • That doesn't make any sense dude.
    – Daniel
    Feb 19, 2011 at 16:21
  • The & gives me the address in the memory where it is stored. BUT WHAT CAN I DO WITH IT???
    – Daniel
    Feb 19, 2011 at 16:22
  • 1
    The + gives me the sum of two numbers. BUT WHAT CAN I DO WITH IT???
    – user395760
    Feb 19, 2011 at 16:23
  • are you asking what pointers are useful for?
    – tenfour
    Feb 19, 2011 at 16:23

3 Answers 3

4
  • & is used to pass address of arguments (pointer) to function, when it's used at calling site.
  • & is used to pass arguments by reference to function, when it's used in function parameter list.
  • & is bitwise AND. e.g. (a & b)
  • & is used in logical AND. In this case, two & make logical AND. e.g (a && b).
1
  • @Dan: If it clarified your doubts, the accept it as your answer, by clicking on the tick mark! Feb 24, 2011 at 19:49
0

For example to pass a pointer to your object into some function.

0

Many functions in the STL or other commonly available libraries requires a pointer to an object (not the object itself). Also, many time you'll want to pass pointers. When you need that, the & operator allows you to get a pointer to any object you have access to.

Browse through the boost libraries and find some. One example:

template<class Y> explicit shared_ptr(Y * p);

To pass in a pointer to a Y you'd have to use the & operator.

Furthermore, your profile says you're into 3-d games. Almost every C++ 3d library I know of uses pointers to arrays of floats or ints to manipulate everything. You need the & operator to pass in the pointers to those arrays.

1
  • It is quite likely that you do not want to pass an address obtained with & to shared_ptr.
    – UncleBens
    Feb 19, 2011 at 17:28

Not the answer you're looking for? Browse other questions tagged or ask your own question.