简体   繁体   中英

Program not initializing extern variable in C

I'm trying to initialize an empty 2 dimensional array of pointers that point to data structures I have created known as "node"s. For some reason, I'm getting this scoping issue when I'm trying to modify the list, which is a crucial part of the program. A function called "repository_init" is supposed to truly initialize the array. However, I am using the array in other functions, which is why I'm getting the error. It doesn't recognize that the array exists. I'm new to C but after doing some research I found something on "extern", and the site told me that I could use extern to declare an empty variable. So, I declared my list as a global variable by putting it before main and writing it like so:

    extern node *main_list[][];
    #define MAX_LEVEL 10000

Here is the repository_init() function:

     void repository_init(int p){
         int new;
         new  = (max_range/2) + 5;
         max_height = 1;
         while(new > 1){
            new = (new * probability)/100;
            max_height++;
         }

         main_list[max_height][MAX_LEVEL];

         /*fill the array with empty values*/
     }

The error output to the screen is "error: array type has incomplete element type

extern node *main_list[][];"

I need this list to be global. I don't see what the problem is with just having the array initialized in a separate function. I've done this tons of times in other languages.

The problem is twofold: The first is that the declaration of main_list is incomplete, just as the compiler is telling you. You need sizes for the array(s) when declaring an array. The second problem is the expression main_list[max_height][MAX_LEVEL]; which uses this incomplete array. It doesn't set the sizes, it tries to access the array(s).

In your case it's probably best solved by having pointers to pointers. Something like

extern node ***main_list;

...

main_list = malloc(sizeof(node **) * max_height);
for (size_t i = 0; i < max_height; ++i)
    main_list[i] = malloc(sizeof(node *) * MAX_LEVEL);

What your declaring is not a array.

It's a pointer to 2D array . You can't declare a pointer to a 2D array without defining its size(at least rows while using pointers concept).

If you want 2D array declare it as following:-

extern node main_list[][];

If you want a pointer to 2D try using a triple pointer

extern node ***main_list;

Note:- array size is calculated at compile time so compiler has to know its size.

From C99 standard, section 6.2.5, article 20 -

An array type describes a contiguously allocated nonempty set of objects with a particular member object type, called the element type. Array types are characterized by their element type and by the number of elements in the array. An array type is said to be derived from its element type, and if its element type is T , the array type is sometimes called array of T .

It furthers says in the footnote #36 -

Since object types do not include incomplete types, an array of incomplete type cannot be constructed.

This means that conceptually there's no difference between 1-D array and 2-D array etc. They are all the same type - the array type . The following statement

extern node *main_list[][];

is intended to declare an array main_list of type node *[] of unknown size. The type node *[] is itself an array type but its size is not know and hence it's an incomplete type. You can declare an array of unknown size which will be resolved with its external definition, but you cannot declare an array of incomplete type as clearly stated in the standard which is quoted above. The type of the array element must be a complete type. This explains the error statement. Here is what I suggest -

// header file myheader.h

#define MAX_LEVEL 10000

extern node *main_list[][MAX_LEVEL];

I would suggest not to use variable names like new which is a keyword in many languages and may cause confusion. Also, you can initialize your array with NULL .

// myfile.c

void repository_init(int p) {
     int newval;
     newval  = (max_range / 2) + 5;
     max_height = 1;
     while(newval > 1) {
        newval = (newval * probability) / 100;
        max_height++;
     }

     // definition of the array. specifies the array size 
     // and allocates memory on the stack. size is resolved with
     // the extern declaration of the array main_list.
     // initialize all elements of the array to NULL. 

     node *main_list[max_height][MAX_LEVEL] = {0};  

     // process the array main_list
 }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM