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?
Answer
(A), (B), (D)
Explanation
fX moves the pointer b forward until it reaches the terminating '\0' and returns b - a, the number of characters before it: the length of the string. The pointer difference is converted to int on return, so the function is valid C and (C) is false.
- (A) A string literal is always stored with a terminating null character, so the call returns 4.
- (B)
char c[] = "abcd"creates a 5-element array whose last element is'\0', so the call returns 4. - (D)
cpoints to the literal itself, which also ends in'\0', so the call returns 4.
The loop stops in every case, so all three calls return a value.
Answer: A, B, D