如何在void函数中插入for循环?

How to insert for loop in void function?

是否可以在void function中插入for loop

void decode(unsigned char* msg)
{

    int result[5];// can store our value
    unsigned char lala[50];

    if (strstr(msg, "UI01?") != msg) // write in Hyperterminal
    {
        sprintf(lala, "UI01 %d \r\n", result[0]); // UIO1 ADC Value
        sendString(lala);
    }
    else if (strstr(msg, "UI02?") != msg) // write in Hyperterminal
    {
        sprintf(lala, "UI02 %d \r\n", result[1]); // UIO2 ADC Value
        sendString(lala);
    }
    else if (strstr(msg, "UI03?") != msg) // write in Hyperterminal
    {
        sprintf(lala, "UI02 %d \r\n", result[2]); // UIO3 ADC Value
        sendString(lala);
    }
    else if (strstr(msg, "UI04?") != msg) // write in Hyperterminal
    {
        sprintf(lala, "UI02 %d \r\n", result[3]); // UIO4 ADC Value
        sendString(lala);
    }
}


int main()
{

    int result[5]; // can store four value
    int a = 0;     // start from UI0-UI4

    while (1)

    {
        for (a = 0; a < 5; a++)
        {
            AD1CHS0bits.CH0SA = a;  // UI0-UI4
            AD1CHS0bits.CH0NA = 0;  // Vreferences as channel 0 -ve input
            AD1CON1bits.ADON = 1;   // 1 = A/D Converter module is operating
            AD1CON1bits.SAMP = 1;   // sapling mode
            __delay32(50);          // delay after sampling
            AD1CON1bits.SAMP = 0;   //sampling bit  
            while (!AD1CON1bits.DONE);
            result[a] = ADC1BUF0;   // have 4 result
        }
    }

    if (U1STAbits.URXDA == 1) //check if data avilbe
     {
         rxbuf [len] = U1RXREG;
         if (rxbuf[len]== '\r')//press enter
         {
             sendChar (rxbuf[len]);
             sendChar ('\n'); //new line
             decode (rxbuf); // calling decode funtion
             len = 0 ;
         }
         else
         {
             sendChar (rxbuf[len]);
             len++;
         }         
     }
}

从上面的代码中,在void decode中启用代码UIO1?在超级终端中输入,它会回复ADC值。接着 UIO2? 直到 UIO4? .

while(1)循环中,代码用于扫描UIO1直到UIO4。实际上UIO1UIO4都有模拟值读入数字值。我用的是for loop,因为看起来比较简单

问题是,在 void decode 循环中,出现错误:下标值既不是数组也不是指针。如何读取 result 值?

main 中的

resultdecode 中的 result 是两个不同的变量。

如果您希望 decode 了解 main 中的 result,您应该将其作为参数传递:

void decode(unsigned char* msg, int result[5])
{
    // Remove declaration of result 
    // Keep the rest of the code
}

int main()
{
  /* other code */
            decode (rxbuf, result); // calling decode funtion
  /* other code */
}