Reputation: 9634
I want to clarify something about pointers as it is something that I think is difficult in C++. When I declare a pointer and pass it to a function like:
//just an example
void showInt(int* numbers)
{
numbers += 3;
}
int main()
{
int* a = 10;
showInt(a);
return 0;
}
When I pass the variable a
to the function, am I actually passing the original variable to it? Or is it creating a copy of the variable and then passing that to the function?
How do I know if I'm passing a copy or a real/original one?
Upvotes: 0
Views: 587
Reputation:
You can't do
int* a = 10
It makes no sense and your compiler will tell you about error.
When you do this:
//just an example
void showInt(int* numbers)
{
numbers += 3;
}
int main()
{
int a = 10;
showInt(&a);
return 0;
}
There you pass an adress of a and then add 3 to this adress, so nothing changes.
If you change:
numbers += 3;
to
*numbers += 3;
then you'll modify value of variable a.
There's one more method to do this: just change
void showInt(int* numbers)
to
void showInt(int& numbers)
So then you can use
showInt(a);
and you'll modify value of a, and copy won't be created.
Upvotes: 3
Reputation: 9536
Let's go through your example:
void showInt(int* numbers) // (1)
{
numbers += 3; // (2)
}
int main()
{
int* a = 10; // (3)
showInt(a); // (4)
return 0;
}
(1) numbers
variable is a copy of variable passed to this function;
(2) because of (1) all changes made on numbers
here will remain only within this function; value of the original variable passed to this function will remain the same! But please note the following: you cannot change the value of the pointer (a
) passed to the function but through the copy of that pointer (which is numbers
) you can change the value it points to!
That's the main trick - once you have the address inside the function, you can write at that address and those changes remain after function return. Writing at the address includes pointer dereferencing but you luckily didn't do that - I say 'luckily' because the address you passed was just an arbitrary address in the memory (10). If you tried to write at the address 10 your program would likely crash.
(3) You've declared a
to be of type "pointer to int" so it's value is supposed to be an address of some int
object. You made a dangerous mistake here as you assumed 10 was a valid address of some int but you don't actually know what's at that address.
(4) You're passing copy of variable a
to the function. Function stores its value in numbers
variable and increases it. So numbers
contains now address 13 (not the address of some integer variable which value is 13!). a
remains the same though and it still has the same value, 10.
You probably wanted something like this:
void showInt(int* numbers)
{
*numbers += 3; // (1)
}
int main()
{
int a = 10; // (2)
showInt(&a); // (3)
return 0;
}
(1) function modifies value at the address kept in numbers
. Pointer is dereferenced.
(2) We need to have a valid address of some int variable, not just randomly picked address (like 10). Therefore we declare int variable, a
. Its address is &a
.
(3) Address of a
is passed to showInt
through variable numbers
and now function can write to that address (which is now a valid address of int object - a
) and so modify value of a
. When function returns, a
has value of 13. Note that the address of a
is not changed at any point of time here.
Upvotes: 0
Reputation: 7230
With a pointer your are passing a copy of the pointer as parameter, the pointer points to the original variable. So if you dereference the pointer you will have the original value. Remember a pointer is basically an address of something, an int *
is the address of an integer, if you change the value of the pointer, you are changing the address, which means that the pointer will point to something else, if you change the value of what the pointer points to, you will change the value while pointing to the same object.
void ptrfunc(int *a)
{
*a = 10;
}
void reffunc(int &a)
{
a = 50;
}
void valfunc(int a)
{
a = 30;
}
int main()
{
int b = 20;
// Pass the point to b to the function, will alter the original.
ptrfunc(&b);
cout << b << endl;
// Pass a reference to b to the function, will alter the original.
ptrfunc(b);
cout << b << endl;
// Pass the value of b to the function, will not alter the original.
valfunc(b);
cout << b << endl;
return 0;
}
This function will print the value 10 int the first cout, and 50 in the next since the value of the object pointed to and referenced are changed in the function. The valfunc changes the value of a copy of b, and thus not the original.
Upvotes: 0
Reputation: 42023
a
refers to the pointer, which points at a value of 10
. If you want to pass the value 10
to the method, use *a
. As it stands, you pass a memory location to the top method. In that method you increment it by 3.
There's no 'copy' and 'original' per se: in main()
, the 10
exists in memory, and a
holds a pointer to it. In the other method [with no name?] it takes an int
- not a pointer - so if you modify it you're not changing the target of the pointer, just the parameter (which acts like a local variable).
Hope that helps in some way.
[Edit: you've changed the top method to take an int*
parameter instead of int
now. That makes more sense.. let me revise my answer]
First I want to clarify, no objects are copied in your code (actually there are no real objects in the code at all, just an int in memory and a pointer passed around and possibly modified).
showInt
now takes a pointer, correctly. It increments that pointer, in this case pointing it at something undefined/invalid. If you do numbers += 3
then numbers will simply point at something 3 bytes on from where it originally pointer, that's all that changed. And that's only within the scope of that method.
If you did: *numbers += 3
then you'd be incrementing the target of the pointer, which means replace the 10
with 13
. That would be in effect wherever that memory is accessed, i.e. in main
.
Can I suggest you do some reading into the benefits of pointers vs references (e.g. int&
) - with a decent article you should have a fairly decent 'aha' moment :)
Upvotes: 0