IAE Academy

GATE 2024 CS2 Q.33: solution and answer

GATE 2024 · CS2Q.33 · 1 mark · MSQ

Consider the following C function definition.

int fX(char *a){
    char *b = a;
    while(*b)
        b++;
    return b - a;}

Which of the following statements is/are TRUE?

  1. (A)

    The function call fX("abcd") will always return a value

  2. (B)

    Assuming a character array c is declared as char c[] = "abcd" in main(), the function call fX(c) will always return a value

  3. (C)

    The code of the function will not compile

  4. (D)

    Assuming a character pointer c is declared as char *c = "abcd" in main(), the function call fX(c) will always return a value

See the question in the paper

Answer and solution

ANSWER

GATE CS 2024 (CS2) Q.33 — options A, B and D are TRUE; option C is FALSE.

Official answer: A, B, DSource: Official answer key, GATE 2024 CS2 (IISc Bengaluru)

  1. fX(char *a) sets b = a, then repeatedly does b++ while *b is nonzero — i.e., it walks forward until it reaches the string's null terminator '\0'. It returns b - a, the number of characters scanned before that terminator. This is exactly the standard strlen algorithm: it always terminates and always returns a value, provided a points into a properly null-terminated block of memory.

  2. Option A: fX("abcd") passes a pointer to the string literal "abcd", which the compiler stores as the 5 bytes 'a','b','c','d','\0'. The loop scans the 4 non-null characters and stops at the terminator, returning 4. It always returns a value. TRUE.

  3. Option B: char c[] = "abcd" declared in main() creates a local array initialised with the same 5 bytes 'a','b','c','d','\0'. Passing c to fX decays the array to a char* pointing at its first element — identical behaviour to option A, always returning 4. TRUE.

  4. Option C: the function body is syntactically and semantically valid C — a pointer declaration (char b = a), a while loop dereferencing a char, pointer subtraction (b - a) whose result is implicitly convertible to the declared return type int. It compiles without error, so 'will not compile' is FALSE.

  5. Option D: char *c = "abcd" declared in main() makes c a pointer directly at the string-literal storage 'a','b','c','d','\0' — the same guaranteed-null-terminated storage as in option A. fX(c) walks that same 5-byte block and returns 4, always returning a value. TRUE.

  6. Options A, B and D are three different ways of handing fX a pointer into a properly null-terminated character block, so all three are always safe and always return the string length (4 for "abcd"); option C is false because the code compiles fine. Final: A, B, D TRUE; C FALSE.

KEY IDEA

fX is just strlen written out by hand: b walks forward from a until it hits the terminating '\0', and b - a is the number of characters before it. It always returns some value as long as a points into a properly null-terminated block of memory — which is true for a string literal argument, for a local array initialised with "...", and for a pointer initialised with "...", since in every one of these cases the compiler lays down the characters followed by a '\0'.

WATCH OUT FOR

Assuming the loop can run past the end of allocated memory or that fX might not terminate — all three constructions (a string literal argument, a local array initialised from a literal, and a pointer initialised from a literal) guarantee a null-terminated block of memory, so the while(*b) loop always finds a '\0' and the function always returns; there is no case here where it fails to return.

From the GATE 2024 CS2 paper. Every question of this paper · Download the full paper · All 11 Pointers & arrays questions, with notes

More Pointers & arrays questions