在引用指针时遇到段错误

时间:2017-06-28 17:32:37

标签: c function pointers

我正在参加C编程课程,我试图理解指针,以及它们究竟是如何工作的。我尝试将变量counter传递给gameRuntime();,但counter并未在while循环后返回。所以,我选择使用指针。而现在,我比我开始时更加混乱。

代码:

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

int *gameRuntime(int *counter)
{
    srand(time(NULL));
    int random = rand() % 100 + 1;
    printf("> %d <\n", random);
        int guess;
        *counter = 1;
        int local_counter = (*counter);
        while ((guess != random) && (local_counter <= 10))
        {
            printf("\n-----------------------\n");
            printf("What is your guess?\n-----------------------\n> ");
            scanf("%d", &guess);

            if ((guess > 100) || (guess < 1))
            {
                printf("Please choose a number between 1 and 100\n");
            }
            else if (guess < random)
            {
                printf("My number is larger than %d!\n", guess);
                    local_counter++;
                printf("%d guesses left!\n", (11 - local_counter));
            }
            else if (guess > random)
            {
                printf("My number is less than %d!\n", guess);
                    local_counter++;
                printf("%d guesses left!\n", (11 - local_counter));
            }
            else if (guess == random)
            {
                printf("You guessed it! My number was %d!\n", random);
                printf("You found it with just %d guess(es)!\n", local_counter);
            }
        }
        (*counter) = local_counter;
        return counter;
}

int main()
{
    char quit;
    int counter;
    int random;

    printf("Welcome to the Number Guess Game!\n");
    printf("I chose a number between 1 and 100 and you have only 10 chances to guess it!\n\n");
    printf("Continue? [Y\\N]\n\n> ");
    scanf("%c", &quit);
    if ((quit == 'n') || (quit == 'N'))
    {
        printf("Exiting....");
            return 0;
    }
    else
    {
        printf("\n=*=+=*=+=*=+=*==*=+=*=+=*=+=*==*=+=*=+=*=+=*==*=+=*=+=+*\n");
        printf("   ~       ~       ~       Ready?       ~       ~       ~   \n");
        printf("=*=+=*=+=*=+=*=+=*=+=*=+=*=+=*=+=*=+=*=+=*=+=*=+=*=+=*=+=*\n");
        printf("\n");
        printf("\n-----------------------");
        printf("\nOk, I've made up my mind!\n");
        printf("-----------------------\n");
    }

    gameRuntime(counter);
    printf("\n---%d---\n", counter);

    char continueGame;
    while ((continueGame != 'N') || (continueGame != 'n'))
    {
        printf("\n---%d---\n", counter);
        if (counter >= 10)
        {
            printf("SORRY! You could not find my number with 10 guesses!\n");
            printf("My number was %d\n", random);
            printf("Maybe next time!\n");
            printf("\nTry again? [Y\\N]\n");
            scanf(" %c", &continueGame);

            if ((continueGame == 'Y') || (continueGame == 'y'))
            {
                gameRuntime(counter);
            }
            else
            {
                printf("Thanks for playing! See you later!");
                    return 0;
            }
        }
        else
        {
            printf("Play again? [Y\\N]\n> ");
            scanf(" %c", &continueGame);

            if ((continueGame == 'Y') || (continueGame == 'y'))
            {
                gameRuntime(counter);
            }
            else
            {
                printf("\nThanks for playing! See you later!");
                    return 0;
            }
        }
    }
}

任何帮助都会非常感激,TIA

1 个答案:

答案 0 :(得分:2)

更改

gameRuntime(counter);

gameRuntime(&counter);

您正在传递counter的值,然后您使用它就像是某个地址(您取消引用指针)的地址。它确实编译,因为指针和int在c中是可互换的,但这并不意味着定义了行为。但是,它应该生成关于指向int转换的指针的警告。您还可以将所有警告转换为错误,以防止在出现警告时进行编译。

在极少数情况下可以忽略警告,因此根据经验,您的编译器会对其可能的一切发出警告,如果可能,请将警告视为错误。

通过应用建议的修复,您传递counter的地址,这是一个指针,其中包含counter的位置,因此您可以通过取消引用指针来影响该位置的值