Pointers In Parameters For Functions Confusion?

So you guys are always a great help and I appreciate that. Something I've always struggled with is pointers but I'm slowly getting better I think.

So I've seen people pass reference this way

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include <string>
using namespace std;

void ChangeStr(string* strEnter)
{
  *strEnter += " Not Much";	
}

int main(int argc, char *argv[])
{
	string strTell = "Dude What's Up?";
	ChangeStr(&strTell);

	cout << strTell << endl;


	system("PAUSE");
	return 0;
}


For example. Is there anything bad about doing the above code and is this how you should properly pass reference?

Second I see a lot of pointers within functions that have no variables name like

1
2
void Whatever(char*)
{}


What possible reason do people declare functions like this or am I missing something?

Thanks guys!
In answer to your first question, yes, that works fine, although the phrase is "pass by reference".

However, C++ introduces a new way of doing it. It introduces a new construct - the reference. Instead of passing a pointer into your function, you pass a reference.

To declare this, use an ampersand, & instead of an asterisk, *.

Then, inside your function, you use the same semantics as the actual variable, rather than those of a pointer, like so:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include <string>
using namespace std;

void ChangeStr(string& strEnter)
{
  strEnter += " Not Much";	
}

int main(int argc, char *argv[])
{
	string strTell = "Dude What's Up?";
	ChangeStr(strTell);

	cout << strTell << endl;


	system("PAUSE");
	return 0;
}


The (major) advantage of using references is that you don't have to worry about pointer safety, i.e. whether or not your pointer is pointing to valid memory.

The (slight) advantage of using pointers is that within the body of your function, it's immediately obvious that you're using a pointer and that any changes you make to the value of the memory it points to will be present in the calling code. With references, you can only tell whether you're using a reference or a local copy by looking at the function declaration to see if there's an & symbol.

I won't go into a full tutorial on references; if you're learning C++, then you presumably already have access to reference material.

Regarding your second question, I've usually seen it used in situations where your function has to conform to a particular interface, but a specific implementation doesn't actually use the argument that the interface required to be passed in. If you don't specify a name for the argument in your function implementation, then it makes it clear to anybody else reading the code that it won't be used in your function.

It will also stop the compiler warning you about unused arguments. Getting rid of compiler warnings is a good thing, because it makes it easier to spot any new warnings you introduce that indicate a genuine problem with your code.
Last edited on
The other big thing about ptrs in functions, is to provide quick access to large data structures. Instead of passing the whole thing by value, just provide a pointer to it instead.

As MikeyBoy said, the C++ way is to use references which is very similar. One advantage of a reference is that it always refers to something valid, whereas a ptr can be made to point at anything - valid or not.

HTH
Couple nitpicks.

MikeyBoy wrote:
In answer to your first question, yes, that works fine, although the phrase is "pass by reference".

Passing a pointer to an object is not passing by reference. It is passing a pointer by value.

TheIdeasMan wrote:
One advantage of a reference is that it always refers to something valid, whereas a ptr can be made to point at anything - valid or not.

Not true, unfortunately. References can reference things that are not valid.

1
2
3
4
5
char& function()
{
    char ch ;
    return ch ;
}
@cire

As you said - a nitpick, I thought I would post this, just for the education of anyone who was wondering.

Wiki wrote:
C++ references differ from pointers in several essential ways:

It is not possible to refer directly to a reference object after it is defined; any occurrence of its name refers directly to the object it references.
Once a reference is created, it cannot be later made to reference another object; it cannot be reseated. This is often done with pointers.
References cannot be null, whereas pointers can; every reference refers to some object, although it may or may not be valid. Note that for this reason, containers of references are not allowed.
References cannot be uninitialized. Because it is impossible to reinitialize a reference, they must be initialized as soon as they are created. In particular, local and global variables must be initialized where they are defined, and references which are data members of class instances must be initialized in the initializer list of the class's constructor. For example:

int& k; // compiler will complain: error: `k' declared as reference but not initialized


Your example is interesting though, it is the return of a reference as opposed to a reference as a parameter. If the compiler is to complain about the snippet from wiki above, then shouldn't it complain about your example as well?

I was trying to find something in the standard.
I found this:

§ 8.3.2

5
There shall be no references to references, no arrays of references, and no pointers to references. The
declaration of a reference shall contain an initializer (8.5.3) except when the declaration contains an explicit
extern specifier (7.1.1), is a class member (9.2) declaration within a class definition, or is the declaration
of a parameter or a return type (8.3.5); see 3.1. A reference shall be initialized to refer to a valid object
or function.
[ Note: in particular, a null reference cannot exist in a well-defined program, because the only
way to create such a reference would be to bind it to the “object” obtained by dereferencing a null pointer,
which causes undefined behavior. As described in 9.6, a reference cannot be bound directly to a bit-field.
— end note ]

Yes. In the example I gave, I would expect as a QOI issue to get a warning from the compiler. However, the reference is initialized to refer to a valid object. It just ceases being valid before it can be accessed.

It isn't really that hard to invalidate a reference. There are three really common cases I've seen. The one I presented, the case where the objected referred to is moved (either by reallocation of the container it's in, or some other method), and the case where the object's lifetime simply doesn't match the reference's (e.g. it's erased from a container or deleted.)

Things to keep in the back of your mind when dealing with references (or pointers.)
Last edited on
Topic archived. No new replies allowed.