0

As a beginner in C++, first thing I came accross for functions is that they use a copy of the argument, for example if we execute this :

void add (double x){
  x=x+3;
}

int main(){
double x=2 ; 
add(x) ;
}

Then x would actually be equal to 2, not 5. Later on, learning about pointers, I found the following :

void fill (int *t){
  for (int j=0, j<10, j++){
  t[j]=j;
}

int main(){
int* p;
p = new int[10];
fill(p);
}

Now if we print what p contains, we find that it's indeed filled by the integers. I have some trouble understanding why it does that as I feel like it should have been the same as the first function ?

Thanks.

10
  • 5
    The argument itself is copied, but when you copy a pointer, the copied pointer still points to the original object, which means that when you modify the object being pointed to, you will see the change reflected when dereferencing the original pointer as well. This is a common way of creating a function that can modify an existing object (although in modern C++ the use of raw pointers is discouraged, with references or STL smart pointers being preferred) Commented Apr 28, 2018 at 13:04
  • 2
    You still pass the pointer by values. You don't modify the argument t itself, you modify the data it points to. Commented Apr 28, 2018 at 13:04
  • This kind of thing is much clearer if you give different variables different names. Saying "x would actually be equal to 2" isn't accurate; the x in main would be 2, but the x in add would be 5 (before it goes away). Commented Apr 28, 2018 at 13:06
  • A pointer is not an array. When you talk about what "p contains" you have to be very careful: p is a pointer, and it contains a memory address; nothing more. Your function fill treats that memory address as the first element in an array; that's okay, because that's what the code does, but p does not contain an array; it points to an array. Commented Apr 28, 2018 at 13:09
  • To get add to modify x, define the parameter as a reference (i.e. an alias): void add (double& y){ y=y+3; } Commented Apr 28, 2018 at 13:11

2 Answers 2

1

The reason it isn't the same as the first function is because you are passing the pointer by value. This means that if you modify the actual pointer, e.g by assigning to it, then it would only be in that state inside the function. The value that the pointer points to is still the original value, which will get modified since both copied pointers point to that same original value (don't forget notation of the form a[i] is equivalent to *(a + i), which does a dereference and is modifying the pointed value, not the pointer itself).

A small example that illustrates this would be the following (not accounting for memory leaks):

#include <iostream>

int test(int* x)
{
    int* y = new int{10};
    x = y;
    std::cout << "Inside function: " << *x << "\n";
}

int main()
{
    int* t = new int{5};
    std::cout << "Before function: " << *t << "\n";
    test(t);
    std::cout << "After function: " << *t << "\n";
}
Sign up to request clarification or add additional context in comments.

Comments

0

In first example you are using ordinary variable. When passing normal variable to function, like this, the function creates its own copy of the variable (it has same value as it has when you passed it, but it was copied to different place in memory). That is standard behaviour.

In second example you are using pointer: we can say that it points to the place in memory, where values are stored. Few of the advantages of them:

1) If you want to spare memory, but need to use same value in different functions -> mostly appplies to bigger objects than double, like array in your example

2) If you need to change value of variable/array/object in different functions

But careful, in the second function you still created copy, but not of value, but pointer. So basically, the "new" pointer is different object, but it is pointing to the same place in memory, so when accessing the value (which you are doing with [j]), you are editing the same place in memory.

It is not that easy concept to grasp, especially with more dimentional arrays, but hope this helped a little. You can learn some more in tutorials or c++ docs, for example this is a good one: https://www.geeksforgeeks.org/pointers-in-c-and-c-set-1-introduction-arithmetic-and-array/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.