Using a 2d array in multiple tabs

I am trying to use a 2d array in multiple tabs.

  • Tab 1: array declaration
    unsigned int array_data[2][data_i];

  • Tab 2: array initialization
    The array initialization happens within a while loop and I can get it to work when I put the declaration into Tab 2.
    array_data[0][data_index] = ai;
    array_data[1][data_index] = timerCounts;
    data_index++;

  • Tab 3: display array content. I am using Putty instead of the Arduino serial monitor
    extern array_data[2][data_i];
    for (int a=0; a <= data_index; a++){
    Serial.print(array_data[0][a]);Serial.print(array_data[1][a]);
    }

The use of the extern keyword does not compile; not using the declaration produces the error message: “array is not declared in this sope”. Is there a way to declare, initialize an retrieve data from an array using multiple tabs (files)?

Thanks,
Thomas

Try something like this, make sure data_i is a constant, declared in both tabs.

  • Tab 1: array declaration unsigned int array_data[2][data_i];

  • Tab 3 - (see what you're missing) extern unsigned int array_data[][data_i];

What I would do is put the following into a .h file, and #include the file in all the tabs.

define DATA_I 10 // whatever.

extern unsigned int array_data[][DATA_I];

Defining and declaring a variable are not the same thing and appreciating the difference between the two makes questions like these so much easier to understand. When you define a variable, storage is allocated for it. When you declare a variable, you are simply constructing an attribute list, usually to aid type checking. Data declarations do not cause storage to be allocated. Therefore if you define an array in file 1:

unsigned int array_data[2][data_i];

but wish to use it in some other file, you must declare it in the other file. This is the purpose of the extern keyword:

extern unsigned int array_data[][data_i];

The extern keyword yields a data declaration and makes it possible for the compiler to construct an attribute list for the variable so it can be used in a file other than the one in which it is defined. For more details, see:

http://www.drdobbs.com/cpp/computer-programming-and-precise-termino/208808373

Thanks for the suggestions and link. Using "extern" causes the error message "undefined reference" during compilation. I have started to look into the use of pointers, as well and will try that next.

To re-iterate, what you said:

  • Tab 3: display array content. I am using Putty instead of the Arduino serial monitor extern array_data[2][data_i];

What I said:

  • Tab 3 - (see what you're missing) extern unsigned int array_data[][data_i];

See the difference? I don't think you need to use pointers here.

I have used you suggestions "extern unsigned int array_data[][data_i];", but with no success. Could this be related to the version of the compiler?

Thanks.

No, that's just standard C syntax. Did you also properly declare data_i in the tab files, as I mentioned earlier? What I said earlier, I tested myself before posting, and it worked.