c++ - C output differ with the same code -


#include <stdio.h> #include <stdlib.h>  int main() {      int* a[10];     int* p = a;     int = 0;     (p = &a[0], = 0; p < &a[10]; p++, i++)     {         *p = i;     }     (int = 0; < 10; i++)     {         printf("%d\n", a[i]);     } } 

the output on gcc using eclipse:

0 2 4 6 8 10 12 14 16 18 

the output using visual studio:

0 1 2 3 4 5 6 7 8 9 

why ?

a declared array of 10 pointers int. a in int* p = a; decay int **, p of type int *. compiler raise warning incompatible pointer assignment. need change declaration

int* a[10];   

to

int a[10]; 

this program invokes undefined behavior, strange things can happen.


Comments

Popular posts from this blog

python - Operations inside variables -

Generic Map Parameter java -

arrays - What causes a java.lang.ArrayIndexOutOfBoundsException and how do I prevent it? -