繁体   English   中英

包含指针的 C 中的分段错误(核心转储)

[英]Segmentation fault (core dumped) in C that includes pointers

我正在做一个家庭作业项目,我们正在使用指针,似乎这些指针值在我的程序中的某些地方发生了变化,我找不到那个特定的位置。 rp是正在改变的指针。 我是指针的新手,所以我尝试以不同的方式取消引用它们,但我最终不确定。

这是我的代码

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

int getMenuChoice();
void generateCoords(int row, int cols, char[][cols], int *rp, int *cp);

int main() {
    int myChoice;
    int userDifficulty;

    srand(time(NULL));

    do {
        myChoice = getMenuChoice();

        if (myChoice == 1) {
            printf("\nEnter difficulty (1, 2, or 3): ");
            scanf("%d", &userDifficulty);

            int gridSize = 2;

            gridSize = userDifficulty * 2;

            char array[gridSize][gridSize];

            preSetBoard(gridSize, gridSize, array);
            generateCoords(gridSize, gridSize, array, x, y);
            //printf("%d %d", x, y);
        }
    } while (myChoice != 0);
    return 0;
}

int getMenuChoice() {
    int userInput;

    printf("***MEMORY!***\n");
    printf("1 - Play Game\n");
    printf("2 - Check Scores\n");
    printf("0 - EXIT\n");
    scanf("%d", &userInput);

    return userInput;
}

void generateCoords(int row, int cols, char array[][cols], int *rp, int *cp) {
    _Bool run = 1;

    srand(time(NULL));

    while (run) {
        int place1 = rand() % (row) + 1;

        rp = &place1;
        //printf("%d\n", *rp);

        int place2 = rand() % (row) + 1;
        cp = &place2;

        //printf("%d\n", *cp);
        if (array[*rp][*cp] == 'A') {
            run = 0;
        }
    }
}

预期输出是13之间的数字。

发布的代码中有多个问题:

您为坐标生成的随机数加1 :这可能会导致程序尝试访问超出其边界的 2D 数组。 索引值应 >= 0 和 < 大小。

此外,您不会将int变量的地址传递给generateCoords ,而是将xy未在发布的代码中定义。 这并没有真正造成问题,因为您修改了函数参数以指向局部变量。

这是一个更正的版本:

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

int getMenuChoice(void);
void presetBoard(int row, int cols, char[][cols]);
void generateCoords(int row, int cols, char[][cols], int *rp, int *cp);

int main() {
    int myChoice, userDifficulty, x, y;

    srand(time(NULL));

    while ((myChoice = getMenuChoice()) > 0) {
        if (myChoice == 1) {
            printf("\nEnter difficulty (1, 2, or 3): ");
            if (scanf("%d", &userDifficulty) != 1)
                break;

            int gridSize = userDifficulty * 2;
            char array[gridSize][gridSize];
            preSetBoard(gridSize, gridSize, array);
            generateCoords(gridSize, gridSize, array, &x, &y);
            printf("%d %d\n", x, y);
        }
    }
    return 0;
}

int getMenuChoice(void) {
    int userInput;

    printf("***MEMORY!***\n");
    printf("1 - Play Game\n");
    printf("2 - Check Scores\n");
    printf("0 - EXIT\n");
    if (scanf("%d", &userInput) != 1)
        userInput = 0;
    return userInput;
}

void generateCoords(int rows, int cols, char array[][cols], int *rp, int *cp) {
    int col, row;

    for (;;) {
        row = rand() % rows;
        col = rand() % cols;
        if (array[row][col] == 'A') {
            break;
        }
    }
    /* return found coordinates to the caller */
    *rp = row;
    *cp = col;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM