On 26 Dec 2005 20:34:26 -0800, "smartbeginner" <rr******@gmail.com>
wrote:
I got from a book a 2D array(a[i][j] is resolved by compiler as
a[i][j]=*(&a[0][0]+total_columns*i+j); // ->1
[Stephen Holzners Assembly Language with C]
And I know when I use pointer to array I should refer
a[i][j]=*(*(a+i)+j); ->2
You may think you know this but you had it wrong in your previous
message thread.
Is this also resolved by the compiler to (->1) (Awkward question I
know)
And my main question is why we use *(a+i) here
In the pointer to array notation
a[i][j]=*(a[i]+j);
& this again resolves to
a[i][j]=*(*(a+i)+j);
which is same as (2)
Then whats the need of both array of pointers and pointer to array
Is this makes compilers work easy
What is the need of integer and float? The answer to both questions
is they do different things and part of your design process is to
choose the object type that works best for your needs and code
accordingly.
There are four key points:
If is a is an array and p is a pointer, then a[i] and p[i] both
designate the i-th object in the collection (assuming it exists).
In most expressions, a term with type array evaluates to the
address of the first element of the array with type pointer to
element.
The [ ] operator associates left to right. This means that
a[i][j] is treated as (a[i])[j], a[i][j][k] is treated as
((a[i])[j])[k], etc.
The language standard states that a[i] is equivalent to *(a+i).
Note that a+i is an expression in pointer arithmetic, not integer
arithmetic, as explained in my response to your previous message
thread. This makes intuitive sense since a+i is the address of the
i-th object and *(a+i) dereferences this address to yield the object
itself, just as described in the first paragraph above.
Chaining these rules together in the correct sequence and applying
them recursively where needed, keeping track of the type of each
expression, will show that a[i][j] always evaluates to *(*(a+i)+j).
This works if a is an array, a pointer to an array, or a pointer to
pointer.
<<Remove the del for email>>