S
Srinu
Hi All,
We know the following facts,
1. A two dimentional arrays in C is stored similar to a single
dimentional array.
2. * is the dereference operator that gives value at address denoted
by the operand to this operator. i.e *p is the value at address given
by p.
3. In case of two dimentional array a[][], a+1 refers to the second
row, and a+3 refers to the fourth row.
4. *(derefernce) operator has higher precedence that + operator.
So lets look at the following program...
1 #include<stdio.h>
2 int main()
3 {
4 int p[5][5];
5 p[0][0]=4;
6 p[2][0]=5;
7 p[2][3]=6;
8 printf("\n%d", *(*(p+2)+3));
9 printf("\n%d", *(*(p+2)));
10 printf("\n%d", *(*p+0));
11 }
Out put of this program compiled in gcc 3.4.2 is
6
5
4
Discussion on line number 8 :
-------------------------------------------
In expression *(*(p+2)+3)), p is the base address and points to p[0]
[0]; according to fact 3, (p+2) refers to third rowof p, i.e. p[2][0].
According to fact 2, * operator on this should give the value at
address denoted by p[2][0], so *(p+2) should be 5; then again +3, that
is, total is 8 and the first * on this...so it should not give the
appropriate result.
But it gives !
So, what is that second * in the expression *(*(p+2)+3)) doing?
Does the meaning of this second * is "value at..." or something else?
[The other two lines numbered 9, 10 in the program is just for
reference]
With regards.
Srinu
We know the following facts,
1. A two dimentional arrays in C is stored similar to a single
dimentional array.
2. * is the dereference operator that gives value at address denoted
by the operand to this operator. i.e *p is the value at address given
by p.
3. In case of two dimentional array a[][], a+1 refers to the second
row, and a+3 refers to the fourth row.
4. *(derefernce) operator has higher precedence that + operator.
So lets look at the following program...
1 #include<stdio.h>
2 int main()
3 {
4 int p[5][5];
5 p[0][0]=4;
6 p[2][0]=5;
7 p[2][3]=6;
8 printf("\n%d", *(*(p+2)+3));
9 printf("\n%d", *(*(p+2)));
10 printf("\n%d", *(*p+0));
11 }
Out put of this program compiled in gcc 3.4.2 is
6
5
4
Discussion on line number 8 :
-------------------------------------------
In expression *(*(p+2)+3)), p is the base address and points to p[0]
[0]; according to fact 3, (p+2) refers to third rowof p, i.e. p[2][0].
According to fact 2, * operator on this should give the value at
address denoted by p[2][0], so *(p+2) should be 5; then again +3, that
is, total is 8 and the first * on this...so it should not give the
appropriate result.
But it gives !
So, what is that second * in the expression *(*(p+2)+3)) doing?
Does the meaning of this second * is "value at..." or something else?
[The other two lines numbered 9, 10 in the program is just for
reference]
With regards.
Srinu