尝试清除缓冲区后继续出现分段错误?

时间:2014-04-07 21:51:46

标签: c segmentation-fault function-pointers

您好我正在编写一个简单的代码来帮助我自我理解函数指针,但后来由于" \ n"我遇到了另一个问题,试图清除缓冲区。字符被转移,我试图清除缓冲区但遇到另一个问题,当我运行代码我得到分段错误,当else语句为真时,为什么这?有人可以指导我,我可以清除缓冲区或更好的处理方法,谢谢。

               /*
                *      funcptrs.c
                *
                *      Program to demonstrate the use of function pointers
                *
                *      by Mvitagames
                */
                #include <stdio.h>
                #include <stdlib.h>

                static void goodbye() {

                  printf("\nPress ENTER to exit: ");
                  fflush(stdin);
                  getchar();

                }

                static int add(int a, int b) { return a + b;}

                static int subtract (int a, int b) { return a - b;}

                int main() {
                  int  i, j;
                  int  result;
                  int  (*func_ptr)(int , int );
                  int ch;
                  char buf[BUFSIZ];

                  atexit(goodbye);

                  printf("Please enter the first number: ");
                  scanf("%d", &i);
                  printf("Please enter the second number: ");
                  scanf("%d", &j);


                  while ((ch = getchar()) != '\n' && ch != EOF);

                  printf("Would you like to add or subtract (a/s)? ");
                  if (getchar() == 'a')
                    func_ptr = add;
                  else
                    //printf("I got here");
                    func_ptr = subtract;

                   result = func_ptr(i,j);

                   printf("The result is %d\n", result);


                   return (0);
                  }

1 个答案:

答案 0 :(得分:1)

一种解决方案是使用fgets读取输入行,然后使用sscanf进行任何转换。这避免了scanf在输入缓冲区中留下杂散字符的问题。结果代码如下所示

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

#define MAXL 1024
static char line[MAXL];

static void goodbye() {

    printf("\nPress ENTER to exit: ");
    fgets( line, MAXL, stdin );
}

static int GetIntFromUser( char *prompt )
{
    int result;

    printf( "%s", prompt );
    fflush(stdout);

    if ( fgets( line, MAXL, stdin ) == NULL )
        exit( 1 );
    if ( sscanf(line, "%d", &result) != 1 )
        exit( 1 );

    return( result );
}

static char GetCharFromUser( char *prompt )
{
    char result;

    printf( "%s", prompt );
    fflush(stdout);

    if ( fgets( line, MAXL, stdin ) == NULL )
        exit( 1 );
    if ( sscanf(line, " %c", &result) != 1 )
        exit( 1 );

    return( result );
}

static int add(int a, int b) { return a + b;}

static int subtract (int a, int b) { return a - b;}

int main() {
    int  i, j;
    int  result;
    int  (*func_ptr)(int , int );
    char ch;

    atexit(goodbye);

    i = GetIntFromUser( "Please enter the first number: " );
    j = GetIntFromUser( "Please enter the second number: " );
    ch = GetCharFromUser( "Would you like to add or subtract (a/s)? " );

    if ( ch == 'a' )
        func_ptr = add;
    else
        func_ptr = subtract;

    result = func_ptr(i,j);

    printf("The result is %d\n", result);

    return (0);
}