Knowee
Questions
Features
Study Tools

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]);

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

Solution

The output of the code will be 3.

Here's the step by step explanation:

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

  2. A pointer to an integer p is declared and it is pointed to the first element of the array arr.

  3. printf("%d\n", p[2]); is used to print the value at the index 2 of the array pointed by p. In C, array indexing starts from 0. So, p[2] refers to the third element of the array arr, which is 3.

Therefore, the output of the code is 3.

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

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.