Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using pointers with multidimensional Arrays

Status
Not open for further replies.

DazedNConfused

Programmer
Mar 18, 2002
8
US
I'm having trouble in understanding this.For example,
If we have an array BEANS[j]:

*(*(beans+i)+j)-why does *(beans+i)refer to an address of the element and not it's value?Afterall,'*'is INDIRECTION operator.Why does only the outmost '*' behave like indirection operator?

I have another question.This two expresions are the same:

*(beans+j)-this one I understand
(*(beans+i))[j]-why is indirection operator in parentheses,shouldn't it be just the other way:
*((beans+i)[j])

Thank you for helping me out,so I can finaly skip the confused part in my name
 
beans is identical to *(beans+i). Thus

beans[j] or *(beans+j) or *(*(beans+i)+j) or (*(beans+i))[j]

are all equivalent.

Hope this helps,

Vincent
 
Sorry about that. Here it is again:

beans is identical to *(beans+i). Thus

beans[j] or *(beans+j) or *(*(beans+i)+j) or (*(beans+i))[j]

are all equivalent.

Hope this helps,

Vincent
 
Thank you for your replie,but what I want to know is:

why is *(beans +i) identical to beans.Why doesn't *(beans+i)refer to value instead of an adrress?Why isn't '*' redeferencing operator here?

Again,thank you
 
Again this multidimensionality... :)
The meaning of operators '*' and '[]' is the same - apply one level of indirection to the operand. Two-dimensional array
int beans[5][10];
has double indirection. The internal '*' or '[]' takes out only one indirection, so beans[3] or *(beans+3) gives a pointer to single-dimensional array of 10 elements, the 3-rd element of beans[5][10] with the type (int *). Elements of the lowest level can be accessed as beans[3][8] or *(*(beans+3)+8) - with double indirection.

Vincent, your turn...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top