We are running a survey to help us improve the experience for all of our members. If you see the survey appear, please take the time to tell us about your experience if you can.
Hello all, I have the following situation and error message that I'm not quite able to wrap my head around. I have a module, let's call it table.c that has something like the following. code const entry_type table[] = {...}; code const unsigned int tablesize = sizeof(table); Of course, ... is replaced by a bunch of table data. In another module, call it access.c, I have the following: extern code const entry_type table[]; extern code const unsigned int tablesize; code const unsigned int entrysize = sizeof(entry_type); code const unsigned int numentries = tablesize/entrysize; I receive an error C247 when trying to compile the line where I calculate numentries. Oddly enough, it doesn't complain about calculating entrysize, so it's good with making that a code constant. Nor does it have a problem in table.c creating the tablesize code constant. I'm not sure why then it wouldn't think that numentries could be a code constant. I've also tried making sure that table.c is compiled before access.c (just a blind attempt) and that didn't help. Any help would be greatly appreciated. Best Regards, Jay Daniel
Given that information, is there any other way in access.c that I could create code constants for the number of entries? Not if you want to use it to e.g. initialize variables with static duration or size arrays with it. A const variable declared by a header file, with its definition found elsewhere, doesn't qualify as a "constant" for the purposes of C programming, because it's not known at compile time. Only the linker knows the value or even actual address of such a variable, but by the time the linker runs, it's too late. You could use the expression
tablesize / sizeof(table[0])
it is actually dead simple in a .h file everybody include #define ARRAY_SIZE 123 in the file with the array arrayname[ARRAY_SIZE] Erik
Hans-Bernhard, Thanks for the explanation. I actually wanted some way to size another array to match it, but have resigned my self to working around this another way. I figured it was worth asking though.