I have the following snippet from some foo.c file:
unsigned char foo[] = {1, 2, 3, 4};
void copy_foo(unsigned char *dest)
{
memcpy(dest, foo, sizeof(foo));
}
And I want to declare a bar array in another C file (main.c) that has the same size as foo, to hold the output of copy_foo:
int main(void)
{
unsigned char bar[?];
copy_foo(bar);
return 0;
}
Is it possible to get access to sizeof(foo) from main.c at compile time, so that it could be used to declare the bar array?
The usual "solution" to this problem is to use some sort of heap allocation to allocate memory at runtime, but I am looking for a static, compile time solution, as some embedded systems prohibit the use of dynamic allocation.
Of course, I can add #define FOO_SIZE 4 to foo.h, but then if foo changes FOO_SIZE will have to be manually changed as well.
Is this possible?