I have an array as:
int x[3][5]={
{1,2,3,4,5},
{6,7,8,9,10},
{11,12,13,14,15}
};
- What does *x refer to?
- *(*x+2)+5 refer to “8”.How does that happen?
- Is *(*x+2) same as *(*x)+2?
-
What if I do:
*n=&x;
Where is the pointer n pointing to? if it would have been only x and not an & then it would have been the base address.What for now?
*xis a dereference operation. In other words, “give me whatxis pointing at”. Since this is an array (of arrays), dereferencing x will give you the first array. This is equivalent to the array access syntax ofx[0].*(*x+2)+5is equivalent tox[0][2] + 5, which gives you 8. This is because:*xis the same asx[0](see #1) and*(x + 2)is the same asx[2]. Once you’ve done two dereferences, you’ve gone from an array of arrays (similar to a double-pointer) to an array (single pointer) to an actual number (the third item in the first array). Then, it’s just 3 + 5 = 8.*(*x+2)is equivalent tox[0][2](see #2), which is 3 (third element in array). However,*(*x) + 2gives youx[0][0] + 2(first element in array plus 2), which is 1 + 2 = 3. Same answer, but very different way of getting it.