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
  • The reason that sizeof doesn't do what you want is because the object you are requesting the size of isn't defined, yet. So, its size is unknown.

    If you change your code to the following:

    byte code rictest[12] =
    {
    	sizeof(rictest),
    	1,
    	2
    };
    

    It should work.

    If you are creating the VID and PID tables for USB, you can create structs for each of these and then you may use the sizeof operator to initialize the VID or PID structure.

    Jon

Reply
  • The reason that sizeof doesn't do what you want is because the object you are requesting the size of isn't defined, yet. So, its size is unknown.

    If you change your code to the following:

    byte code rictest[12] =
    {
    	sizeof(rictest),
    	1,
    	2
    };
    

    It should work.

    If you are creating the VID and PID tables for USB, you can create structs for each of these and then you may use the sizeof operator to initialize the VID or PID structure.

    Jon

Children