This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Compiler error C247

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

Parents
  • 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])

    to refer to the number of entries of that array, in, say, a for() loop over it, even from "access.c". But you can't use it in all the same ways you could apply a manifest constant like, say, 1000.

Reply
  • 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])

    to refer to the number of entries of that array, in, say, a for() loop over it, even from "access.c". But you can't use it in all the same ways you could apply a manifest constant like, say, 1000.

Children