I have two code, one is true, and other is wrong. (the purpose of this work is test, how to change an array in function.
First is:
and second is:Code:#include <stdio.h> int array(int a,int b,int *x); int main(){ int *a; array(5,6,a); printf("%d %d\n",a[1],a[2]); } int array(int a, int b,int *x){ x[1]=a; x[2]=b; }
First code is true (it will print 5 6), but second code is wrong. It's wrong at lineCode:#include <stdio.h> int array(int a,int b,int *x); int main(){ int a; array(5,6,&a); printf("%d %d\n",a[1],a[2]); } int array(int a, int b,int *x){ x[1]=a; x[2]=b; }I have google but I don't know so much about this problem.Code:Printf("%d %d",a[1],a[2]); // It has an error name:invalid type int[int] for array subscript
So, who can help me, why It's wrong, please.
thanks so much![]()



LinkBack URL
About LinkBacks




A "bad pointer" is reference to random address. It hard to see problem when debug small problem, but it will be really an awful event if run this module in big project. (Am I thinking right
:"> ) 