4

In file1.c, I have the array

const uint8 myArray[] =
{
    0x4b, 0x28, 0x05, 0xbf,   
    ...
    0xff, 0xff, 0xff, 0xff
};

In file2.c, I need to use the array as follows:

uint8* buffer = myArray;
uint32 length = ???

I've tried length = sizeof(myArray), but this results in the following error:
error: invalid application of ‘sizeof’ to incomplete type ‘const uint8[] {aka const unsigned char[]}’. Since it is constant, I could physically count the number of entries, but I need to do it programmatically because this constant is likely to change further on in development.

3
  • use a #define for the size of the array....#define ARRAY_SIZE x then const uint8 myArray[ARRAY_SIZE] = { ... }; Commented May 17, 2019 at 20:18
  • 3
    It's probably an incomplete type because you're just picking up a declaration of the array (through a header file you haven't shown here) rather than the definition of the array. If you create a const myArrayLength in file1.c, you should be able to use sizeof there. Commented May 17, 2019 at 20:20
  • 1
    The definition of myArray is not visible in file2.c. Only the declaration is (though it seems you left that out in your question?), so you cannot get the size. Commented May 17, 2019 at 20:20

1 Answer 1

4

In file1.c, export the length:

const size_t myArrayLength = sizeof(myArray);

And then add a declaration somewhere (in a header file for file1.c or maybe directly in file2.c) like:

extern const size_t myArrayLength;
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.