Passing by reference/using pointers

This is a discussion on Passing by reference/using pointers within the C++ Programming forums, part of the General Programming Boards category; I've just started C++ and am trying to get to grips with pointers, consider the following 2 programs:
Code:
void ...

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.

"C makes it easy to shoot yourself in the foot; C++ makes it harder, but when you do, it blows away your whole leg."-Bjarne Stroustrup
Nearing the end of finishing my 2D card game! I have to work on its 'manifesto' though <_<

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;

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.

"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;

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.

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.

> 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.

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.