0

Possible Duplicate:
Why use pointers?

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

Community
  • 1
  • 1
Daniel Node.js
  • 6,734
  • 9
  • 35
  • 57

3 Answers3

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).
Nawaz
  • 353,942
  • 115
  • 666
  • 851
  • @Dan: If it clarified your doubts, the accept it as your answer, by clicking on the tick mark! – Nawaz Feb 24 '11 at 19:49
0

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

Andrew
  • 24,218
  • 13
  • 61
  • 90
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.

Chris Pfohl
  • 18,220
  • 9
  • 68
  • 111