字符串打印导致C程序崩溃

时间:2014-11-05 21:08:39

标签: c arrays string multidimensional-array

以下代码由于某种原因而崩溃,产生以下消息“Process returned -1073741819(0xC0000005)”。调试器说这行有一个分段错误:

printf("|%s|", grid[a][b]);

但是,我认为这不是意味着以前必须出错的实际错误。由于我对C中的字符串和数组不太熟悉,你可以查看我的代码,看看你是否能找到错误。

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

#define VALUES1 3
#define VALUES2 3
#define NAMELEN 10

void printgrid(char grid[VALUES1][VALUES2]);
void play(char grid[VALUES1][VALUES2], char player);
int game(void);


int main() {

    return game();
}


int game() {
    char grid[VALUES1][VALUES2], player, turn, names[2][NAMELEN];
    int a, b;

    for (a = 0; a < 2; ++a){
        printf("Please enter the name of player number %d\n", a + 1);
        scanf("%s", names[a]);
    }

    for (a = 0; a < VALUES1; ++a){
        for (b = 0; b < VALUES2; ++b){
            grid[a][b] = ' ';
        }
    }

    for (turn = 9; turn > 0; --turn){
        if (turn % 2 == 1){
            player = names[0];
        }
        else {
            player = names[1];
        }
        play(grid, player);
        printf("\n");
        printgrid(grid);
        printf("\n");
    }

    return 0;
}


void printgrid(char grid[VALUES1][VALUES2]) {
    int a, b;

    for (a = 0; a < VALUES1; ++a) {
        for (b = 0; b < VALUES2; ++b) {
            printf("|%s|", grid[a][b]);
        }
        printf("\n");
    }
}


void play(char grid[VALUES1][VALUES2], char player) {
    int row, column;

    printf("Enter the row number:\n");
    scanf("%d", &row);
    printf("Enter the column number:\n");
    scanf("%d", &column);

    --row;
    --column;

    grid[row][column] = player;
}

编辑: 使用%c输出

|þ||þ||þ|
|±||±||þ|
|±||±||þ|

1 个答案:

答案 0 :(得分:2)

问题在于您使用player。您将其指定为char,但仍尝试为其分配char*值。您可以在整个功能中将charchar *混合使用。目的很明确。您希望在游戏过程中使用name[0]name[1]中的第一个字符来构建游戏网格。你可以这样做,但你必须非常小心

此外,编译代码,始终打开警告。他们会立即引导您解决代码中的问题:

$ gcc -Wall -Wextra -o bin/play play.c

play.c: In function ‘game’:
play.c:36:20: warning: assignment makes integer from pointer without a cast [enabled by default]
            player = names[0];
                    ^
play.c:39:20: warning: assignment makes integer from pointer without a cast [enabled by default]
            player = names[1];
                    ^
play.c: In function ‘printgrid’:
play.c:56:13: warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘int’ [-Wformat=]
            printf("|%s|", grid[a][b]);
            ^

player的使用带来一致性的一个解决方案是,当您需要使用char *作为字符时,将其声明为dereference然后player *player完整形式*(player + 0)相当于player[0])解决方案如下:

int game() {
    char grid[VALUES1][VALUES2], turn, names[2][NAMELEN];
    char *player;
    int a, b;

    for (a = 0; a < 2; ++a){
        printf("Please enter the name of player number %d\n", a + 1);
        scanf("%s", names[a]);
    }

    for (a = 0; a < VALUES1; ++a){
        for (b = 0; b < VALUES2; ++b){
            grid[a][b] = ' ';
        }
    }

    for (turn = 9; turn > 0; --turn){
        if (turn % 2 == 1){
            player = names[0];
        }
        else {
            player = names[1];
        }
        play(grid, *player);
        printf("\n");
        printgrid(grid);
        printf("\n");
    }

    return 0;
}


void printgrid(char grid[VALUES1][VALUES2]) {
    int a, b;

    for (a = 0; a < VALUES1; ++a) {
        for (b = 0; b < VALUES2; ++b) {
            printf("|%c|", grid[a][b]);
        }
        printf("\n");
    }
}

<强>输出:

$ ./bin/play
Please enter the name of player number 1
Don
Please enter the name of player number 2
Millie
Enter the row number:
1
Enter the column number:
2

| ||D|| |
| || || |
| || || |
...
Enter the row number:
3
Enter the column number:
1

|M||D||D|
|D||M||M|
|D||M||D|

顺便说一下,您可以使用简单的printgrid语句改进||的输出,以消除内部列中的双if

void printgrid(char grid[VALUES1][VALUES2]) {
    int a, b;

    for (a = 0; a < VALUES1; ++a) {
        for (b = 0; b < VALUES2; ++b) {
            if (b == 0)
                printf("|%c|", grid[a][b]);
            else
                printf("%c|", grid[a][b]);
        }
        printf("\n");
    }
}

<强>输出:

|M|D|D|
|D|M|M|
|D|M|D|