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

Unbounded Arrays and the sizeof operator

I take it using sizeof() on a array that is unbounded is a problem (I am getting sizeof returns size of type 0 warnings).

I have defined a constant array of pointers unbounded IE

#define CCHARPTR code char *
CCHARPTR numerous_strings[] =
{
"string1",
"string2",
"string3.5"
};


If I wished to know the number of entries in numerous_strings I thought I could do this

(sizeof(numerous_strings)/sizeof(CCHARPTR))


Obviously I was wrong since the compiler warns sizeof(numerous_strings) is zero. How can I get the size of this array because I would like to save time in having to write a 'magic' number for the number of numerous_strings. Sounds simple, unbounded arrays seem to be what it has issues with. Suggestions anyone?

Parents
  • So my real 'need' is to have a literal representing the number of elements of an unbounded array.

    That can be done, but only indirectly. You have to define the array and the length (macro) separately, then rely on either a hack (compile-time Assert()) or external tools (e.g. Lint) to tell you if they ever run out of synch. Lint will warn you right away if NELEM in

    int array[NELEM] = {
      /*...*/
    }
    

    doesn't correspond to the actual number of entries in the initializer.

Reply
  • So my real 'need' is to have a literal representing the number of elements of an unbounded array.

    That can be done, but only indirectly. You have to define the array and the length (macro) separately, then rely on either a hack (compile-time Assert()) or external tools (e.g. Lint) to tell you if they ever run out of synch. Lint will warn you right away if NELEM in

    int array[NELEM] = {
      /*...*/
    }
    

    doesn't correspond to the actual number of entries in the initializer.

Children
No data