In C programming on a 32-bit system (pointer size = 4 bytes), what will this program print for sizeof results of an array versus a pointer, and of the pointed elements? #include <stdio.h> int main() { char a[] = "Visual C++"; /* array holds characters plus the '\0' terminator */ char b = "Visual C++"; / pointer to string literal */ printf("%d, %d ", sizeof(a), sizeof(b)); printf("%d, %d", sizeof(*a), sizeof(*b)); return 0; }

Difficulty: Easy

Correct Answer: 11, 4 1, 1

Explanation:


Introduction / Context:
This question tests understanding of sizeof with arrays, pointers, and the elements they reference on a 32-bit C environment. It highlights the crucial difference between an array's storage size and a pointer's storage size, as well as the size of the types they point to.


Given Data / Assumptions:

  • Pointer size is 4 bytes (32-bit).
  • a is a char array initialized with the characters of "Visual C++" plus the null terminator.
  • b is a pointer to char that points at a string literal.


Concept / Approach:
sizeof on an array gives total bytes of the array in this scope. The string "Visual C++" has 10 visible characters; arrays include an extra '\0', so sizeof(a) = 11. sizeof on a pointer variable yields the pointer's size, not the string length, hence sizeof(b) = 4 on a 32-bit system. The expression *a is a char (the first character), so sizeof(*a) = 1. Similarly, *b is also a char, so sizeof(*b) = 1.


Step-by-Step Solution:
Count characters in "Visual C++" → 10 + 1 terminator → sizeof(a) = 11.Pointer size (32-bit) → sizeof(b) = 4.*a has type char → sizeof(*a) = 1.*b has type char → sizeof(*b) = 1.


Verification / Alternative check:
Printing sizeof("Visual C++") alone gives 11, confirming the array size equals the literal's size including the terminator in this context.


Why Other Options Are Wrong:
Any answer with 10 for the array ignores the null terminator. Values like 2 for pointer size are not correct on a 32-bit target. A value of 2 for sizeof(*b) is wrong because a char is 1 byte in C.


Common Pitfalls:
Confusing array decay to pointer in expressions with how sizeof treats arrays in their defining scope, and forgetting the null terminator byte.


Final Answer:
11, 4 1, 1

Discussion & Comments

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