Hi all, I am working with the TI TUSB3200, trying to modify their example code for our own USB device. USB requires many tables which start with a length byte. I would like to use the sizeof operator to calculate this for me, as shown here:
byte code rictest[] = { sizeof(rictest), 1, 2 };
warning C198: sizeof returns 0
Hmm, I think you can make use of how Keil arranges/places variables in memory. They are not arranged in order of declaration, they are arranged alphabetically. So, if you declare: const unsigned char myarray[] = {1,2,3,4}; const unsigned int amyarraysize = sizeof(myarray) + 2/*include size of amyarraysize */); in one file, with no other declarations. amyarraysize will appear as the first in memory, followed by myarray. (Of cause myarray itself should no longer contain the size ...). Remember to move the pointer to amyarraysize, when copying. Now, Im not sure that it will always work, but I think so. If you really want to be sure, use the SECTIONS(?NC?YOURFILENAME?NCONST (addr)) to place it to a fixed place in memory Best regards Niels Sejersen
A very neat trick Niels, I'll remember that in future. It would probably be possible to base a solution on this trick. I now find that externally linked modules needed to know the length also, currently it is defined in a .H file. I could modify the external modules to read the length from the array, but I might just stick with writing a utility to auto-genaerate the .c and .h files, with the length calculated automatically and inserted into the source. This will make the source easier to read (& understand) when the project is complete.
View all questions in Keil forum