Knowee
Questions
Features
Study Tools

PointersWhat will be the output of the following C code?main() { static int a[ ] = {0, 1, 2, 3, 4}; int *p[ ] = {a, a+1, a+2, a+3, a+4}; int **ptr = p; ptr++; printf("\n %d %d %d", ptr-p, *ptr-a, **ptr); *ptr++; printf("\n %d %d %d", ptr-p, *ptr-a, **ptr); *++ptr; printf("\n %d %d %d", ptr-p, *ptr-a, **ptr); ++*ptr; printf("\n %d %d %d", ptr-p, *ptr-a, **ptr);}Options1 1 12 2 23 3 33 4 41 1 12 2 23 3 34 4 41 1 12 2 24 4 43 3 31 1 12 2 23 3 33 3 3

Question

PointersWhat will be the output of the following C code?main() { static int a[ ] = {0, 1, 2, 3, 4}; int *p[ ] = {a, a+1, a+2, a+3, a+4}; int **ptr = p; ptr++; printf("\n %d %d %d", ptr-p, *ptr-a, **ptr); *ptr++; printf("\n %d %d %d", ptr-p, *ptr-a, **ptr); *++ptr; printf("\n %d %d %d", ptr-p, *ptr-a, **ptr); ++*ptr; printf("\n %d %d %d", ptr-p, *ptr-a, **ptr);}Options1 1 12 2 23 3 33 4 41 1 12 2 23 3 34 4 41 1 12 2 24 4 43 3 31 1 12 2 23 3 33 3 3

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

Solution

The output of the given C code will be:

1 1 1 2 2 2 3 3 3 3 4 4

So, the correct option is 1 1 1, 2 2 2, 3 3 3, 3 4 4.

Here's the step by step explanation:

  1. ptr is a pointer to p, which is an array of pointers to a. Initially, ptr points to p[0] which is a. When ptr is incremented, it points to p[1] which is a+1. So, ptr-p is 1, *ptr-a is 1 and **ptr is 1.

  2. *ptr++ increments ptr to point to p[2] which is a+2. So, ptr-p is 2, *ptr-a is 2 and **ptr is 2.

  3. *++ptr increments ptr to point to p[3] which is a+3. So, ptr-p is 3, *ptr-a is 3 and **ptr is 3.

  4. ++*ptr increments the value pointed by ptr which is a[3] from 3 to 4. So, ptr-p is still 3, *ptr-a is 4 and **ptr is 4.

This problem has been solved

Similar Questions

What is the output of the following C code:#include <stdio.h>void fun(int **p);int main(){ int a[3][4] = {1, 2, 3, 4, 4, 3, 2, 8, 7, 8, 9, 0}; int *ptr; ptr = &a[0][0]; fun(&ptr); return 0;}void fun(int **p){ printf("%d\n", **p);}1234

What will be the output of the following C code?void main() {  int a[] = {1,2,3,4,5}, *p;  p = a;  ++*p;  printf("%d ", *p);p += 2;  printf("%d ", *p);}a.24b.22c.23d.34

What will be the output of the following C code? #include <stdio.h> void main() { int x = 0; int *ptr = &x; printf("%d\n", *ptr); }

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 will be the output of the following code?int main() {    int *ptr = NULL;    ptr = (int*)malloc(sizeof(int));    *ptr = 5;    printf("%d\n", *ptr);    free(ptr);    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.