What will be the output of the C program given below? int* func()
What will be the output of the C program given below?
#include <stdio.h>
int* func()
{
static int arr[5] = { 1, 2, 3, 4, 5 };
return arr;
}
int main()
{
int* ptr = func();
printf("Array Elements: ");
for (int i = 0; i < 5; i++) {
printf("%d ", *ptr);
}
return 0;
}
Show answerHide answer
Correct answer: (D) 1 1 1 1 1
Explanation
ptr is never incremented inside the loop, so *ptr always reads arr[0] = 1, printing 1 five times (after "Array Elements: "). To print 1 2 3 4 5 the loop would need ptr[i] or *(ptr + i).