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.

#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.

q0[0] is an address + 0x00 (a base address, really), and what it points to is just an int. An int does not have enough space for 10 ints.

You'll need to memcpy if you actually want to copy the data.

Though, I'm not sure my solution is what the book is telling you to do, so lets back up a second.

You have made 2 distinct arrays (although one of them only contains 0 for each element), but you know, you can use the pointer q1 like this:

q1[0];

Don't you think it might be hinting for you to do that inside of a function, where you pass q1? You could also pass the address (by reference) of the beginning of q0 (&q0[0])

Good luck!

I took your 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.

#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;
}

Edited 6 Years Ago by Nathaniel10: n/a

This article has been dead for over six months. Start a new discussion instead.