-2
    int main()
    {
static int x[]={100,200,300,400,500};
static int *p={x+2,x,x+3,x+4,x+1};
int **ptr=p;
ptr++;
printf("%d",*ptr);

return 0;

}

can anyone please explain why is the output 500, initially ptr contains x+2, after incrementation, it should contain x and the output should be 100, but the output is 500. I don't know why! A lot of guys are concerned about the warning and norms of C , the thing is this is a textbook question and i need to find the output ,and how i got the output Any help is appreciated!!

Galik
  • 47,303
  • 4
  • 80
  • 117
Sagarb
  • 9
  • 4
  • Are you not getting a warning for `int **ptr=p;`? Or `printf("%d",*ptr);`? – mediocrevegetable1 May 09 '21 at 09:22
  • 1
    Do you have a [compilable version](http://coliru.stacked-crooked.com/a/a4e71fb444517b9e) of this code also? – πάντα ῥεῖ May 09 '21 at 09:23
  • `ptr` is a pointer to a pointer. `*ptr` is a pointer. You print that pointer, not what `*ptr` is pointing to (which would be `**ptr`). A decent compiler should warn you about mismatching `printf` formatting and argument. – Some programmer dude May 09 '21 at 09:31
  • I am new to c programming, and i need to find the output of this code and how i got this , any help is appreciated?? – Sagarb May 09 '21 at 09:32
  • Your code doesn't compile. – Galik May 09 '21 at 09:36
  • @Galik it does , try gdb online C complier – Sagarb May 09 '21 at 09:37
  • Sorry, you tagged this as `C++`, I didn't realize it's `C`. – Galik May 09 '21 at 09:38
  • Actually, you don't allocate memory for p array here. You should have had to write it as int p[] = {x+2,x+3,x+4,x+1}; – bimjhi May 09 '21 at 09:38
  • 2
    No, your code doesn't compile with GDB online compiler. It gives several errors. Concentrate on the first error which is `main.c:14:30: warning: excess elements in scalar initializer static int* p = { x + 2, x, x + 3, x + 4, x + 1 }; ` – fpiette May 09 '21 at 09:39
  • 1
    This line is wrong: `static int *p={x+2,x,x+3,x+4,x+1};`, even if your compiler happens to accept it. It looks like it should be an array. – interjay May 09 '21 at 09:42
  • what did you tried to do? you are playing with the pointers but it is not clear what you want to do here.. – Asaf Itach May 09 '21 at 09:42
  • @AsafItach, this was my homework and i need to find the output. – Sagarb May 09 '21 at 09:45
  • 1
    > the thing is this is a textbook question and i need to find the output ,and how i got the output Any help is appreciated!! I suspect your textbook suggest you to find errors here. – bimjhi May 09 '21 at 09:46
  • @bimjhi i think you are right there should be int *p[]; – Sagarb May 09 '21 at 09:46

1 Answers1

0

if you change this

int main() {
  static int x[]={100,200,300,400,500};
  static int *p={x+2,x,x+3,x+4,x+1};
  int **ptr=p;
  ptr++;
  printf("%d",*ptr);
  return 0;
}

to this

int main() {
  static int x[]={100,200,300,400,500};
  static int *p[]={x+2,x,x+3,x+4,x+1};
  int **ptr=p;
  ptr++;
  printf("%d",**ptr);

  return 0;
}

the output will be: 100

change printf("%d",*ptr); to this printf("%d",**ptr);

and this static int *p={x+2,x,x+3,x+4,x+1}; to this
static int *p[]={x+2,x,x+3,x+4,x+1};

Consider compiling your C code with GCC invoked with all warnings and debug info:
gcc -Wall -Wextra -g then using the GDB debugger

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
Asaf Itach
  • 300
  • 6
  • 13