The value isn't being changed in int main despite passing of array as pointer

Ok, so I am basically making a function that resizes the size of the array at run time. Please see the following code, it is working perfectly.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <iostream>
#include <string>
using namespace std;

int* resize(int *p, int &size)
{
	int *temp = new int[size + 1];
	for (int i = 0; i < size; i++)
		temp[i] = p[i]; 
	size++;
	delete[]p;
	return temp; 
}

int main()
{
	int *p = NULL;
	bool fh = true; 
	int size = 0; 
	while (fh)
	{
		cout << "Enter Number: "; 
		p = resize(p, size); 
		cin >> p[size-1]; 
		cin.ignore(); 
		if (p[size - 1] == -1)
			fh = false; 
	}
	cout << endl << endl;
	for (int i = 0; i < size; i++)
		cout << p[i] << " ";
	getchar();
	return 0;
}


Now, in the above code, I returned the new formed array (with increased size)
Please see the following code now,


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <iostream>
#include <string>
using namespace std;

void resize(int *p, int &size)
{
	int *temp = new int[size + 1];
	for (int i = 0; i < size; i++)
		temp[i] = p[i]; 
	size++;
	delete[]p;
	p = temp; 
}

int main()
{
	int *p = NULL;
	bool fh = true; 
	int size = 0; 
	while (fh)
	{
		cout << "Enter Number: "; 
		resize(p, size); 
		cin >> p[size-1]; 
		cin.ignore(); 
		if (p[size - 1] == -1)
			fh = false; 
	}
	cout << endl << endl;
	for (int i = 0; i < size; i++)
		cout << p[i] << " ";
	getchar();
	return 0;
}


This above code is exactly similar to the last one, however there is no returning of pointer from function and instead I am assigning the new array's pointer to my original pointer within that function. But it is giving error, apparently because it can't access that memory...
The problem here is, from what I understand, whenever you pass an argument by "pointer" (like I did with the array here), the argument variable gets updated within that function.
Considering the same concept, this second code also should work and the value of the original pointer (i.e. p) should get updated, but it isn't doing that. Can I know what's the issue here?
There are two ways to pass a parameter to function: by value and by reference. The by value creates a copy. The reference is a ... reference.

Lets start easy:
1
2
3
4
int x = 42;
int y = x; // y is a copy of x
y = 7;
// did the x change? 

again:
1
2
3
4
5
int z = 42;
int* px = &z; // px points to z
int* py = x; // py points to z
*py = 7; // did the z change?
py = nullptr; // did the px change? 

Function's parameter p is like the py. It is a copy of caller's variable.
You can modify what the pointer points to (z), but you won't modify other pointers.

If you want to modify the caller's pointer, then pass by reference:
void resize( int* &p, int &size );
Let me come up with an example (I hope it helps):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <iostream>


int* doSomething( int* ptr );


int main()
{
    int i { 13 };
    int* p1 { &i };
    std::cout << "Inside main()\n-------------\n"
                 "p1 has address "       << (long long)&p1
              << " and it's pointing to " << (long long)p1
              << " where is stored "      << *p1
              << "\n\n";

    auto* p2 { doSomething(p1) };

    std::cout << "Inside main()\n-------------\n"
                 "Here p1 has address "       << (long long)&p1
              << " and it's still pointing to " << (long long)p1
              << " where is stored "      << *p1
              << "\n\n";

    std::cout << "Inside main()\n-------------\n"
                 "p2 has address "       << (long long)&p2
              << " and it's pointing to " << (long long)p2
              << " where is stored "      << *p2
              << "\n\n";
}

int* doSomething( int* ptr )
{
    std::cout << "Inside doSomething()\n--------------------\n"
                 "ptr has address "       << (long long)&ptr
              << " and it's pointing to " << (long long)ptr
              << " where is stored "      << *ptr
              << "\n\n";

    ptr = new int { 666 };
    std::cout << "Inside doSomething()\n--------------------\n"
                 "Now ptr has still address "       << (long long)&ptr
              << " but it's pointing to " << (long long)ptr
              << " where is stored "      << *ptr
              << "\n\n";

    return ptr;
}


Output:
Inside main()
-------------
p1 has address 6422032 and it's pointing to 6422044 where is stored 13

Inside doSomething()
--------------------
ptr has address 6421984 and it's pointing to 6422044 where is stored 13

Inside doSomething()
--------------------
Now ptr has still address 6421984 but it's pointing to 17504208 where is stored 666

Inside main()
-------------
Here p1 has address 6422032 and it's still pointing to 6422044 where is stored 13

Inside main()
-------------
p2 has address 6422024 and it's pointing to 17504208 where is stored 666

Topic archived. No new replies allowed.