简体   繁体   中英

Segmentation fault (core dumped) in C that includes pointers

I am working on a homework project and we are using pointers and it seems that those pointer values are changed at some in my program and I can't find that specific spot. rp is the pointer that is changing. I am new to pointers so I tried de dereferencing them in different ways, but I am ultimately unsure.

Here is my code

#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;
        }
    }
}

The expected output is a number between 1 and 3 .

There are multiple issues in the posted code:

You add 1 to the random numbers generated for coordinates: this may cause the program to try and access the 2D array beyond its boundaries. Index values should be >= 0 and < size.

Furthermore, you do not pass addresses of int variables to generateCoords , but x and y which are not defined within the code posted. This does not really pose a problem since you modify the function arguments to point to local variables.

Here is a corrected version:

#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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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