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 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; }

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.