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

Placing size of array at start of array

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
};
This gives the error
warning C198: sizeof returns 0
, so plainly the compiler only does a single pass through the code, and does not know the array size until it has finished parsing the entire array definition. :(

Is there any other way to achieve this? I presume it would be possible with some sort of pre-processor macro to count the number of elements manually before defining the aray...

Parents Reply Children
  • Or, just create the array with an empty first element:

    unsigned char array [] = 
      { 0, 1, 2, 3, 4, ... };
    

    and then initialize the size in the program:

    .
    .
    .
    array[0] = sizeof (array);
    .
    .
    .
    

    Jon

  • Sorry guys, I didn't spell out all my requirements.

    The table is in read-only ROM, so I can't modify the first element later on.

    I want to experiment adding and removing entries to these tables, so the length will vary. It seems very labour intensive (and error-prone) to manually count the bytes each time.

    I was trying to think of some way to make a macro to somehow do a dummy define on the data to work out the size, and then do the real define once it knows the answer, but I can't think of a way to do this without really creating the table (so I would end up with two copies of it).

    I suppose the last resort solution would be to write a pre-processor in Visual Basic to construct the C source file for me...

  • You could do something like...

    #include <string.h>
    
    static unsigned char code descriptor [] =
      { 1,2,3,4,5,6,7 };
    
    unsigned char xdata xdescriptor [1 + sizeof (descriptor)] =
      { sizeof (descriptor), };
    
    void main (void)
    {
    memcpy (&xdescriptor[1], descriptor, sizeof (descriptor));
    }
    

    You could then refer to the xdescriptor.

    That would make your life easier but would use CODE AND XDATA.

  • "read-only ROM"

    What other sort of ROM is there?! ;-)

  • Can you make use of strings?

    #define RICTEST_STR "\x1\x2"
    
    struct rictest_t
    {
    	byte size;
    	char str[sizeof(RICTEST_STR)];
    };
    
    struct rictest_t rictest = { sizeof(struct rictest_t), RICTEST_STR};
    
    Of course there will be terminating zero, but maybe you can put up with that.
    Regards,
    Mike

  • Andrew said:
    : "read-only ROM"
    :
    : What other sort of ROM is there?! ;-)

    What's wrong with a bit of redundancy? :~)
    It fits in well with "LCD display", "DOS operating System", "PIN number", "USB bus" ...

  • "What's wrong with a bit of redundancy? :~)"

    It's not redundancy, it's tautology!

    "It fits in well with 'LCD display', 'DOS operating System', 'PIN number', 'USB bus' ...

    Yes, it's exactly the same error - they're all just as bad! ;-)