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 1 of 3.

SAIRAM said:   4 years ago
@All.

Here, I will explain it clearly,

#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;
}

a [0] [1] [2] [3]
[0] 1 2 3 4 --> a[0][0] = 1 a[0][1] = 2 a[0][2] = 3 a[0][3] = 4
[1] 4 3 2 1 --> a[1][0] = 4 a[1][1] = 3 a[1][2] = 2 a[1][3] = 1
[2] 7 8 9 0 --> a[2][0] = 7 a[2][1] = 8 a[2][2] = 9 a[2][3] = 0

In general,
'a' contains base address which is the address of the first row first element in the array i.e., a[0][0],
&a is also refers to same base address a[0][0]

a+1 points to the address of a[1][0], which is the second-row first element.
Similarly a+2 points to the address of a[2][0], which is the third-row first element.

Each element or integer occupies 2bytes.
a[3][4] means we have 3 rows and 4 columns. I each row we have 4 elements.


Question:

What will be the output of the program if the array begins at 65472 and each integer occupies 2 bytes?


this means address of a[0][0] = 1 is 65472
this means address of a[0][1] = 2 is 65474
this means address of a[0][2] = 3 is 65476
this means address of a[0][3] = 4 is 65478
this means address of a[1][0] = 4 is 65480.
a+1 points the address of a[1][0], which is second row first element. so answer is 65480

&a represents the whole array but stores only the base address which is the starting element array a[0][0].

&a+1 points fourth row first element which means a[3][0]
address of a[0][0] is 65472 ------ 1st row 1st element
address of a[0][1] is 65474
address of a[0][2] is 65476
address of a[0][3] is 65478
address of a[1][0] is 65480 ------ 2nd row 1st element
address of a[1][1] is 65482
address of a[1][2] is 65484
address of a[1][3] is 65486
address of a[2][0] is 65488 ------ 3rd row 1st element
address of a[2][1] is 65490
address of a[2][2] is 65492
address of a[2][3] is 65494
address of a[3][0] is 65496 ------ 4th row 1st element.

Totally we have to cross 12 elements to reach &a+1 i.e., a[3][0].

Therefore, &a+1 denotes "12 ints * 2 bytes * 1 = 24 bytes".
Hence, begining address of 4th row first element is 65472 + 24 = 65496. So, &a+1 = 65496 which is also address of a[3][0].
(9)

Dharmesh said:   3 years ago
For second part of the question &a+1 means (&a) +1 as precedence of & is higher than the precedence of the arithmetic operator, and &a means base address of the array.

After 1st part pointer is pointing to 2nd array's 1st element. In order to reach the base address ie. Address of 1st array 1 element we need to add a size of 8*2=16 bytes to the address of (a+1) address.

65480+16=65496.

Now we have reached till base address &a now in order to reach (&a) +1 we need to add base address+ 4*2 more i.e. 65496+8= 65104.
The difference between 1st part and 2nd part will be 32 bits.
Address of (&a) +1 - a = 32 bits.

Thanks!
(1)

Neha said:   1 decade ago
As we know s+2 of a two dimensional array is s[2][0] means 1st element of 3rd row, as then 1st row's 1st element is s[0][0] then its clear now s+1 is the 1st element of 2nd row.

Now to reach 1st element of 2nd row there are 4integers (see the columm number) to cross,i.e 4*2bytes=8bytes will be added to the base address [i.e. s[0][0]'s address 65472] 65472+8 = 65480.

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

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

Dipak said:   3 years ago
In this, the base address is 64472.

&a means the initial address of array i.e. 64472.
In this array, 12 elements & each contain 2 bytes so 12 * 2 = 24.
so &a+1= 64472 + 24 = 64496.
means it shift the address after the a[3][4].

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.

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.

San said:   1 decade ago
&a+1 is the address of the variable stored next to the array.

Array requires "12 int * 2 bytes = 24 bytes".

Hence,

65472 + 24 = 65496. So, &a+1 = 65496.

Omkar said:   3 years ago
@Sairam.

As you said a+1 means a[1][0] and a+2 means a[2][0].

So, if a[0][1] we have to access then what we have to add in a
is a+4 means a[0][1].


Post your comments here:

Your comments will be displayed after verification.