Knowee
Questions
Features
Study Tools

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

Question

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

🧐 Not the exact question you are looking for?Go ask a question

Solution

The code will output "3".

Here's the step-by-step explanation:

  1. An integer array arr of size 3 is declared and initialized with the values 1, 2, and 3.

  2. A pointer p is declared and initialized to point to the first element of the array arr.

  3. The printf function is called to print the value of the element at the address (p + 2). In C, when you add an integer to a pointer, it advances by that many elements in the array. So, p + 2 points to the third element of the array arr.

  4. *(p + 2) dereferences the pointer (p + 2), which means it gives the value stored at the address (p + 2). This is the third element of the array arr, which is 3.

  5. So, the output of the program is "3".

This problem has been solved

Similar Questions

What will print out when the following code runs?

What will be the output of the following C++ code?

What will be the output of the following C code?

What will be the output of the following program?

What will be the output of the following PHP code?

1/4

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.