C++ Passing Pointer to Function (Howto) + C++ Pointer Manipulation

argumentscfunctionpointers

I am a little confused as to how passing pointers works.

Let's say I have the following function and pointer, and…

EDIT:

…I want to use a pointer to some object as an argument in the function.

i.e.:

void Fun(int Pointer){
    int Fun_Ptr = ---Passed Pointer---; 
    //So that Fun_Ptr points to whatever ---Passed Pointer points to

Between the *Pointer and &Pointer notations, I am very confused. I know that *Pointer means give whatever it points to.

Do I put void (int *pointer) in the declaration. What about when I use the function?

Your assistance is appreciated.

EDIT 2:

Okay, I now understand that using *variable in a declaration means that a pointer will be passed. However, what about when i use the function?

i.e.

int main(){
    int foo;
    int *bar;
    bar = foo;
    Fun(bar);
}

EDIT 3:
Okay, so correct me if I am wrong:

According to the conventions of the above code:

bar = &foo means: Make bar point to foo in memory

*bar = foo means Change the value that bar points to to equal whatever foo equals

If I have a second pointer (int *oof), then:

bar = oof means: bar points to the oof pointer

bar = *oof means: bar points to the value that oof points to, but not to the oof pointer itself

*bar = *oof means: change the value that bar points to to the value that oof points to

&bar = &oof means: change the memory address that bar points to be the same as the memory address that oof points to

Do I have this right?

EDIT 4: Thanks so much for all your help (I wish I could accept more than 1 answer, but I have to go with the first one. I am not sure how a community wiki works exactly, but I will leave it this way for editing (feel free to turn it into a ref guide if you like).

Best Answer

There is a difference in the * usage when you are defining a variable and when you are using it.

In declaration,

int *myVariable;

Means a pointer to an integer data type. In usage however,

*myVariable = 3;

Means dereference the pointer and make the structure it is pointing at equal to three, rather then make the pointer equal to the memory address 0x 0003.

So in your function, you want to do this:

void makePointerEqualSomething(int* pInteger)
{
    *pInteger = 7;
}

In the function declaration, * means you are passing a pointer, but in its actual code body * means you are accessing what the pointer is pointing at.

In an attempt to wave away any confusion you have, I'll briefly go into the ampersand (&)

& means get the address of something, its exact location in the computers memory, so

 int & myVariable;

In a declaration means the address of an integer, or a pointer!

This however

int someData;    
pInteger = &someData;

Means make the pInteger pointer itself (remember, pointers are just memory addresses of what they point at) equal to the address of 'someData' - so now pInteger will point at some data, and can be used to access it when you deference it:

*pInteger += 9000;

Does this make sense to you? Is there anything else that you find confusing?

@Edit3:

Nearly correct, except for three statements

bar = *oof;

means that the bar pointer is equal to an integer, not what bar points at, which is invalid.

&bar = &oof;

The ampersand is like a function, once it returns a memory address you cannot modify where it came from. Just like this code:

returnThisInt("72") = 86; 

Is invalid, so is yours.

Finally,

bar = oof

Does not mean that "bar points to the oof pointer." Rather, this means that bar points to the address that oof points to, so bar points to whatever foo is pointing at - not bar points to foo which points to oof.