What is the output of the following code?#include <stdio.h>int main() { int arr[] = {1, 2, 3, 4, 5}; int *p = arr; printf("%d\n", *(p + 3)); return 0;}
Question
What is the output of the following code?#include <stdio.h>int main() { int arr[] = {1, 2, 3, 4, 5}; int *p = arr; printf("%d\n", *(p + 3)); return 0;}
Solution
The output of the code will be 4.
Here's the step by step explanation:
-
An integer array
arr[]is declared and initialized with the values{1, 2, 3, 4, 5}. -
A pointer
*pis declared and it is pointed to the base address of the arrayarr[]. So,pis pointing to the first element of the array, which is1. -
In the
printfstatement,*(p + 3)is used. Here,p + 3means the address of the 4th element in the array (since array indexing starts from 0). So,*(p + 3)is the value at the addressp + 3, which is the 4th element of the arrayarr[]. -
Therefore,
4is printed as the output.
Similar Questions
What will be the output of the following code snippet?
What is the output of the following code in Python?
What will be the output of the following C code?
What is the output for the following code?Note: This question helps in clearing conceptual technical interviews for service-based companies.
What will be the output of the following C++ code?
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.