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:
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;
}
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 line
Code:
Printf("%d %d",a[1],a[2]);
// It has an error name:invalid type int[int] for array subscript
I have google but I don't know so much about this problem.
So, who can help me, why It's wrong, please.
thanks so much