简体   繁体   English

功能指针-变量未初始化

[英]Function Pointer - variable not initialized

#include <stdio.h>

int Calc(int n1, int n2, int(*func) (int n1, int n2))
{
    return func(n1, n2);
}

int Plus(int n1, int n2)
{
    return n1 + n2;
}

int Minus(int n1, int n2)
{
    return n1 - n2;
}

int main(void)
{
    int n1, n2;
    int result;
    char mark;

    printf("입력 : ");
    scanf_s("%d", &n1);
    printf("choose function : ");
    scanf_s("%c", &mark, sizeof(mark));
    printf("입력 : ");
    scanf_s("%d", &n2);

    switch (mark)
    {
    case '+':
        result = Calc(n1, n2, Plus);
    case '-':
        result = Calc(n1, n2, Minus);
    }
    printf("%d\n", result);

    return 0;
}

I`m studying Function Pointer in C with simple code. 我正在用简单的代码学习C语言中的函数指针。 I made it initialize variable result from using scanf_s, but it send me a error message that inform me variable result is not initialized. 我通过使用scanf_s使其初始化了变量结果,但它向我发送了一条错误消息,通知我变量结果未初始化。 How can I fix this code? 如何解决此代码?

You have two problems: 您有两个问题:

1. you leave '\\n' in the input buffer after scanf_s("%d", &n1); 1.scanf_s("%d", &n1);之后,将'\\n'留在输入缓冲区中scanf_s("%d", &n1); which is then taken as your input to scanf_s("%c", &mark, sizeof(mark)); 然后将其作为您对scanf_s("%c", &mark, sizeof(mark)); You must remove the trailing newline before attempting read with "%c" . 您必须先删除结尾的换行符,然后才能尝试使用"%c"进行读取。 A simple empty_stdin function will do, 一个简单的empty_stdin函数就可以了,

eg 例如

#include <stdio.h>

void empty_stdin ()
{
    int c = getchar();

    while (c != '\n' && c != EOF)
        c = getchar();
}
...
    printf("입력 : ");
    scanf_s("%d", &n1);
    empty_stdin();           /* remove all extraneous chars from stdin */
    printf("choose function : ");
    scanf_s("%c", &mark, sizeof(mark));

2. You fail to include break at the end of each switch case causing automatic fall-through to the next switch case, you need, 2.您无法在每个switch盒的末尾添加break ,从而导致自动掉线到下一个switch盒,您需要,

eg 例如

    switch (mark)
    {
    case '+':
        result = Calc(n1, n2, Plus);
        break;
    case '-':
        result = Calc(n1, n2, Minus);
        break;
    }

Putting it altogether, you could do: 综上所述,您可以执行以下操作:

#include <stdio.h>

void empty_stdin ()
{
    int c = getchar();

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

int Calc(int n1, int n2, int(*func) (int n1, int n2))
{
    return func(n1, n2);
}

int Plus(int n1, int n2)
{
    return n1 + n2;
}

int Minus(int n1, int n2)
{
    return n1 - n2;
}

int main(void)
{
    int n1, n2;
    int result = 0;
    char mark;

    printf("입력 : ");
    scanf_s("%d", &n1);
    empty_stdin();
    printf("choose function : ");
    scanf_s("%c", &mark, sizeof(mark));
    printf("입력 : ");
    scanf_s("%d", &n2);

    switch (mark)
    {
    case '+':
        result = Calc(n1, n2, Plus);
        break;
    case '-':
        result = Calc(n1, n2, Minus);
        break;
    }
    printf("%d\n", result);

    return 0;
}

( NOTE: you must also verify the return of each scanf to insure you have valid input -- that is left to you. Also note, you could ignore leading whitespace -- including '\\n' -- by including a space before your format specifier, eg " %c" , but that doesn't protect against a matching failure from your prior input -- ideally, you should empty_stdin() following each input with scanf to protect against that case and ready the input buffer for your next input.) 注意:您还必须验证每个scanf返回值 ,以确保有有效的输入-留给您。也请注意,您可以忽略前导空格 -包括'\\n' ,方法是在格式之前添加一个空格说明符,例如" %c" ,但是不能防止先前输入的匹配失败-理想情况下,您应该在每个输入之后用scanf empty_stdin()以防止这种情况,并为下一个输入准备好输入缓冲区。)

Example Use/Output 使用/输出示例

$ ./bin/fnpointer
입력 : 2
choose function : +
입력 : 3
5

您尚未初始化结果变量,您对n1,n2使用了scanf_s,但是对结果没有执行任何操作,因此编译器会抱怨

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

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