C Programming - Pointers - Discussion

Discussion Forum : Pointers - General Questions (Q.No. 6)
6.
What would be the equivalent pointer expression for referring the array element a[i][j][k][l]
((((a+i)+j)+k)+l)
*(*(*(*(a+i)+j)+k)+l)
(((a+i)+j)+k+l)
((a+i)+j+k+l)
Answer: Option
Explanation:
No answer description is available. Let's discuss.
Discussion:
70 comments Page 1 of 7.

Er. Rohit Deshmukh said:   7 years ago
Here In the above problem "a" is an Array having $ dimensions.

Which indicates that a is a pointer and i , j , k , l are offset values.

Let Us Consider An Example : Of An Array;

int ptr = a + i;
where a is Base address and i is Offset,
now it gives address of a[i] which is in integer form
Hence we will it into address as *(a+i) ;
and then add it with the 2-nd offset i.e. j -> *(a+i)+j.

Similarly The Process Continues as Follows:
Address = *(a+i)+j Offset = k
new Address = (*(*(a+i)+j)+k)
Address = *(*(a+i)+j)+k Offset = l
new Address = *(*(*(a+i)+j)+k)+l

And Then Finally The Address Of The Element a[i][j][k][l] = *(*(*(*(a+i)+j)+k)+l).
(2)

Aseil said:   1 decade ago
@Mark. I TRIED TO CLEAR IT BY THIS PROGRAM:

#include<stdio.h>

int main()

{
int a[2][2]={1,2,3,4};
printf("val address\n");
printf("%d %d\n",*(*(a)),*(a));
printf("%d %d\n",*(*(a)+1),*(a)+1);
printf("%d %d\n",*(*(a+1)),*(a+1));
printf("%d %d\n",*(*(a+1)+1),*(a+1)+1);

return 0;
}

Try to make some diagram to understand this and I am sure if you do so it will be clear.

Raju Naidu said:   1 decade ago
Arrays internally works based on pointers. In arrays each subscript represents one pointer.

Ex:a[1]

Once you compile this code it internally converted into
*(a+1)or*(1+a)

That's y
a[i][j][k][l]---->a[i]--->*(a+i)(one subscript means onepointer)
a[i][j]--->*(*(a+i)+j)(Two subscripts means two pointers)
a[i][j][k]----->*(*(*(a+i)+j)+k)
a[i][j][k][l]---->*(*(*(*(a+i)+j)+k)+l)

Bye friends.

Atul Mithe said:   1 decade ago
If we want to refer one dimensional we just use single pointer ie like *p, if we need to refer two dimensional we have to use double pointer reference ie *(*p), if we have to refer 4 at a time just use 4 pointer statement i.e. *(*(*(*(p))))and so on..

a[i] = *(a+i)

a[i][j] = *(a[i]+j)
= *(*(a+i)+j)

a[i][j][k]= *(a[i][j]+k)
= *(*(a[i]+j)+k)
= *(*(*(a+i)+j)+k)

Gopi reddy said:   1 decade ago
If we want to refer one dimensional we just use single pointer ie like *p, if we need to refer two dimensional we have to use double pointer reference ie *(*p), if we have to refer 4 at a time just use 4 pointer statement i.e. *(*(*(*(p))))and so on.

a[i] = *(a+i)

a[i][j] = *(a[i]+j)
= *(*(a+i)+j)

a[i][j][k]= *(a[i][j]+k)
= *(*(a[i]+j)+k)
= *(*(*(a+i)+j)+k)

Rajasimhan said:   1 decade ago
Let A[3][3] be a matrix.


A(address =50) --------> (address 60) ------>(address 100).
(address 61) ------>(address 103).
(address 62) ------>(address 106).

Now to get A[2][2] = *(*(*(50)+2)+2)
= *(*(60+2)+2)
= *(*(62)+2)
= *(106+2)
= *(108).

= value at address 108.

Venkatesh said:   1 decade ago
Consider an array of pointers. These pointers are pointing to another single dimensional array.

Now (a+i) stores the value of address of a pointer.

* (a+i) gives this value.

So if a[i][j] is the array its first term will be * (a+i). Jth one will be * (a+i) +j.

This can be extended to any multi dimensional array.
(1)

Sneha said:   3 years ago
Here is the coding part;


#include<stdio.h>
int main ()
{
int arr[2][3]={5, 10, 15, 20, 25, 30};
int (*ptr) [2][3]=&arr;
printf ("%d\t", ***ptr) ;
printf ("%d\t", ***(ptr+1)) ;
printf ("%d\t", **(*ptr+1)) ;
printf ("%d\t", *(*(*ptr+1)+2)) ;
return 0;
}
(4)

Mark said:   1 decade ago
Please, someone clear my doubt.

main()
{
int a[2][2]={1,2,3,4};
printf("%d %d",a+1,*(a+1));
}

In this problem both values of printf are same.. how could it.. if a+1 denotes address of a[1][0], then *(a+1) should display its value, but why not..??? please explain ?

Chitti said:   2 years ago
Here is the coding part;


#include<stdio.h>
int main ()
{
int arr[2][3]={5, 10, 15, 20, 25, 30};
int (*ptr) [2][3]=&arr;
printf ("%d\t", ***ptr);
printf ("%d\t", ***(ptr+1));
printf ("%d\t", **(*ptr+1));
printf ("%d\t", *(*(*ptr+1)+2));
return 0;
}
(2)


Post your comments here:

Your comments will be displayed after verification.