错误:'我'未申报(首次使用此功能)

时间:2017-01-21 04:19:22

标签: c if-statement controls

我无法弄清楚为什么我会收到此编译器错误。我在for循环中声明了i,但我的控制语句没有看到它。

此行发生错误:if (mystring[cmd_index][i] == '\\')

/* 
 * Check for the " character in the mystring 
 * and remove the character if it doesn't have the delimiter
 */
for (int i = 0; i < strlen(mystring[cmd_index]); i++) {
    if (mystring[cmd_index][i] == '\\') {
        i++;
    } else
    if (mystring[cmd_index][i] == '"') {
        printf("HELLO");

        if (i != strlen(mystring[cmd_index] - 1)) {
            shiftLeft(mystring[cmd_index], i + 1, 1);
        } else {
            mystring[cmd_index][i] = '\0';
        }
    }
}

Edit1:我使用的是gcc版本5.4.0

Edit2:我复制了相同的代码并将其粘贴在原始代码的正下方。然后评论出原文。它正在编译。但是当我撤消它并使用原始代码时,它将无法再次编译。为什么?

2 个答案:

答案 0 :(得分:0)

我认为您可能遇到这个奇怪问题的主要原因是您也在for循环中更改i。我使用continue;跳过这些增量,让for循环成为一致。下面的代码试图删除双引号保留正斜杠,正如我从你的问题中所理解的那样。

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

int main(int argc, char* argv[])
{
    char mystring[3][10];
    memset(mystring, '\0', sizeof(mystring));

    memcpy(mystring[0], "He\\l\"lo\"", sizeof(mystring[0]));
    memcpy(mystring[1], "cr\\u\"el\"", sizeof(mystring[1]));
    memcpy(mystring[2], "wo\\r\"ld\"", sizeof(mystring[2]));

    int i;
    int j;
    int k;

    for(j = 0; j < 3; j++)
    {
        printf("Before: %s\n", mystring[j]);
        for(i = 0; i < strlen(mystring[j]); i++)
        {
            if(mystring[j][i] == '\\') // Skip Backward slashes
                continue;
            else if(mystring[j][i] == '"') // Remove double quotes
            {  
                for(k = i; k < strlen(mystring[j]); k++)
                    mystring[j][k] = mystring[j][k+1];
            }
        }
        printf("After: %s\n", mystring[j]);
    }
    return(0);
}

请注意,在使用gcc进行编译时,无需添加任何特定标记。

答案 1 :(得分:0)

Wy狂野猜测,您在原始;声明的末尾有一个额外的for,例如:

for (int i = 0; i < strlen(mystring[cmd_index]); i++);
{
    ...

这将有效地为for循环提供一个空体,后续块不在for循环的范围内。

{放在for行的末尾而不是单独的行上,以避免出现此类错误。