sizeof an array of pointers and strlen of the first word: assume pointer size is 4 bytes. What does this print? #include<stdio.h> #include<string.h> int main() { char str[] = {'Frogs', 'Do', 'Not', 'Die', 'They', 'Croak!'}; printf('%d, %d', sizeof(str), strlen(str[0])); return 0; }

Difficulty: Easy

Correct Answer: 24, 5

Explanation:


Introduction / Context:
This checks your understanding of sizeof with arrays of pointers versus the length of a pointed-to string via strlen. Pointer size is specified as 4 bytes to make the arithmetic straightforward and portable for the question’s context.


Given Data / Assumptions:

  • str is an array of 6 pointers to char (char).
  • Each pointer is 4 bytes → sizeof(str) = 6 * 4 = 24 bytes.
  • str[0] points to the string literal 'Frogs' whose length is 5 characters.


Concept / Approach:
sizeof on an array yields the total bytes of the array object, not the sum of the lengths of target strings. strlen counts characters until the terminating '\0' in the referenced string.


Step-by-Step Solution:

Compute sizeof(str): 6 elements * 4 bytes each = 24.Compute strlen(str[0]): length('Frogs') = 5.printf prints '24, 5'.


Verification / Alternative check:
If the pointer size were 8 bytes (common on 64-bit systems), sizeof(str) would be 48; the second value would still be 5.


Why Other Options Are Wrong:

22, 4 / 25, 5 / 20, 2 / 28, 6: These reflect incorrect pointer sizes or wrong string length.


Common Pitfalls:
Confusing sizeof(pointer) with strlen of the pointed-to string; forgetting that sizeof on an array does not decay inside the sizeof operator.


Final Answer:
24, 5.

More Questions from Strings

Discussion & Comments

No comments yet. Be the first to comment!
Join Discussion