C programming allows passing a pointer to a function. To do so, simply declare the function parameter as a pointer type.
Example 2: Passing Pointers to Functions Here, the value stored at p , *p , is 10 initially. We then passed the pointer p to the addOne() function. The ptr pointer gets this address in the addOne() function. Inside the function, we increased the value stored at ptr by 1 using (*ptr)++; .
There are three ways to pass variables to a function – pass by value, pass by pointer and pass by reference.
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.
To declare a function that takes a pointer to an int:
void Foo(int *x);
To use this function:
int x = 4;
int *x_ptr = &x;
Foo(x_ptr);
Foo(&x);
If you want a pointer for another type of object, it's much the same:
void Foo(Object *o);
But, you may prefer to use references. They are somewhat less confusing than pointers:
// pass a reference
void Foo(int &x)
{
x = 2;
}
//pass a pointer
void Foo_p(int *p)
{
*x = 9;
}
// pass by value
void Bar(int x)
{
x = 7;
}
int x = 4;
Foo(x); // x now equals 2.
Foo_p(&x); // x now equals 9.
Bar(x); // x still equals 9.
With references, you still get to change the x that was passed to the function (as you would with a pointer), but you don't have to worry about dereferencing or address of operations.
As recommended by others, check out the C++FAQLite. It's an excellent resource for this.
Edit 3 response:
bar = &foo means: Make bar point to foo in memory
Yes.
*bar = foo means Change the value that bar points to to equal whatever foo equals
Yes.
If I have a second pointer (int *oof), then:
bar = oof means: bar points to the oof pointer
bar will point to whatever oof points to. They will both point to the same thing.
bar = *oof means: bar points to the value that oof points to, but not to the oof pointer itself
No. You can't do this (assuming bar is of type int *) You can make pointer pointers. (int **), but let's not get into that... You cannot assign a pointer to an int (well, you can, but that's a detail that isn't in line with the discussion).
*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
No. You can't do this because the address of operator returns an rvalue. Basically, that means you can't assign something to it.
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;
}
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
.
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With