Knowee
Questions
Features
Study Tools

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);
🧐 Not the exact question you are looking for?Go ask a question

Solution

The output of the code will be 2.

Here's the step by step explanation:

  1. An integer array arr[] is declared and initialized with the values {1, 2, 3, 4, 5}.

  2. A pointer to an integer *p is declared and it is initialized to point to the first element of the array arr[]. So, *p is pointing to the value 1.

  3. The pointer p is incremented by one using p++. This means that the pointer now points to the next element in the array, which is 2.

  4. The value of the element pointed to by p is printed using printf("%d\n", *p);. Since p is pointing to the second element of the array, 2 is printed.

This problem has been solved

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 snippet?int x = 5;int y = 10;int *ptr = &x;*ptr = y;printf("%d", x);510015Generates a compilation error

What is the output of the following code?#include <stdio.h>int main() {    int i = 0;    for (; i < 5; i++);    printf("%d ", i);    return 0;}

What is the output of the following code?int arr[5] = {10, 20, 30, 40, 50};int *ptr = arr;cout << *(ptr + 2);10203040

What is the output of the following piece of code?int i;i = 10;while (i < 20){ printf("%d", i % 2); i++;}

1/3

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.