views:

122

answers:

7

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

A: 

It might be easier for you to understand using Functionoids which are expressively neater and more powerful to use, see this excellent and highly recommended C++ FAQ lite, in particular, look at section 33.12 onwards, but nonetheless, read it from the start of that section to gain a grasp and understanding of it.

To answer your question:

typedef void (*foobar)() fubarfn;

void Fun(fubarfn& baz){
   fubarfn = baz;
   baz();
}

Edit:

  • & means the reference address
  • * means the value of what's contained at the reference address, called de-referencing

So using the reference, example below, shows that we are passing in a parameter, and directly modify it.

void FunByRef(int& iPtr){
    iPtr = 2;
}

int main(void){
    // ...
    int n;
    FunByRef(n);
    cout << n << endl; // n will have value of 2
}
tommieb75
Maybe I should clarify. I am not trying to use a pointer to a function, but pass a pointer to some object (say an int) to the function, so the function can operate on it. I will revise my question to clear this up.
Biosci3c
A: 
void Fun(int *Pointer)
{
  //if you want to manipulate the content of the pointer:
  *Pointer=10;
  //Here we are changing the contents of Pointer to 10
}

* before the pointer means the content of the pointer (except in declarations!)

& before the pointer (or any variable) means the address

EDIT:

int someint=15;
//to call the function
Fun(&someint);
//or we can also do
int *ptr;
ptr=&someint;
Fun(ptr);
vash47
That's C syntax when the OP clearly specified C++....
tommieb75
@tommieb75: why does it matter that it is valid C? It's also C++ syntax, and the questioner did not specify "C++, with C removed". Whatever that is.
Steve Jessop
+1  A: 

To pass a pointer to an int it should be void Fun(int* pointer).

Passing a reference to an int would look like this...

void Fun(int& ref) {
   ref = 10;
}

int main() {
   int test = 5;
   cout << test << endl;  // prints 5

   Fun(test);
   cout << test << endl;  // prints 10 because Fun modified the value

   return 1;
}
dgnorton
+1  A: 

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

Tomas Cokis
Okay, so does that mean that pInteger pointer points to the someData pointer?
Biosci3c
Tomas Cokis
Okay, I think I have it somewhat down (pointers always confused me, and I am really trying hard to understand them). I think I have a fairly good grasp of it now.
Biosci3c
It takes a while to get used to them, you'll get their
Tomas Cokis
+1  A: 

If you want to pass a pointer-to-int into your function,

Declaration of function (if you need it):

void Fun(int *ptr);

Definition of function:

void Fun(int *ptr) {
    int *other_pointer = ptr;  // other_pointer points to the same thing as ptr
    *other_ptr = 3;            // manipulate the thing they both point to
}

Use of function:

int main() {
    int x = 2;
    printf("%d\n", x);
    Fun(&x);
    printf("%d\n", x);
}

Note as a general rule, that variables called Ptr or Pointer should never have type int, which is what you have then in your code. A pointer-to-int has type int *.

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

bar = oof means: bar points to the oof pointer

It means "make bar point to the same thing oof points to".

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

That doesn't mean anything, it's invalid. bar is a pointer *oof is an int. You can't assign one to the other.

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

Yes.

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

Nope, that's invalid again. &bar is a pointer to the bar variable, but it is what's called an "rvalue", or "temporary", and it cannot be assigned to. It's like the result of an arithmetic calculation. You can't write x + 1 = 5.

It might help you to think of pointers as addresses. bar = oof means "make bar, which is an address, equal to oof, which is also an address". bar = &foo means "make bar, which is an address, equal to the address of foo". If bar = *oof meant anything, it would mean "make bar, which is an address, equal to *oof, which is an int". You can't.

Then, & is the address-of operator. It means "the address of the operand", so &foo is the address of foo (i.e, a pointer to foo). * is the dereference operator. It means "the thing at the address given by the operand". So having done bar = &foo, *bar is foo.

Steve Jessop
+1  A: 
void Fun(int* Pointer)   -- would be called as Fun( &somevariable )

would allow you to manipulate the content of what 'Pointer' points to by dereferencing it inside the Fun function i.e.

*Pointer = 1;

declaring it as above also allows you also to manipulate data beyond what it points to:

int foo[10] = {0};
Fun(foo);

in the function you can then do like *(Pointer + 1) = 12; setting the array's 2nd value.

void Fun(int& Pointer)  -- would be called Fun( somevariable )

you can modify what Pointer references to, however in this case you cannot access anything beyond what Pointer references to.

Anders K.
+1  A: 
JoshD