C题,左边的'->'必须指向类/结构/联合/泛型类型?

发布于 2024-09-03 13:00:03 字数 2571 浏览 4 评论 0原文

试图理解为什么这不起作用。我不断收到以下错误: '->nextNode' 的左边必须指向 class/struct/union/generic 类型
(还有函数 new_math_struct 中带有 -> 的所有行)

头文件

#ifndef MSTRUCT_H
#define MSTRUCT_H

    #define PLUS 0
    #define MINUS 1
    #define DIVIDE 2
    #define MULTIPLY 3
    #define NUMBER 4

    typedef struct math_struct
    {
        int type_of_value; 
        int value;
        int sum;
        int is_used;
        struct math_struct* nextNode;
    } ;

    typedef struct math_struct* math_struct_ptr;
#endif

C 文件

int get_input(math_struct_ptr* startNode)
{
    /* character, input by the user */ 
    char input_ch; 
    char* input_ptr;

    math_struct_ptr* ptr;
    math_struct_ptr* previousNode;

    input_ptr = &input_ch;
    previousNode = startNode;

    /* as long as input is not ok */ 
    while (1)
    {             
        input_ch = get_input_character();
        if (input_ch == ',') // Carrage return
            return 1;
        else if (input_ch == '.') // Illegal character
            return 0;


        if (input_ch == '+')
            ptr = new_math_struct(PLUS, 0);
        else if (input_ch == '-')
            ptr = new_math_struct(MINUS, 0);
        else if (input_ch == '/')
            ptr = new_math_struct(DIVIDE, 0);
        else if (input_ch == '*')
            ptr = new_math_struct(MULTIPLY, 0);
        else
            ptr = new_math_struct(NUMBER, atoi(input_ptr));

        if (startNode == NULL)
        {
            startNode = previousNode = ptr;
        }
        else
        {
            previousNode->nextNode = ptr;
            previousNode = ptr;
        }
    } 
    return 0;
}

math_struct_ptr* new_math_struct(int symbol, int value)
{
    math_struct_ptr* ptr;
    ptr = (math_struct_ptr*)malloc(sizeof(math_struct_ptr));
    ptr->type_of_value = symbol;
    ptr->value = value;
    ptr->sum = 0;
    ptr->is_used = 0;    
    return ptr;
}

char get_input_character()
{
    /* character, input by the user */ 
    char input_ch; 

    /* get the character */ 
    scanf("%c", &input_ch);     

    if (input_ch == '+' || input_ch == '-' || input_ch == '*' || input_ch == '/' || input_ch == ')')
        return input_ch; // A special character
    else if (input_ch == '\n')
        return ','; // A carrage return
    else if (input_ch < '0' || input_ch > '9')
        return '.'; // Not a number          
    else
        return input_ch; // Number
}

C 文件的头仅包含对结构头和定义的引用的功能。语言C .

Trying to understand why this doesn't work. I keep getting the following errors:
left of '->nextNode' must point to class/struct/union/generic type
(Also all the lines with a -> in the function new_math_struct)

Header file

#ifndef MSTRUCT_H
#define MSTRUCT_H

    #define PLUS 0
    #define MINUS 1
    #define DIVIDE 2
    #define MULTIPLY 3
    #define NUMBER 4

    typedef struct math_struct
    {
        int type_of_value; 
        int value;
        int sum;
        int is_used;
        struct math_struct* nextNode;
    } ;

    typedef struct math_struct* math_struct_ptr;
#endif

C file

int get_input(math_struct_ptr* startNode)
{
    /* character, input by the user */ 
    char input_ch; 
    char* input_ptr;

    math_struct_ptr* ptr;
    math_struct_ptr* previousNode;

    input_ptr = &input_ch;
    previousNode = startNode;

    /* as long as input is not ok */ 
    while (1)
    {             
        input_ch = get_input_character();
        if (input_ch == ',') // Carrage return
            return 1;
        else if (input_ch == '.') // Illegal character
            return 0;


        if (input_ch == '+')
            ptr = new_math_struct(PLUS, 0);
        else if (input_ch == '-')
            ptr = new_math_struct(MINUS, 0);
        else if (input_ch == '/')
            ptr = new_math_struct(DIVIDE, 0);
        else if (input_ch == '*')
            ptr = new_math_struct(MULTIPLY, 0);
        else
            ptr = new_math_struct(NUMBER, atoi(input_ptr));

        if (startNode == NULL)
        {
            startNode = previousNode = ptr;
        }
        else
        {
            previousNode->nextNode = ptr;
            previousNode = ptr;
        }
    } 
    return 0;
}

math_struct_ptr* new_math_struct(int symbol, int value)
{
    math_struct_ptr* ptr;
    ptr = (math_struct_ptr*)malloc(sizeof(math_struct_ptr));
    ptr->type_of_value = symbol;
    ptr->value = value;
    ptr->sum = 0;
    ptr->is_used = 0;    
    return ptr;
}

char get_input_character()
{
    /* character, input by the user */ 
    char input_ch; 

    /* get the character */ 
    scanf("%c", &input_ch);     

    if (input_ch == '+' || input_ch == '-' || input_ch == '*' || input_ch == '/' || input_ch == ')')
        return input_ch; // A special character
    else if (input_ch == '\n')
        return ','; // A carrage return
    else if (input_ch < '0' || input_ch > '9')
        return '.'; // Not a number          
    else
        return input_ch; // Number
}

The header for the C file just contains a reference to the struct header and the definitions of the functions. Language C.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

殊姿 2024-09-10 13:00:03

由于您的 math_struct_ptr 已包含指针声明符,因此您无需在使用时指定它。删除*

math_struct_ptr ptr;

或者写

struct math_struct *ptr;

Since your math_struct_ptr already contains the pointer declarator, you don't need to specify it at the point of use. Drop the *:

math_struct_ptr ptr;

Or write

struct math_struct *ptr;
逆蝶 2024-09-10 13:00:03

math_struct_ptr* 是一个 math_struct** - 您正在创建一个指向带有嵌套星号的指针的指针。 typedef 的优点是您不必在 math_struct_ptr 之后添加 *,因此您可以将其省略

math_struct_ptr* is a math_struct** - you're creating a pointer to a pointer with nested stars there. The advantage of your typedef is that you don't have to put a * after math_struct_ptr, so you can just leave that out

乖乖 2024-09-10 13:00:03

你 typedef

 typedef struct math_struct* math_struct_ptr;

所以如果你使用 math_struct_ptr* ,你会得到一个指向指针的指针。在这种情况下,您可能只需要 math_struct_ptr 。我不会用 typedef 隐藏指针,但我看到很多人这样做。我觉得这很令人困惑。这可能是一个品味问题。

You typedef

 typedef struct math_struct* math_struct_ptr;

So if you use math_struct_ptr*, you get a pointer to a pointer. You probably just want math_struct_ptr in that case. I would not hide the pointer with typedef, but I see many people do that. I find it just confusing. It's probably a matter of taste.

我很OK 2024-09-10 13:00:03

他们的 typedef 解析方式,您将使用:

struct math_struct * *

而不是

struct math_struct *

直接使用 math_struct_ptr typedef 。

They way you're typedef resolves, you will be using:

struct math_struct * *

rather than

struct math_struct *

Just use the math_struct_ptr typedef directly.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文