GATE / CS ARCHIVE

SIMILAR QUESTIONS / PROGRAMMING IN C

Resolve multidimensional array offsets and accesses

6 questions solved the same way, newest first. Back to Programming in C

How these are solved

Determine row-major strides and the type/level at each array decay or dereference, then resolve the requested address, value or sequence.

2 marks · MCQ
Report issue

Arrays & Strings

Question 43, GATE 2022: An ANSI C program initializes int a[3][3][3]={{1,2,3,4,5,6,7,8,9},{10,11,12,13,14,15,16,17,18},{19,20,21,22,23,24,25,26,27}}; int i=0,j=0,k=0; then for(i=0;i<3;i++){for(k=0;k<3;k++) printf("%d ",a[i][j][k]); printf("\n");}. Find the rows printed. A (1,2,3)/(10,11,12)/(19,20,21); B (1,4,7)/(10,13,16)/(19,22,25); C (1,2,3)/(4,5,6)/(7,8,9); D (1,2,3)/(13,14,15)/(25,26,27).
Tap to enlarge
Your answer
Answer & solution

Official answer: A

The initializer fills the 3×3×3 array in row-major order with 1 through 27. The middle index is fixed at j=0, while i and k vary. The selected values are a[0][0][k]=1, 2, 3; a[1][0][k]=10, 11, 12; a[2][0][k]=19, 20, 21. Thus the output is option A.

The address/value formula for this initializer is a[i][j][k]=9i+3j+k+1. Since j never changes from zero, each outer iteration jumps nine values, while each inner iteration advances one. The newlines occur after the three k values, producing exactly three rows.

Original paper, page 28 · Answer key

1 mark · MCQ
Report issue

Pointers & Memory

Question 10, GATE 2021: ANSI C declares int arr[4][5] and initializes arr[i][j]=10*i+j for 0≤i<4,0≤j<5. It prints *(arr[1]+9) with %d. Offered outputs: A 14; B 20; C 24; D 30. Explain the intended result and the language-standard issue.
Tap to enlarge
Your answer
Answer & solution

Official answer: C

Our answer: C under intended flattening; undefined in strict C

The intended contiguous-address calculation begins at row1, column 0, linear index 5; advancing 9 integers reaches index 14=row2, column 4, whose initialized value is 24, C. However arr[1] decays to a pointer into the five-element row subarray. Adding9 goes beyond that array and its one-past position, so the expression has undefined behavior under C pointer-arithmetic rules. Contiguous storage of the outer array does not legalize this cross-row pointer arithmetic. The official C is retained as the intended exam answer, not a portable guarantee.

The intended memory layout has rows 0–4,10–14,20–24,30–34. The desired element is arr[2][4], which is a portable way to express it. Do not run this original expression and treat one observed compiler output as a proof of defined behavior. The official option describes the conventional exam address calculation only.

Original paper, page 60 · Answer key

1 mark · NAT
Report issue

Pointers & Memory

Question 22, GATE 2020: Find the output of this C program:
#include <stdio.h>
int main(void) {
 int a[4][5]={{1,2,3,4,5},{6,7,8,9,10},{11,12,13,14,15},{16,17,18,19,20}};
 printf("%d\n", *(*(a+**a+2)+3));
 return 0;
}
Tap to enlarge
Answer & solution

Official answer: 19

Step through it (6 steps)

Worked explanation

Track pointer types: in this expression a decays to a pointer to an array of five ints, so adding 1 advances by a whole row. *a denotes row zero, and a is its first integer, 1. Therefore a+a+2 is a+3. Dereferencing gives row three, which in the addition +3 decays to a pointer to its first int. Adding 3 selects its fourth entry and the outer dereference reads 19. Written with subscripts the expression is a[3][3], not a[0][6] or a linear six-element offset. Both row and column indices are valid.

Derivation and answer

Here a=a[0][0]=1. Thus a+a+2=a+3 points to the fourth row. Dereferencing that row and adding 3 selects a[3][3]=19. All pointer arithmetic stays within the relevant row/outer array, so the printed expression has the defined result 19.

Original paper, page 8 · Answer key

2 marks · MCQ
Report issue

Pointers & Memory

Question 62, GATE 2015: Assume x address=2000 decimal and unsigned int occupies four bytes. Code: int main(){unsigned int x[4][3]={{1,2,3},{4,5,6},{7,8,9},{10,11,12}};printf("%u, %u, %u",x+3,*(x+3),*(x+2)+3);}. A 2036,2036,2036; B 2012,4,2204; C 2036,10,10; D 2012,4,6.
Tap to enlarge
Your answer
Answer & solution

Official answer: A

Our answer: A under intended address display; literal printf has undefined behavior

x decays to a pointer to a whole three-element row, so adding 3 advances 3×12 bytes. Dereferencing that pointer produces a row array, which decays to its first-element pointer rather than the value 10. By contrast *(x+2)+3 adds three four-byte elements to the third row's base2024, again yielding numerical address 2036, now one past that row. These address calculations explain intended A only. Printing pointers with %u is a variadic type mismatch and undefined behavior; there is no guaranteed actual C output.

Solution: Each row occupies 3*4=12 bytes. x+3 points to the fourth row at 2000+36=2036; *(x+3) decays to that row’s first element at 2036. *(x+2)+3 is the valid one-past pointer of the third row, also numerically 2036 under the stated layout. Thus the intended address triple is 2036, 2036, 2036, A. However all three arguments are pointers while %u requires unsigned int; this variadic format mismatch is undefined behavior in C. A conforming address display uses %p with each pointer cast to void*, whose textual format is implementation-defined. The omitted stdio declaration is another standalone-code caveat.

Original paper, page 23 · Answer key

2 marks · MCQ
Report issue

Arrays & Strings

Question 50, GATE 2015: Y is a one-dimensional array of size n and X a C two-dimensional n×n array, with 2≤n≤10. Each segment starts with Y[i]=0 and X[i][j]=i+j. All X elements lie in the same main-memory page frame. Segment 1: for(i=0;i<n;i++) Y[i]+=X[0][i]; Segment 2: for(i=0;i<n;i++) Y[i]+=X[i][0]; Assess S1: final Y contents are identical; S2: X elements accessed in segment 1 are contiguous in memory; S3: X elements accessed in segment 2 are contiguous. Options: (A) S2 only; (B) S3 only; (C) S1 and S2 only; (D) S1 and S3 only.
Tap to enlarge
Your answer
Answer & solution

Official answer: C

1. Both loops add i to initially zero Y[i], because X[0][i]=0+i and X[i][0]=i+0.

2. In C row-major storage, X[0][i] traverses consecutive elements, whereas X[i][0] advances by n elements for n≥2. Being in one page does not make those column elements adjacent. Thus S1, S2, C.

Original paper, page 71 · Answer key

2 marks · MCQ
Report issue

Address of an element of a C array

Arrays & Strings

Question 2.8, GATE 2002: C declares char a[100][100]. Memory is byte-addressable, and the array starts at address 0. What is the address of a[40][50]?
(A) 4040; (B) 4050; (C) 5040; (D) 5050.
Tap to enlarge
Your answer
Answer & solution

Answer: B (no official key was published)

Step 1: Compute the linear index before the byte address.

Step 2: There are 40 complete rows before row40, each contributing 100 characters, and 50 earlier characters within that row. Therefore the zero-based linear index is 4000+50=4050. Since sizeof(char)=1, no further scale factor is needed. The5040 alternative reverses the row and column roles.

Conclusion: C lays out rows contiguously in row-major order. Each char occupies one byte, and each row has 100 elements. The offset of a[40][50] is 40×100+50=4050 bytes, giving address 4050 from the stipulated zero base, B.

Original paper, page 4