I've just started C++ and am trying to get to grips with pointers, consider the following 2 programs:

Code:

void change(int &i)
{
i = 5;
}

int main()
{
int i = 0;
change(i);
printf("%d\n",i);
}

Code:

void change(int *i)
{
*i = 5;
}

int main()
{
int i = 0;
change(&i);
printf("%d\n",i);
}

The first one passes the reference of i, and the second passes up a pointer to i(correct me if I'm wrong)

What is the difference between the two methods of passing variables? As far as I can see, both edit the value of i directly. Why are pointers more useful?

Thanks

06-02-2007

Queatrix

There is no difference really, you're just writing the address/pointer conversions in different places.

06-02-2007

Salem

> Why are pointers more useful?
Shouldn't that be why are references more useful?

A reference is always valid, whereas a pointer may be NULL, or garbage.

A reference is type-safe, whereas a pointer can be coerced into only appearing to be the right type.

06-02-2007

Wiretron

Hmm ok.

In what situations are pointers more useful than references then?

06-02-2007

AverageSoftware

Quote:

Originally Posted by Wiretron

Hmm ok.

In what situations are pointers more useful than references then?

One occasion is when you need an "empty" value. In that occasion you use a pointer set to NULL.

06-02-2007

Hussain Hani

the difference a pointer and a reference is that references have cleaner syntax plus less memory taken compared to pointer :)
but i use pointers anyway to have the complete functionality of the memory. And when using pointers, be careful not to dereference a pointer that doesn't point to anything.

the difference a pointer and a reference is that references have cleaner syntax plus less memory taken compared to pointer :)

"Cleaner syntax" is subjective: not everyone's interpretation of "clean" is the same. There is no particular reason that use of references would consume either more or less memory than use of pointers.

A reference, once created, cannot be reseated (i.e. made to refer to something else). A pointer can. Changing the value of a reference (eg assignment, iterating it) always affects the object referred to, whereas changing the value of a pointer reseats the pointer. For example;

Code:

int main()
{
int x[2];
int &refx(x[0]);
int *px(&x[0]);

x[0] = 5; x[1] = 10; // some arbitrary values in x

++refx; // sets x[0] to be 6
++px // changes px so it points at x[1] (reseating the pointer)

refx = x[2]; // assigns x[0] so it has the value in x[1] (10)
*px = 5; // sets x[1] to be 5, but x[0] is still 10

}

One effect of this is that pointers can be used to work on arrays (eg incrementing a pointer makes it point to the next element in an array, therefore the joys of pointer arithmetic. References cannot be used in a similar manner.

06-03-2007

JJFMJR

Quote:

Originally Posted by grumpy

"Cleaner syntax" is subjective: not everyone's interpretation of "clean" is the same. There is no particular reason that use of references would consume either more or less memory than use of pointers.

A reference, once created, cannot be reseated (i.e. made to refer to something else). A pointer can. Changing the value of a reference (eg assignment, iterating it) always affects the object referred to, whereas changing the value of a pointer reseats the pointer. For example;

Code:

int main()
{
int x[2];
int &refx(x[0]);
int *px(&x[0]);

x[0] = 5; x[1] = 10; // some arbitrary values in x

++refx; // sets x[0] to be 6
++px // changes px so it points at x[1] (reseating the pointer)

refx = x[2]; // assigns x[0] so it has the value in x[1] (10)
*px = 5; // sets x[1] to be 5, but x[0] is still 10

}

One effect of this is that pointers can be used to work on arrays (eg incrementing a pointer makes it point to the next element in an array, therefore the joys of pointer arithmetic. References cannot be used in a similar manner.

Well Put..

06-03-2007

brewbuck

Quote:

Originally Posted by Wiretron

The first one passes the reference of i, and the second passes up a pointer to i(correct me if I'm wrong)

What is the difference between the two methods of passing variables? As far as I can see, both edit the value of i directly. Why are pointers more useful?

This might trigger a huge debate. I try to advocate completely pointer-less programming in C++, although there are times when there's simply no other option but to use a pointer. In most cases it is unnecessary, especially if you take full advantage of the containers provided by STL.

At the level of the underlying functionality of the language, a pointer and a reference are pretty much the same thing. At the syntactic level, they work differently. A pointer is a variable that can be assigned and reassigned at will. You can use the same pointer variable to point at one object, then change it to point at another. A reference can't do this. All references are "bound" to an object the moment they come into existence. This eliminates a host of problems centering around uninitialized pointers.

I'd suggest that if you don't see why you need pointers, then you don't need pointers. Stick with references until you bump into one of the cases where they simply aren't enough.

06-03-2007

brewbuck

Quote:

Originally Posted by Salem

> Why are pointers more useful?
Shouldn't that be why are references more useful?

Pointers are strictly more powerful. I don't know about "more useful." The most common case I can think of where pointers are required would be a container of polymorphic objects. Most STL containers require that their contents be default-constructible. A reference is not default-constructible so it is impossible to use references to implement containers of polymorphic types. You simply have to use pointers in that case.

06-03-2007

MacGyver

Deep down, for function calls, pointers and references are more or less the same, as the function does need the address of the variable to work on it. I hope no one mistakes what I'm saying here. I don't think they should be interchanged whenever a programmer might feel like it.

The importance of references is a higher-level concept for the sake of the programmer and users.