What is the output of the following code?int arr[5] = {1, 2, 3, 4, 5};int *p = arr;printf("%d\n", p[2]);
Question
What is the output of the following code?int arr[5] = {1, 2, 3, 4, 5};int *p = arr;printf("%d\n", p[2]);
Solution
The output of the code will be 3.
Here's the step by step explanation:
-
An integer array
arrof size 5 is declared and initialized with the values 1, 2, 3, 4, 5. -
A pointer to an integer
pis declared and it is pointed to the first element of the arrayarr. -
printf("%d\n", p[2]);is used to print the value at the index 2 of the array pointed byp. In C, array indexing starts from 0. So,p[2]refers to the third element of the arrayarr, which is 3.
Therefore, the output of the code is 3.
Similar Questions
What will the following code output?#include <stdio.h>int main() { int arr[3] = {1, 2, 3}; int *p = arr; printf("%d\n", *(p + 2)); return 0;}
What is the output of the following code?#include <stdio.h>int main() { int arr[2][2] = {{1, 2}, {3, 4}}; printf("%d\n", arr[0][2]); return 0;}
.What will be the output of the following C code?int main() {int arr[] = {1, 2, 3, 4, 5};printf("%d", arr[3]);return 0;}*1 point1234
What is the result of the following code?#include <stdio.h>int main() { int arr[5] = {1, 2, 3, 4, 5}; int *ptr = (int *)(&arr + 1); printf("%d\n", *(ptr - 1)); return 0;}
What is the output of this program?#include <stdio.h> int main() { int arr[5] = {1, 2, 3,4,5}; int p, q, r; p = ++arr[1]; q = arr[1]++; r = arr[p++]; printf("%d, %d, %d", p, q, r); return 0; }
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.