C Programming - Pointers - Discussion

Discussion Forum : Pointers - Find Output of Program (Q.No. 12)
12.
What will be the output of the program assuming that the array begins at the location 1002 and size of an integer is 4 bytes?
#include<stdio.h>

int main()
{
    int a[3][4] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 };
    printf("%u, %u, %u\n", a[0]+1, *(a[0]+1), *(*(a+0)+1));
    return 0;
}
448, 4, 4
520, 2, 2
1006, 2, 2
Error
Answer: Option
Explanation:
No answer description is available. Let's discuss.
Discussion:
61 comments Page 2 of 7.

Vikash Mishra said:   8 years ago
According to me,

a[0]=1002;
and 1=4 bytes //because talking about address not talking about value.
so->1002+4=1006;
*(a[0]+1)=2;

SOUMYA said:   8 years ago
Here %u is given instead of %d, %u gives addresS, so would it not display an error?

Priya said:   8 years ago
We can write a[i]=i[a]
is it work for a[i][j]=?

Pratik said:   9 years ago
How to know the address when it is not given. Here the &a[0] is 1002. How can you get that?

GCE said:   9 years ago
But printf using control string %u, then how to prints array elements?

Tiwari said:   9 years ago
A multidimensional array is of form, a[i][j]. Let's see how we can make a pointer point to such an array. As we know now, the name of the array gives its base address. In a[i][j], a will give the base address of this array, even a+0+0 will also give the base address, that is the address of a[0][0] element.

Here is the generalized form for using a pointer with multidimensional arrays.

*(*(ptr + i) + j)
is same as

a[i][j]

Swetha said:   9 years ago
In arrays of C programming, the name of the array always points to the first element of an array. Here, address of first element of an array is &arr[0]. Also, arr represents the address of the pointer where it is pointing. Hence, &arr[0] is equivalent to arr.

Also, value inside the address &arr[0] and address arr are equal. Value in address &arr[0] is arr[0] and value in address arr is *arr. Hence, arr[0] is equivalent to *arr.

Similarly,

&a[1] is equivalent to (a+1) AND, a[1] is equivalent to *(a+1).
&a[2] is equivalent to (a+2) AND, a[2] is equivalent to *(a+2).
&a[3] is equivalent to (a+1) AND, a[3] is equivalent to *(a+3).
.
.
&a[i] is equivalent to (a+i) AND, a[i] is equivalent to *(a+i).

Prabha said:   10 years ago
We are using format specifiers as %u but instead of address values are printed. Whether it won't return any error?

Vamsi tharun kumar said:   10 years ago
Why the address start from 1002 why not 520, any one explain about this addresses?

Anjum said:   10 years ago
Why is the address assumed to be 1002 only?


Post your comments here:

Your comments will be displayed after verification.