This is very difficult

I tried to know how the computer is calculation expression "* (a + I) [j]". I wrote the following program. But I still don't know why. I hope you to help me to explain this phenomenon. Thank you! (PS: we Chinese QQ (equivalent to MSN) are not solve this problem!)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream.h>
void main(){
	int a[3][3] = {
		1,2,3,
		4,5,6,
		7,8,9};
	int i = 1,j = 1;
	int (*m1)[3]=a+i;
	int* m2 = m1[j];
	int b,c;
	for(b=0;b<3;b++)
	{
		for(c=0;c<3;c++)
		cout<<"&a["<<b<<"]["<<c<<"] address="<<&a[b][c]<<"( data="<<a[b][c]<<")"<<endl;
	}
	cout<<"sizeof int="<<sizeof(int)<<endl
		<<"int (*m1)[3]=a+i;int* m2 = m1[j];"<<endl
		<<"m1 address="<<m1<<endl
		<<"m1 data="<<*m1<<endl
		<<"m2 address="<<m2<<endl
		<<"m2 data="<<*m2<<endl;
}
Uh does this even compile??
No namespace
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
#include <iostream>

using namespace std;

int main(){
	int a[3][3] = {
		1,2,3,
		4,5,6,
		7,8,9 };
	int i = 1,j = 1;
	int (*m1)[3]=a+i;
	int* m2 = m1[j];
	int b,c;
	for(b=0;b<3;b++)
	{
		for(c=0;c<3;c++)
		cout<<"&a["<<b<<"]["<<c<<"] address="<<&a[b][c]<<"( data="<<a[b][c]<<")"<<endl;
	}
	cout<<"sizeof int="<<sizeof(int)<<endl
		<<"int (*m1)[3]=a+i;int* m2 = m1[j];"<<endl
		<<"m1 address="<<m1<<endl
		<<"m1 data="<<*m1<<endl
		<<"m2 address="<<m2<<endl
		<<"m2 data="<<*m2<<endl;
		cin.get();
}


Worked but I dunno what you are up to!?!
It's probably the worst written code I've seen in awhile.
He is using an old compiler (notice the #include <iostream.h> directive)

Even GCC will let the intialisation of the array without dimension brackest and the void main() through with just warnings.

but make the couple of relevant changes

1
2
3
4
5
6
7
8
9
#include <iostream> //use a uptodate compiler
using namespace std; //modern compiler

int main() //not void main

int a[3][3] = { //correct bracketing for the array
    {1,2,3},
    {4,5,6},
    {7,8,9} };


and it will run fine.

I'm not meaning code has a problem. At least I compiler successfully compile the code. I just want to know how the computer is calculated * (a + I) [j].
There is no "* (a + I) [j]" anywhere in this program. If you are asking about the fragment
1
2
3
    int i = 1,j = 1;
    int (*m1)[3]=a+i;
    int* m2 = m1[j];

then this is what happens:

First, "a+i" is evaluated. a is an array, i is an int.
There is no operator+ that takes an array on the left and an int on the right, so the compiler considers implicit conversions.... Long story short
1) it performs array-to-pointer conversion: 'a' becomes '&a[0]', that is, a new pointer is created that points at the first row of a.
2) it adds i to that pointer. Since i equals 1, this creates a pointer to the second row of a.
3) That pointer is stored in the object called "m1".
4) To evaluate m1[j], it evaluates *(m1 + j), by definition of [].
5) Since j is also 1, the pointer that results from m1+j is pointing at the third row of a.
6) Applying unary * to that pointer, you get the third row of a (that is, an array of 3 int with values {7, 8, 9}
7) Now the code is attempting to initialize a pointer called m2 using the array as the initializer. An array is not a pointer, so this does not work, but again, there is a conversion from array to pointer to its first element, so m1 gets the address of the first element of the third row of a. When you print out *m2, you access that element, which is 7.

Last edited on
I don't even understand what he says
Thangdo wrote:
I don't even understand what he says


This is just the usual question about the interchangebility between arrays and pointers.
It comes up so often enough that I believe Disch or another member has written an article about it.
thank you.
Last edited on
Topic archived. No new replies allowed.