Copying by Reference

I am trying to do an exercise in Stroustrup's book. The task is to create two arrays (with different values) and pointers to each array, and copy the first array to the second using the pointer and reference operators. My code is below.
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
#include "../../std_lib_facilities.h"

int main ()
{
	int p0[10] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
	int* p1 = &p0[0];

	int q0[10] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
	int* q1 = &q0[0];

	for (int i = 0; i < 10; ++i) {
		p0[i] = 2 * (i + 1);
	}

	for (int i = 0; i < 10; ++i) {
		cout <<"Array place " << i << " has value " << p0[i] << ".\n";
	}
	cout << "  \n";
	for (int i = 0; i < 10; ++i) {
		cout <<"Array place " << i << " has value " << q0[i] << ".\n";
	}

	cout << "  \n";
	cout << "  \n";

	q1 = p1;
	q0[0] = *q1;

	for (int i = 0; i < 10; ++i) {
		cout <<"Array place " << i << " has value " << q0[i] << ".\n";
	}

	keep_window_open();
        return 0;
}   


The program compiles and runs. BUT the output is correct only for q[0], the first element of the second array. The values of the other array elements remain unchanged. I don't know if I think I am supposed to do is even possible. I thought the rest of the array would copy automatically after pointing to the first element (line 27). Thanks in advance for your help.
1
2
q1 = p1; // q1 and p1 now point to p0[0]
q0[0] = *q1; // this just says copy value of p0[0] to q0[0] 


if your goal is to copy values of p0 into q0, you need a loop
1
2
for(int i=0;i<10;++i)
       q0[i] = p0[i];


incidentally, int* p1 = &p0[0] is same as int* p1 = p0 . You can try cout<<p0<<endl; and cout<<&p0[0]<<endl; . You should get the same value on the screen.
Last edited on
Thanks for your reply. Your suggestion is a direct copy 'by value'. I believe we are supposed to copy 'by reference'. I followed someone else's suggestion and used a pointer array. The program ran and yielded the desired results. I just don't think my program was what Stroustrup was intending for us to write. The code is below. Constructive comments are very welcome.
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
#include "../../std_lib_facilities.h"

int main ()
{
	int p0[10] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
	int* p1[10] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};

	int q0[10] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
	int* q1[10] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};

	for (int i = 0; i < 10; ++i) {
		p0[i] = 2 * (i + 1);
		p1[i] = &p0[i];
	}

	for (int i = 0; i < 10; ++i) {
		cout <<"Array place " << i << " has value " << p0[i] << ".\n";
	}
	cout << "  \n";
	for (int i = 0; i < 10; ++i) {
		cout <<"Array place " << i << " has value " << q0[i] << ".\n";
	}

	cout << "  \n";
	cout << "  \n";

	for (int i = 0; i < 10; ++i) {
		q1[i] = p1[i];
     	q0[i] = *q1[i];
	}

	for (int i = 0; i < 10; ++i) {
		cout <<"Array place " << i << " has value " << q0[i] << ".\n";
	}

	keep_window_open();
    return 0;
}   
Last edited on
If you have an array which you want to copy into another, meaning a separate block of memory, copy by value is unavoidable, whether you do it directly or indirectly. The way you have implemented is strange. you might as well have done the following if you insist on not using the array p0

1
2
3
int *q1 = p0;
for(int i=0;i<10;++)
       q0[i] = q1[i];


If you don't think this is not what was expected, why not post the question verbatim.
you don't need a pointer array, since the array is already a pointer, a constant pointer, etc...

and instead of switching the indice in the pointer array to equal the intended address of the specific array

you can do that with a simple pointer

1
2
3
4
int array[20];
int * p = array; // will go automatically to the first pointer in the array which is 0
p++; //will go to the next one which is 1
Last edited on
Thank you, slack. Your suggestion of using p++ to advance the pointer worked. After reading more on pointers, I think I understand that incrementing a pointer increases the address it points to by one unit. Since the array addresses are consecutive, the incremented pointer then points to the next array element. I hope that is the correct understanding.

My adjusted code is the following.
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
#include "../../std_lib_facilities.h"

int main ()
{
	int p0[10] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
	int* p1;

	int q0[10] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};

	for (int i = 0; i < 10; ++i) {
		p0[i] = 2 * (i + 1);
	}
	p1 = &p0[0];


	for (int i = 0; i < 10; ++i) {
		cout <<"Array place " << i << " has value " << p0[i] << ".\n";
	}
	cout << "  \n";
	for (int i = 0; i < 10; ++i) {
		cout <<"Array place " << i << " has value " << q0[i] << ".\n";
	}

	cout << "  \n";
	cout << "  \n";

	for (int i = 0; i < 10; ++i) {
		q0[i] = *p1;
		p1++;
	}

	for (int i = 0; i < 10; ++i) {
		cout <<"Array place " << i << " has value " << q0[i] << ".\n";
	}

	keep_window_open();
       return 0;
}   


The verbatim question is:
Allocate an array of 10 ints; initialize it to 1, 2, 4, 8, etc.; and assign its address to a variable p1.
Allocate an array of 10 ints, and assign its address to a variable p2. (I used q1.)
Copy the values from the array pointed to by p1 into the array pointed to by p2 (q1).

I realize that what I did above was not what the question asked for because I had not allocated any of the free store (heap) memory. I have new code below (that compiles and runs correctly) but I am still unsure of what kind of code Stroupstrup wants to elicit from us with this question.
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
#include "../../std_lib_facilities.h"

int main ()
{
    int* p1 = new int[10];

    int* q1 = new int[10];

    for (int i = 0; i < 10; ++i) {
        p1[i] = 2 * (i + 1);
    }
    for (int i = 0; i < 10; ++i) {
        q1[i] = 3 * (i + 1);
    }


    for (int i = 0; i < 10; ++i) {
        cout <<"Array place " << i << " has value " << p1[i] << ".\n";
    }
    cout << "  \n";
    for (int i = 0; i < 10; ++i) {
        cout <<"Array place " << i << " has value " << q1[i] << ".\n";
    }

    cout << "  \n";
    cout << "  \n";

    for (int i = 0; i < 10; ++i) {
        q1[i] = p1[i];
    }

    for (int i = 0; i < 10; ++i) {
        cout <<"Array place " << i << " has value " << q1[i] << ".\n";
    }

    keep_window_open();
    return 0;
}   


Thanks for your feedback. I am learning on my own and not having a professor to ask questions of and discuss things with is a serious drawback. This forum and you posters take the place of a professor.
"Allocate" doesn't imply you have to use new or new[]. If you create an array on the stack, the memory will automatically be allocated for you.
Your previous code was correct, however now you're creating arrays using new[] without ever deleting them.
Topic archived. No new replies allowed.