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

number of strings in array

Hello all,

I have the following code:

const char BaudRate[][] = {"600","1200","2400","4800","9600","19200","38400","56700"};
. .
.

Idx = 0;
while (Idx++ < (sizeof(BaudRate)/sizeof(BaudRate[0]))) CheckSpeedOfLink(BaudRate[Idx]);

This code is an attempt to programmatically fill a array with selected text.

Can anyone help ?

Regards

Harry

Parents
  • What is the problem, exactly?
    My guess is that you want either this:

    const char BaudRate[][6] = { ...
    


    or this:

    const char* BaudRate[] = { ...
    


    There is a difference between the two. See your favorite textbook on the C programming language for details.

Reply
  • What is the problem, exactly?
    My guess is that you want either this:

    const char BaudRate[][6] = { ...
    


    or this:

    const char* BaudRate[] = { ...
    


    There is a difference between the two. See your favorite textbook on the C programming language for details.

Children
  • It's normally easier to keep an array of baudrates as numbers and not strings, since the baudrate-setting code will need a number when computing what register values to use for the baudrate generator.

  • Do you intend to skip the first entry in the array and access the one beyond the last?

    Check your loop index value.

  • I often have a define:

    #define NELEM(a) (sizeof(a)/sizeof(*(a)))
    


    since it is quite often I want loops to iterate through all elements of an array. The bad thing is that sizeof() only works if the initialization of the array is seen, so the compiler at that specific point knows the total size of the array.

    The other alternative is to have the last array element contain a zero and instead look for this zero to break the loop. This works even in a function that just gets a reference to the array, in which case sizeof() doesn't knows the total size of the array.