断开循环时的C变量范围问题

时间:2014-02-26 21:17:38

标签: c scope

我在Mac上编写的C程序中有类似下面的代码。 这里应该支持bool类型,因为我相信OSX确实使用了C99。

我的问题是,当我在循环中将找到的变量设置为true时,它仍会在之后的条件中变为false。这里只需要第二双眼睛来帮助我理解我的范围问题。谢谢!

编辑:添加了for循环中出现的代码。

int main(int argc, const char * argv[])
{
    bool found = false;
    int response;
    unsigned char buf[64];
    #define MAX_STR 255
    wchar_t wstr[MAX_STR];
    hid_device *device;
    int i;

for (i = 0; i < 24; i++)
{

    buf[0] = 0x02; // reportID
    buf[1] = 0x08;
    buf[2] = 0x00;
    buf[3] = 0x00;
    buf[4] = 0x00;
    buf[5] = 0x00;
    buf[6] = 0x10;
    buf[7] = 0x00;
    buf[8] = 0x18;

    printf("\nBytes to Write \n");

    for (int i = 0; i < 9; i++)
    {
        printf("0x%02x ", buf[i]);
    }

    //send acces start command
    bool result = hid_write(device, buf, 9);
    if (!result)
    {
        printf("Device Error");
        printf("Error: %ls\n", hid_error(device));
        return 1;

    }

    // Read start command response from device
    response = hid_read(device, buf, AccessStartResonposeSize);

    // Log out our response
    printf("\n");
    printf("\nResponse Size: %i\n", response);
    for (int i = 0; i < 25; i++)
    {
        printf("0x%02x ", buf[i]);
    }

    if (response < 0)
    {
        printf("Unable to read()\n");
    }

    if (buf[1] == 0xFF) {
        printf("Device Error buf[1] == 0xFF");
        printf("Error: %ls\n", hid_error(device));
        return 1;
    }

    if (buf[0] == 0x08 &&
        buf[6] == 0x00)
    {
        printf("Device Error buf[0] == 0x08 && buf[6] == 0x00 \n");
        printf("Error: %ls\n", hid_error(device));
        return 1;
    }

    if (buf[0] != 0x18)
    {
        continue;
    }

    if (buf[6] != 0x00 ||
        buf[7] != 0x00 ||
        buf[8] != 0x00 ||
        buf[9] != 0x94 ||
        buf[10] != 0x00 ||
        buf[11] != 0x03)
    {
        printf("unknown model \n");
        printf("Error:  %ls\n", hid_error(device));
    }
    printf("counter %i", i);

    found = TRUE;
    break;

    if(!found){
        printf("device not found");
        return 1;
    }

    return 0;
}

0 个答案:

没有答案