What is the output of the following code?int arr[] = {1, 2, 3, 4, 5};int *p = arr;p++;printf("%d\n", *p);
Question
What is the output of the following code?int arr[] = {1, 2, 3, 4, 5};int *p = arr;p++;printf("%d\n", *p);
Solution
The output of the code will be 2.
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 to an integer
*pis declared and it is initialized to point to the first element of the arrayarr[]. So,*pis pointing to the value1. -
The pointer
pis incremented by one usingp++. This means that the pointer now points to the next element in the array, which is2. -
The value of the element pointed to by
pis printed usingprintf("%d\n", *p);. Sincepis pointing to the second element of the array,2is printed.
Similar Questions
What is the output of the following code?#include <stdio.h>int main() { int arr[] = {1, 2, 3, 4, 5}; printf("%d\n", arr[3]); return 0;}
What is the output of the following code?int arr[] = {10, 20, 30, 40, 50};printf("%d\n", 2[arr]);
.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 will be the output of the following code?12345678910111213141516#include<stdio.h>int main() { int arr[] = {2, 4, 6, 8, 10}; int *p = &arr[0]; for (int i = 1; i <= 4; i++) { if (i % 3 == 0) { p += 1; } } printf("%d", (*(p + 2))); 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.