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 5 of 7.

Sujan said:   1 decade ago
We can refer one dimensional as:

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

The above four expressions gives same result.

For 2-dimensional array.

s[2][1] = *(s[2]+1) = *(*(s+2)+1).

The above three expressions gives same value.

Anjum said:   1 decade ago
Why is the address assumed to be 1002 only?

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

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

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).

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]

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

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?

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

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


Post your comments here:

Your comments will be displayed after verification.