【问题标题】:initializing a typedef struct pointer to NULL将 typedef 结构指针初始化为 NULL
【发布时间】:2013-01-07 05:09:16
【问题描述】:
#include<stdio.h>

typedef struct student{
int id;
int mark;
}stud;

typedef struct stud *s1;

void main(){
s1 = NULL;
printf("hi");
}

请帮助我如何将结构指针初始化为 NULL。我在编译期间收到以下错误。

graph.c: In function ‘main’:  
graph.c:11:04: error: expected identifier or ‘(’ before ‘=’ token

【问题讨论】:

    标签: pointers struct initialization typedef


    【解决方案1】:

    您的意思是将变量 s1 定义为

    stud *s1;
    

    现场演示:http://ideone.com/9ThCDi

    您遇到错误的原因是您将s1 声明为“指向结构螺柱的指针”的类型。这是错误的,原因有两个:

    1. 你不希望s1 成为一个类型;你希望它成为一个对象。
    2. 你的结构是struct student。但是您定义了一个名为 stud 的实际类型。

    【讨论】:

      【解决方案2】:

      使用 struct student *s1;

      而不是

      typedef struct stud *s1;

      据我所知,仅在定义自定义数据类型时使用 typedef。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多