在c中使用*而不是 - >运算符

Sah*_*ani -1 c pointers linked-list syntax-error

我在C中创建了一个非常简单的链表列表程序.

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

int main(){
    struct Int{
        int num;
        struct Int *ptr;
    };
    typedef struct Int NODE;
    NODE *start;
    start = (NODE *) malloc(sizeof(NODE));
    (*start).num = 100;
    (*start).ptr = (NODE *) malloc(sizeof(NODE));

    (*start).(*ptr).num = 123;
    (*start).(*ptr).ptr = NULL;

}
Run Code Online (Sandbox Code Playgroud)

当我将最后两行替换为: -

start -> ptr -> num = 123;
start -> ptr -> ptr = NULL;
Run Code Online (Sandbox Code Playgroud)

错误解决了.

问题是为什么我不能用(* start).而不是.start ->根据这个答案这是什么意思" - >"? 两者都是一样的.

Mik*_*ike 11

你应该把最后两行写成:

(*(*start).ptr).num = 123;
(*(*start).ptr).ptr = NULL;
Run Code Online (Sandbox Code Playgroud)

因为(*ptr)不是其成员(*start),您应该访问ptr然后取消引用整个表达式.