CS Atlas GATE 2027
PDS course
0 / 103 complete

PDS/Phase 7 · Integration & Final Test

Chapter review L1.5 · Item 28 of 103

Arrays and strings in C

Read at your own pace. Try the questions before opening the answers.

On this page

ACompressed recap

🧾

An array is a row of same-type elements; a[i] is *(a + i) and sits at base + i·w; the name decays to &a[0] except in sizeof and &a; array parameters are pointers (sizeof gives 4), so functions can change elements and must be told the length.

2-D arrays are row-major: &a[i][j] = B + (i·C + j)·w; a + 1 jumps a row; parameters need the column count. int *p[N] is an array of pointers, int (*p)[N] a pointer to an array.

Strings are char arrays ending in '\0'; literals are read-only; strlen stops before \0 while sizeof counts the array; use strcmp, not ==.

BMerged rule card

1.5.1 1-D arrays; a[i] == *(a+i) == i[a]; arrays decaying to pointers; passing arrays to functions; the sizeof trap

  • int a[n]: indices 0 … n−1; element i at base + i·w.
  • Partial initializer zero-fills; int a[] = {…} sizes itself.
  • a[i] ≡ *(a + i) ≡ i[a]; p[i] works for any pointer.
  • a decays to &a[0] except in sizeof a and &a (whole array).
  • Array parameter = pointer: sizeof gives 4 here; pass the length.
  • Out-of-range index = undefined; arrays cannot be assigned.

1.5.2 2-D arrays; row-major layout; pointer to array vs array of pointers; reading complex declarations

  • T a[R][C]: &a[i][j] = B + (i·C + j)·w (row-major).
  • a[i][j] ≡ ((a + i) + j) ≡ *(a[i] + j).
  • a decays to T (*)[C]; a + 1 jumps a row; a[i] + 1 jumps an element.
  • Parameter: int m[][C] or int (*m)[C] — columns required.
  • int *p[N] array of pointers; int (*p)[N] pointer to array.
  • Read declarations: name → right ([] , ()) → left (*), parentheses first.

1.5.3 Strings: char arrays, '\0', read-only string literals, strlen vs sizeof, string functions and tracing string code

  • String = chars + '\0'; "abc" takes 4 bytes.
  • char s[] = "…" writable copy; char *p = "…" points to a read-only literal.
  • strlen counts before \0; sizeof counts the whole array (or 4 for a pointer).
  • strcpy, strcat, strcmp (0, 0, >0), strncpy in string.h; compare with strcmp, not ==.
  • %s prints from a pointer up to \0; p + k prints the tail.
  • *p + 1 ≠ *(p + 1).

CInterleaved GATE-style questions

Q1MCQ2 marksOutput

What does this print?

int a[5] = {1, 2, 3, 4, 5};
int *p = a + 1;
*p++ += 10;
*(p + 1) = *p * 2;
p[-2] = 0;
printf("%d %d %d %d %d\n", a[0], a[1], a[2], a[3], a[4]);
  • (A) 0 12 13 6 5
  • (B) 0 12 3 6 5
  • (C) 1 12 3 6 5
  • (D) 0 2 13 26 5

Q2NAT2 marksAddress

float f[6][8]; starts at 4000 with 4-byte floats. What is the address of f[5][2]?

Numerical answer: ________

Q3NAT1 markStrings

What is strlen("data\0base")?

Numerical answer: ________

Q4MCQ1 marksizeof

In main, int a[8]; (4-byte ints). What is sizeof(a) / sizeof(a[0])?

  • (A) 4
  • (B) 32
  • (C) 8
  • (D) 1

Q5MCQ2 marksOutput

What does this print?

char s[] = "abcdef";
char *p = s;
p += 2;
*p = 'X';
printf("%s %s %c %c\n", s, p + 1, *p + 1, *(p - 1));
  • (A) abcdef cdef d b
  • (B) abXdef def X c
  • (C) abXdef def Y b
  • (D) abXdef Xdef Y b

Q6MCQ1 markDeclarations

In int *x[5];, x is:

  • (A) a function
  • (B) an array of 5 pointers to int
  • (C) a pointer to an array of 5 ints
  • (D) a pointer to int

Q7MSQ2 marksArrays

For int m[4][5]; which are true?

  • (A) m[1] + 1 points to m[1][1]
  • (B) m + 1 points to row 1
  • (C) m[1][5] is the same element as m[2][0]
  • (D) sizeof(m[0]) is 20 with 4-byte ints

Q8MCQ2 marksOutput

What does this print?

int a[] = {4, 8, 15, 16};
int *p = a;
int s = 0;
while (p < a + 4)
    s += *p++;
printf("%d\n", s);
  • (A) 4
  • (B) 27
  • (C) 12
  • (D) 43

Q9MCQ1 markFunctions

Inside void f(char s[]), sizeof(s) with 4-byte pointers is:

  • (A) the length + 1
  • (B) 4
  • (C) depends on the caller
  • (D) the length of the string

DAnswer Vault

Answer Vault

Try the questions first. Open when you are ready to check.

Q1 Answer: (B)2 marks

Trace from Lesson 1.5.1.

Output

0 12 3 6 5
  • (A) is wrong: The 10 is added to a[1].
  • (C) is wrong: p[−2] sets a[0] to 0.
  • (D) is wrong: *p++ += 10 changes a[1], not a[2].

Re-read: Lesson 1.5.1 — 1-D arrays; a[i] == *(a+i) == i[a]; arrays decaying to pointers; passing arrays to functions; the sizeof trap

Q2 Answer: 41682 marks

(5 × 8 + 2) × 4 = 168 → 4168.

Re-read: Lesson 1.5.2 — 2-D arrays; row-major layout; pointer to array vs array of pointers; reading complex declarations

Q3 Answer: 41 mark

strlen stops at the first \0: 4.

Re-read: Lesson 1.5.3 — Strings: char arrays, '\0', read-only string literals, strlen vs sizeof, string functions and tracing string code

Q4 Answer: (C)1 mark

32 / 4 = 8 elements.

  • (A) is wrong: That is sizeof(a[0]).
  • (B) is wrong: That is sizeof(a).
  • (D) is wrong: a is a real array here.

Re-read: Lesson 1.5.1 — 1-D arrays; a[i] == *(a+i) == i[a]; arrays decaying to pointers; passing arrays to functions; the sizeof trap

Q5 Answer: (C)2 marks

p → index 2 is set to 'X'; p + 1 is "def"; *p + 1 = 'X' + 1 = 'Y'; *(p − 1) = 'b'.

Output

abXdef def Y b
  • (A) is wrong: The array was changed through p.
  • (B) is wrong: *p + 1 is the next character code.
  • (D) is wrong: p + 1 starts after X.

Re-read: Lesson 1.5.3 — Strings: char arrays, '\0', read-only string literals, strlen vs sizeof, string functions and tracing string code

Q6 Answer: (B)1 mark

[] binds tighter than *.

  • (A) is wrong: No parentheses for a function.
  • (C) is wrong: That needs (*x)[5].
  • (D) is wrong: It is an array.

Re-read: Lesson 1.5.2 — 2-D arrays; row-major layout; pointer to array vs array of pointers; reading complex declarations

Q7 Answer: (A) (B) (D)2 marks

Index 5 is outside a 5-column row; reading it is undefined even though it lands next to m[2][0].

  • (C) is wrong: An out-of-range column index is undefined behavior, even if the address happens to match m[2][0].

Re-read: Lesson 1.5.2 — 2-D arrays; row-major layout; pointer to array vs array of pointers; reading complex declarations

Q8 Answer: (D)2 marks

4 + 8 + 15 + 16.

Output

43
  • (A) is wrong: p moves every iteration.
  • (B) is wrong: All four elements are added.
  • (C) is wrong: The loop runs four times.

Re-read: Lesson 1.5.1 — 1-D arrays; a[i] == *(a+i) == i[a]; arrays decaying to pointers; passing arrays to functions; the sizeof trap

Q9 Answer: (B)1 mark

s is a pointer parameter.

  • (A) is wrong: Not inside the function.
  • (C) is wrong: Always the pointer size.
  • (D) is wrong: sizeof does not measure text.

Re-read: Lesson 1.5.1 — 1-D arrays; a[i] == *(a+i) == i[a]; arrays decaying to pointers; passing arrays to functions; the sizeof trap

Ready for the next idea?

Mark this item complete when you’ve finished reading and working through it.