I am trying to do an exercise from Stroustup's book. The task is to create an array of 7 ints, assign its address to another variable, then print out the array value and the address. My code is below.

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


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

	for (int i = 0; i < 10; ++i) {
		cout <<"Array place " << i << " has value " << p0[i] << ", and its address is " << p1 << ".\n";
	}

        keep_window_open();
        return 0;
}

My problem is that I get a compile error on line 4 saying that "a value of type int(*)[7] cannot be used to initialize an entity of type int*". I don't understand that message nor how to correct the problem. Thanks in advance for your help.

int *p1 = p0[0];

Use either

int* p1 = &p0[0];

or

int* p1 = p0;

Thank you, guys. I tried

int* p1 = &p0[0];

and it worked fine. Another poster explained to me that I had defined an array in line 3 (p0[7]) but set a pointer to an int in line 4 (&p0). I had to add the subscript to turm p0 back into an array.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.