C Programming - Arrays - Discussion
Discussion Forum : Arrays - Find Output of Program (Q.No. 2)
2.
What will be the output of the program ?
#include<stdio.h>
int main()
{
static int a[2][2] = {1, 2, 3, 4};
int i, j;
static int *p[] = {(int*)a, (int*)a+1, (int*)a+2};
for(i=0; i<2; i++)
{
for(j=0; j<2; j++)
{
printf("%d, %d, %d, %d\n", *(*(p+i)+j), *(*(j+p)+i),
*(*(i+p)+j), *(*(p+j)+i));
}
}
return 0;
}
Discussion:
78 comments Page 6 of 8.
Monika said:
1 decade ago
ThanQ Delip
Yogesh said:
1 decade ago
a[j][k] is equivalent to *((base-type *)a+(j*row length)+k).
Shibu said:
1 decade ago
@Subham.
*(p+1)-> the value at p+1 is a+1.
Now *((a+1)+0) -> *(a+1){a refers to base address of array 'a',which is at location 0}.
Hence we can write it as a[1].
The value at a[1]=2.
*(p+1)-> the value at p+1 is a+1.
Now *((a+1)+0) -> *(a+1){a refers to base address of array 'a',which is at location 0}.
Hence we can write it as a[1].
The value at a[1]=2.
Subham said:
1 decade ago
Please explain how *(*(p+1) +0) =a[1].
Priya said:
1 decade ago
Here we use static type 'p'.
That's why default value of p is 0.
That's why default value of p is 0.
Riya said:
1 decade ago
Please explain clearly why p=0?
Mr. x said:
1 decade ago
Why p is 0 ?
Manoj said:
1 decade ago
Why p=0?
Rupinderjti said:
1 decade ago
@jai -- it's the declaration of an Array of pointers.But here its size is 3 ( static int *p[2]={(int*)a, (int*)a+1, (int*)a+2};).
And the elements in arrays are 3 pointers pointing to first second and third position of elements of an 2-D array named a[2][2] respectively.
And the elements in arrays are 3 pointers pointing to first second and third position of elements of an 2-D array named a[2][2] respectively.
Jai said:
1 decade ago
What means this:- please any one help ?
static int *p[] = {(int*)a, (int*)a+1, (int*)a+2};
static int *p[] = {(int*)a, (int*)a+1, (int*)a+2};
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers