c数组未使用但如果未声明,程序运行错误

时间:2018-01-06 21:42:39

标签: c arrays

我已经声明了一个数组b[3],虽然我没有在程序的任何地方使用它,但如果没有它,程序将无法正常工作。它仅打印出用户之前创建的任何内容或用户之前创建的任何内容。我不知道为什么会发生这种情况或如何在没有b[3]的情况下使程序正常运行。这是我的代码:

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

main() {
    char a[100]="man";
    char b[3];

    printf ("A man is bitten by a spider and becomes spiderman.\n"
            "So in this motive start making stuff.\nTo end the program "
            "just click X on top right of the window\n\n");
    printf ("When you create sth really big, you go back to only having "
            "a man.\n\n");
    unsigned char f=1,x=3,i;

    while(1){
        printf ("You have a %s\n",a);
        /*presentation of the menu*/
        printf ("Press:\n"
                "\t1 for what you have to bite a spider.\n"
                "\t2 for what you have to bite a man.\n"
                "\t3 for what you have to bite spiderman.\n"
                "\t4 for what you have to get bitten by a spider.\n"
                "\t5 for what you have to get bitten by a man.\n"
                "\t6 for what you have to get bitten by spiderman.\n"
                "\t0 to go from the start\n");
        scanf("%hhu",&f);
        switch(f){
        case 0:
            strcpy(a,"man");
            x=2;
            break;
        case 1:
            x+=6;
            if(x<=100){
            strcat(a,"spider");
            }
            break;
            case 2:
            x+=3;
            if(x<=100){
            strcat(a,"man");
            }
            break;
            case 3:
            x+=9;
            if(x<=100){
            strcat(a,"spiderman"); 
            }
            break;
            case 4:
            x+=7;
            if(x<=100){
                for(i=x;i>=6;i--){
                    a[i]=a[i-6];
                }
                a[0]='s';
                a[1]='p';
                a[2]='i';
                a[3]='d';
                a[4]='e';
                a[5]='r';
            }
            break;
            case 5:
            x+=4;
            if(x<=100){
                for(i=x;i>=3;i--){
                    a[i]=a[i-3];
                }
                a[0]='m';
                a[1]='a';
                a[2]='n';
            }
            break;
            case 6:
            x+=10;
            if(x<=100){
                for(i=x;i>=9;i--){
                    a[i]=a[i-9];
                }
                a[0]='s';
                a[1]='p';
                a[2]='i';
                a[3]='d';
                a[4]='e';
                a[5]='r';
                a[6]='m';
                a[7]='a';
                a[8]='n';
            }
            break;
        }
        if(x>100){
            strcpy(a,"man");
            x=2;
        }
    }
}

输入:

1  

输出(b[3]):

You have a manspider  

输出(不含b[3]):

You have a spider  

3 个答案:

答案 0 :(得分:0)

您应该首先使用其他方式从键盘读取,scanf在从键盘读取时并不理想,特别是因为您似乎只是在读取整数。

使用fgets可确保缓冲区中没有剩余字符可能会触发对switch语句的意外附加调用,并防止缓冲区溢出。由于您希望将输入转换为整数,请执行以下操作:

char buffer[32];
if (fgets(buffer, sizeof(buffer), stdin) != NULL)
{
  int f = atoi(buffer);  // or sscanf(buffer, "%d", &f);

  switch (f) 
  {...}
}

strcat不使用strcpy,而是使用更安全的strcpy_s和strcat_s来确保目标缓冲区a具有足够的大小。我的猜测是你不小心超出了一个因为scanf会导致switch语句添加比你想要的更多的字符(调试器会比我的直观调试更好地解决这个问题)

e.g.
strcpy_s(a, sizeof(a), "man");

另请注意,在0的情况下:即使字符串“man”为3,也将x设置为2.

答案 1 :(得分:0)

此代码可正常运行:

#include <stdio.h>
#include <string.h>
main(){
char a[100]="man";
printf("A man is bitten by a spider and becomes spiderman.\nSo in this       motive start making stuff.\nTo end the program just click X on top right of the window\n\n");
printf("When you create sth really big, you go back to only having a man.\n\n");
unsigned char x=3,i;
char f=1;
while(1){
    printf("You have a %s\n",a);
    printf("Press:\n\t1 for what you have to bite a spider.\n\t2 for what you have to bite a man.\n\t3 for what you have to bite spiderman.");
    printf("\n\t4 for what you have to get bitten by a spider.\n\t5 for what you have to get bitten by a man.\n\t6 for what you have to get bitten by spiderman.\n\t0 to go from the start\n");
    scanf(" %c",&f);
    switch(f){
       case '0':
        strcpy(a,"man");
        x=3;
        break;
       case '1':
        x+=6;
        if(x<=100){strcat(a,"spider");}
        break;
        case '2':
        x+=3;
        if(x<=100){strcat(a,"man");}
        break;
        case 3:
        x+=9;
        if(x<=100){strcat(a,"spiderman");}
        break;
        case '4':
        x+=6;
        if(x<=100){
            for(i=x;i>=6;i--){
                a[i]=a[i-6];
            }
            a[0]='s';
            a[1]='p';
            a[2]='i';
            a[3]='d';
            a[4]='e';
            a[5]='r';}
        break;
        case '5':
        x+=3;
        if(x<=100){
            for(i=x;i>=3;i--){
                a[i]=a[i-3];
            }
            a[0]='m';
            a[1]='a';
            a[2]='n';}
        break;
        case '6':
        x+=9;
        if(x<=100){
            for(i=x;i>=9;i--){
                a[i]=a[i-9];
            }
            a[0]='s';
            a[1]='p';
            a[2]='i';
            a[3]='d';
            a[4]='e';
            a[5]='r';
            a[6]='m';
            a[7]='a';
            a[8]='n';}
        break;}
    if(x>100){
        strcpy(a,"man");
        x=2;}
        }
}

问题在于%hhu。通过将f声明为char而不是unsigned char并执行所有其他必要的更改,程序可以正常运行。

答案 2 :(得分:-1)

您可以尝试以下代码! 无论有没有数组'b [3]',该程序都能完美运行。 主要问题是你使用scanf(),strcpy()&amp; strcat()而不是scanf_s(),strcpy_s()&amp; strcat_s()。 我附上了程序输出的截图。

#include <stdio.h>
#include <string.h>
#include <conio.h>
void main() {
    char a[100] = "man";


    printf("A man is bitten by a spider and becomes spiderman.\n"
        "So in this motive start making stuff.\nTo end the program "
        "just click X on top right of the window\n\n");
    printf("When you create sth really big, you go back to only having "
        "a man.\n\n");
    unsigned char f = 1, x = 3, i;

    while (1){
        printf("You have a %s\n", a);
        /*presentation of the menu*/
        printf("Press:\n"
            "\t1 for what you have to bite a spider.\n"
            "\t2 for what you have to bite a man.\n"
            "\t3 for what you have to bite spiderman.\n"
            "\t4 for what you have to get bitten by a spider.\n"
            "\t5 for what you have to get bitten by a man.\n"
            "\t6 for what you have to get bitten by spiderman.\n"
            "\t0 to go from the start\n");
        scanf_s("%hhu", &f);
        switch (f){
        case 0:
            strcpy_s(a, "man");
            x = 2;
            break;
        case 1:
            x += 6;
            if (x <= 100){
                strcat_s(a, "spider");
            }
            break;
        case 2:
            x += 3;
            if (x <= 100){
                strcat_s(a, "man");
            }
            break;
        case 3:
            x += 9;
            if (x <= 100){
                strcat_s(a, "spiderman");
            }
            break;
        case 4:
            x += 7;
            if (x <= 100){
                for (i = x; i >= 6; i--){
                    a[i] = a[i - 6];
                }
                a[0] = 's';
                a[1] = 'p';
                a[2] = 'i';
                a[3] = 'd';
                a[4] = 'e';
                a[5] = 'r';
            }
            break;
        case 5:
            x += 4;
            if (x <= 100){
                for (i = x; i >= 3; i--){
                    a[i] = a[i - 3];
                }
                a[0] = 'm';
                a[1] = 'a';
                a[2] = 'n';
            }
            break;
        case 6:
            x += 10;
            if (x <= 100){
                for (i = x; i >= 9; i--){
                    a[i] = a[i - 9];
                }
                a[0] = 's';
                a[1] = 'p';
                a[2] = 'i';
                a[3] = 'd';
                a[4] = 'e';
                a[5] = 'r';
                a[6] = 'm';
                a[7] = 'a';
                a[8] = 'n';
            }
            break;
        }
        if (x>100){
            strcpy_s(a, "man");
            x = 2;
        }
    }
    _getch();
}

enter image description here