We are running a survey to help us improve the experience for all of our members. If you see the survey appear, please take the time to tell us about your experience if you can.
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?
Another way to approach the problem would be with a little bit of preprocessing. Here's what I mean:
1. List your numerous strings in a plain old text file, with one string on each line. To match the example you gave, the file would look like this:
"string1", "string2", "string3.5"
2. #include this file in your C program, thus:
CCHARPTR numerous_strings[] = { #include "mystrings.txt" };
3. Write a small program that counts the strings in the text file and spits out a tiny header file containing something like
#define NUMBER_OF_NUMEROUS_STRINGS 3
4. #include the header file wherever you need to.
5. Set up your build environment (makefile, batch files, IDE, or whatever) to run the C program just before each build.
This is probably overkill for what you are doing, but the general technique can be extremely useful in more complicated situations.
-- Russ