I have the following code:
File1.c
int arr[10];
File2.c
extern int *arr;
int main()
{
arr[0] = 1;
return 0;
}
Please explain me why accessing the arr in File2.c will cause segmentation fault?
compiler:gcc
OS:Linux.
I have the following code:
File1.c
int arr[10];
File2.c
extern int *arr;
int main()
{
arr[0] = 1;
return 0;
}
Please explain me why accessing the arr in File2.c will cause segmentation fault?
compiler:gcc
OS:Linux.
Reason of error:
This code lead to segmentation fault
int * some_pointer;
...
some_pointer[0] = 1;
You are misunderstood:
The extern int *arr;
is declaration of a pointer.
The int arr[10];
is a declaration of an array.
They do not have any relative in your case.
You just need extern int arr[10];
in File2.c