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]
Discussion:
70 comments Page 1 of 7.
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;
}
#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)
Pradeep said:
1 decade ago
ptr[0]------>*(ptr+0);
ptr[1]------>*(ptr+1);
Like that:
a[i]---->*(a+i);
a[i][j]---->*(*(a+i)+j);
a[i][j][k]---->*(*(*(a+i)+j)+k);
Now you can understand.
ptr[1]------>*(ptr+1);
Like that:
a[i]---->*(a+i);
a[i][j]---->*(*(a+i)+j);
a[i][j][k]---->*(*(*(a+i)+j)+k);
Now you can understand.
(3)
Ramanaji 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.....
(2)
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;
}
#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)
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).
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)
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.
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)
Suraj said:
7 years ago
Thanks @Ramanaji.
(1)
Ajay said:
7 years ago
Thanks, @Pradeep.
(1)
Tukaram said:
8 years ago
Thanks @Ramanaji.
(1)
Praveen said:
1 decade ago
Hi @Suma sahiti, array is not a pointer, array is an sequential collection of elements of similar data types, but pointer is a variable which holds the address of another variable.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers