C Programming - Arrays - Discussion

Discussion Forum : Arrays - Find Output of Program (Q.No. 6)
6.
What will be the output of the program if the array begins at 65472 and each integer occupies 2 bytes?
#include<stdio.h>

int main()
{
    int a[3][4] = {1, 2, 3, 4, 4, 3, 2, 1, 7, 8, 9, 0};
    printf("%u, %u\n", a+1, &a+1);
    return 0;
}
65474, 65476
65480, 65496
65480, 65488
65474, 65488
Answer: Option
Explanation:

Step 1: int a[3][4] = {1, 2, 3, 4, 4, 3, 2, 1, 7, 8, 9, 0}; The array a[3][4] is declared as an integer array having the 3 rows and 4 colums dimensions.

Step 2: printf("%u, %u\n", a+1, &a+1);

The base address(also the address of the first element) of array is 65472.

For a two-dimensional array like a reference to array has type "pointer to array of 4 ints". Therefore, a+1 is pointing to the memory location of first element of the second row in array a. Hence 65472 + (4 ints * 2 bytes) = 65480

Then, &a has type "pointer to array of 3 arrays of 4 ints", totally 12 ints. Therefore, &a+1 denotes "12 ints * 2 bytes * 1 = 24 bytes".

Hence, begining address 65472 + 24 = 65496. So, &a+1 = 65496

Hence the output of the program is 65480, 65496

Discussion:
29 comments Page 2 of 3.

Reena said:   1 decade ago
How we can refer a[0][1] ?

Siddharth said:   1 decade ago
Here if I am not wrong than if I have to print the address of first 4 appearing in the array than will I use like &(a+3)(a).

Vignesh said:   1 decade ago
Can anyone answer Reena's question since I'm having the same doubt.

Venudayyam said:   1 decade ago
65472+(4*2) == 65480 its ok.

But *a+1 == * has high precedence.

So, *a == returns a pointer to base address of first array (i.e. (int*)[4]).

So *a gives == 65472 and *a+1 == 65472+ 2 == 65474.

Vallam said:   9 years ago
Given answer is for turbo C, but while going to GCC compiler answer be,

Let a = (&a[0]) = 1000.

A+1 = (&a[1]) = 1020.

&a+1. It means &a hold starting address (a[0]), 'a' will take whole 2-D array. Adding with one goes to next 2-D array address.

So that 2-D array takes 48 (12*4) bytes. Next array starting with 1052. (as per GCC compiler).

Vallam said:   9 years ago
Sorry & a+1 = 1048.

Pihu said:   8 years ago
Therefore, &a+1 denotes "12 ints * 2 bytes * 1 = 24 bytes".

Why 1 is multiplied with 12*2?

Can anyone please explain?

Krishna reddy said:   8 years ago
{
a-> a b c d
a+1-> e f g h
a+2-> j k l m
}

If a=65472 between a and a+1 there are 4 integers so 4*2(siz of int int 16 bit compiler)=8
a+1-> 65472+8=65480.

Sariya said:   7 years ago
@All.

For the second part just try to run on your compiler with a, &a then a+1,&a+1 you will get the difference and can conclude &a+1 is pointer to Array so next address could be assigned only after covering the whole array(...72->&a -...96->&a+1).

Abhishek said:   7 years ago
Thanks for explaining @Neha.


Post your comments here:

Your comments will be displayed after verification.