Array Declaration problem

Thread Starter

Jswale

Joined Jun 30, 2015
121
Hi All,

Probably an easy mistake but I just can't find it...

Just setting up an array to receive my input from TeraTerm and store it. It originally comes in the RXBUF one character at a time.

When I run it, the compiler warns me that the array is declared but not used..


C:
ISR(USCI_A0, serial_interrupt0)
{
  static int r;
  char RX[5];

    switch(__even_in_range(UCA0IV,USCI_UART_UCTXCPTIFG))
    {
    case USCI_NONE:
    break;
    case USCI_UART_UCRXIFG:

    RX[r]= UCA0RXBUF; r++;
    if(r==5) break;
So when a character is received in the ISR, UCRXIFG is set and then the characters will be placed into the RX array one at a time by the variable 'r' incrementing the positions.
However, it doesn't work and looks like it doesn't recognise RX[] as an array...

Help would be appreciated,

JSwale.
 
Last edited:

WBahn

Joined Mar 31, 2012
30,072
Aside from r not being initialized (which is a problem that definitely needs to be fixed), you don't show enough code to determine if the array is ever used.

Assigning a value to a variable and then never using that variable will be caught as a warning by many compilers. So do you every USE the values that get stored in your RX[] array?
 

vpoko

Joined Jan 5, 2012
267
I'm not sure if some compilers are different (or what the ANSI standard says), but for the C/C++ compilers I've used, static local variables (along with global variables) don't need to be initialized; they'll be set to 0 upon declaration. Non-static local variables do need to be initialized. If the variable isn't used, it might throw a warning, but it shouldn't throw an error. I actually can't see any problem in the posted code fragment (except I have no idea what type UCA0RXBUF is, is it 8 bits wide?)

What's the exact error your compiler's throwing? Also, can you post the code to the end of the function?
 
Last edited:

WBahn

Joined Mar 31, 2012
30,072
I'm not sure if some compilers are different (or what the ANSI standard says), but for the C/C++ compilers I've used, static local variables (along with global variables) don't need to be initialized; they'll be set to 0 upon declaration. Non-static local variables do need to be initialized. If the variable isn't used, it might throw a warning, but it shouldn't throw an error. I actually can't see any problem in the posted code fragment (except I have no idea what type UCA0RXBUF is, is it 8 bits wide?)

What's the exact error your compiler's throwing? Also, can you post the code to the end of the function?
You are correct. Static and global variables are initialized to zero per the standard. There are C compilers that are not standards compliant and some of them have specifically NOT initialized static and global variables so that data can be saved and shared from one run to the next, which can be useful if the memory in question is nonvolatile such as Flash.

It sounds like the compiler is throwing a warning about the array not being used (generally meaning that it's not being read -- writing to it doesn't count as use).
 

Thread Starter

Jswale

Joined Jun 30, 2015
121
Thanks for the replies.

I have a little more information, the UCA0RXBUF is an unsigned short and receives the value and then I want to store it in the array. I don't want to use it yet, but will eventually use an IF statement to then set variables.....

C:
if (RX[3]==72) x=10;
When I debug... 'r' increments like it should AND the UCA0RXBUF is set to the value of the received char in decimal format BUT these values don't get placed into the array for some reason.

The rest of the function is just other cases that have no effect on the original code.

JSwale.
 

Brownout

Joined Jan 10, 2012
2,390
C:
ISR(USCI_A0, serial_interrupt0)
{
  static int r;
  char RX[5];
 
    switch(__even_in_range(UCA0IV,USCI_UART_UCTXCPTIFG))
    {
    case USCI_NONE:
    break;
    case USCI_UART_UCRXIFG:
 
    RX[r]= UCA0RXBUF; r++;
    if(r==5) break;
IN the last line, you shouldn't use a conditional statement for the "break" In a case statement, a break should be unconditional.
 

WBahn

Joined Mar 31, 2012
30,072
IN the last line, you shouldn't use a conditional statement for the "break" In a case statement, a break should be unconditional.
Depends entirely on what the logic of the following code is. From a style standpoint, I agree with you however. Then again, I'm not a proponent of every using break and continue statements in looping constructs, either.
 

WBahn

Joined Mar 31, 2012
30,072
Thanks for the replies.

I have a little more information, the UCA0RXBUF is an unsigned short and receives the value and then I want to store it in the array. I don't want to use it yet, but will eventually use an IF statement to then set variables.....

C:
if (RX[3]==72) x=10;
When I debug... 'r' increments like it should AND the UCA0RXBUF is set to the value of the received char in decimal format BUT these values don't get placed into the array for some reason.

The rest of the function is just other cases that have no effect on the original code.

JSwale.
Is the RX[] array ever used in an expression anywhere within this function?
 

WBahn

Joined Mar 31, 2012
30,072
Break is used to correctly exit each case. It shoud not be used within a condition.
The "break" statement is merely a control statement that performs an unconditional jump to the first line of code immediately following the smallest enclosing switch or iteration statement.

It is used within conditional statements all the time where the conditional statement is there precisely for the purpose of deciding whether to break out of the switch or iteration statement or to proceed within it.
 

Brownout

Joined Jan 10, 2012
2,390
The "break" statement is merely a control statement that performs an unconditional jump to the first line of code immediately following the smallest enclosing switch or iteration statement.

All things in context. I've only refereces the switch statement case.

It is used within conditional statements all the time where the conditional statement is there precisely for the purpose of deciding whether to break out of the switch or iteration statement or to proceed within it.
Maybe in iteration, not in swtich statements. in almost 20 years, I've never seen code use it that way. Maybe some clever programmer got away with it, but that doens't mean it's the proper way to construct a switch statement.
 
Last edited:

WBahn

Joined Mar 31, 2012
30,072
And notice that I specifically included the switch statement in my response. There is nothing syntactically or semantically wrong with putting a break statement in a conditional statement within a switch statement. Nothing at all. I might well argue that it is generally poor style, just as I would generally argue that goto statements should never be used unless there is a damn good reason. But damn good reasons DO exist and C is a language that is about performance, so it is not surprising that style-poor decisions are made all the time in the interest of performance.

Consider the following (admittedly contrived) problem:

Based on the character stored in variable ch, modify the value stored in x as described below:
If the character is a 'C', multiply the value stored in x by 4.
If the character is a 'B', multiply the value stored in x by 12.
If the character is an 'A', if the value in x is more than 5, double it, otherwise multiply it by 24.

There are many ways to implement this logic, but a perfectly valid one is

C:
switch (ch)
{
   case 'A': x *= 2.0;
             if (x > 10.0)
                break;
   case 'B': x *= 3.0;
   case 'C': x *= 4.0;
}
Now, this code is almost certainly a travesty from a performance standpoint, but that is because multiple multiplications are being done when a single one would suffice -- it would be trivial to construct another problem where that's not the case but similar logic applies. The point is that C allows the programmer great flexibility in deciding the flow-through behavior of the logic in a switch statement and programmers can and do use that flexibility for valid reasons to improve the performance of their code.
 

Brownout

Joined Jan 10, 2012
2,390
As I said before, maybe someone is clever enough to make something done improperly work, but at the cost of (in this case) performance or (in other cases) unintended errors. I've not needed such construction in years of professional work, nor have a ever witnessed code written this way professionally. There is certainly much better and more proper ways to perform the stated function. Let the OP look at the line indicated and see if that's what he intended. It just might be the solution to his problem.
 

ErnieM

Joined Apr 24, 2011
8,377
It is not uncommon for even baseline C compilers to toss away variables that are never used. This may include those that get a value assigned to them, but never read to see that value.

It's one simple optimization step, see if your compiler does this. Or just use the value somewhere to check if it comes back.
 
Top