Pointer-array-extern question

后端 未结 5 615
遥遥无期
遥遥无期 2020-12-13 10:02

File 1.c

int a[10];

File main.c:

extern int *a;

int main()
{
    printf(\"%d\\n\", a[0])         


        
相关标签:
5条回答
  • 2020-12-13 10:37

    A bit late, a duplicate of this problem was just entered (and closed). The answers here don't mention header files...

    The problem would be caught at compile time if you put the declaration of array a in a header file, where it belongs, instead of putting it in the .c file. The header file should then be included into both .c files and the compiler can see that what you have declared is wrong.

    Your header file would contain:

    extern int myarray[];
    

    You will get something like "error: conflicting types for a" if you declare a as a pointer instead.

    0 讨论(0)
  • 2020-12-13 10:42

    Well explained in the C FAQ. And there is a followup. The picture in the second link is worth a million bucks.

    char a[] = "hello";
    char *p = "world";
    

    enter image description here

    Short answer: use extern int a[].

    0 讨论(0)
  • 2020-12-13 10:44

    Arrays decompose, or are implicitly converted to pointers when passed to a function as an argument, or when converted to an r-value on the right-hand-side of the assignment operator. So something like:

    int array[10];
    int* a = array;  //implicit conversion to pointer to type int
    
    void function(int* a);
    function(array);  //implicit conversion to pointer to type int
    

    works just fine. But that does not mean that arrays themselves are pointers. So if you treat an array like a pointer as you've done, you're actually treating the array type as-if it was a pointer that held the address to an int object. Since your array is actually a sequence of int objects, and not pointers to int objects, you're actually trying to dereference to some memory location that isn't pointing to anywhere valid (i.e., the first slot in array is a numerical integer value like 0 which would be like dereferencing a NULL). So that is why you're segfaulting. Note that if you had done something like this:

    int array[] = { 1, 2, 3, 4, 5};
    int b = *array;
    

    That still works, since array is again implicitly converted to a pointer to the memory block that is holding a sequence of integer values and is then dereferenced to get the value in the first sequence. But in your case, by declaring your array to the current code module as an externally defined pointer, and not an array, it will skip over the implicit conversion to a pointer that is normally done, and just use the array object as-if it were a pointer to an object itself, not an array of objects.

    0 讨论(0)
  • 2020-12-13 10:48

    Check out the output of the following code.

    File1.c

    #include <stdio.h>
    
    extern int* my_arr;
    
    void my_print()
    {
      printf("%d", my_arr);
    }
    

    main.c

    #include <stdio.h>
    
    int my_arr[2] = {1,2};
    
    extern void my_print();
    
    void main()
    {
        my_print();
    }
    

    output

    1
    

    inside File1.c my_arr is a pointer variable that has the value of 1. meaning the 1st element of my_arr[] was assigned to it. Then if you use *my_arr to access memory location ox1, you get seg fault because you are not allowed to access ox01.

    Why my_arr pointer was assigned 1 (the first element of my_arr[])?

    Has to do with how assembler works. Read this article

    Why your code can't access 0x01 ?

    I know it has to do with the operating system not allowing some address space to be accessed by user code. Thats all I know. google it if you want more info.

    0 讨论(0)
  • 2020-12-13 10:52

    Basically you'd need to write your main.c like this:

    extern int a[];
    
    int main()
    {
        printf("%d\n", a[0]);
        return 0;
    }
    
    0 讨论(0)
提交回复
热议问题