Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/58.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 类型转换指针到结构时出错_C_Pointers_Struct - Fatal编程技术网

C 类型转换指针到结构时出错

C 类型转换指针到结构时出错,c,pointers,struct,C,Pointers,Struct,上面的代码是用来声明和初始化指向结构ll(链表)的指针的。 以粗体显示的行在编译期间显示以下警告: 数据定义没有类型或存储类[默认情况下启用]***, 在“root”的声明中,type默认为“int”不允许在C中的任何函数之外有语句(声明除外)。 由于您尚未为root指定类型,因此错误消息显示root的类型默认为int。显然这不是我想要的 将代码放入函数中(例如,main())并编译 另外, 1) 不要强制转换malloc()的结果,因为它是 2) 在紧接着分配NULL时,您丢失了分配的指针(使

上面的代码是用来声明和初始化指向结构ll(链表)的指针的。 以粗体显示的行在编译期间显示以下警告: 数据定义没有类型或存储类[默认情况下启用]***,
在“root”的声明中,type默认为“int”

不允许在C中的任何函数之外有语句(声明除外)。 由于您尚未为
root
指定类型,因此错误消息显示
root
的类型默认为
int
。显然这不是我想要的

将代码放入函数中(例如,
main()
)并编译

另外,

1) 不要强制转换
malloc()
的结果,因为它是
2) 在紧接着分配
NULL
时,您丢失了分配的指针(使用
malloc()
)。但我不确定你打算用它做什么。

3) 包括您使用的库函数的标题(
malloc
exit

(struct ll)malloc(sizeof(struct ll));*这是编译错误吗?忽略实际问题,调用某个
ll
是您讨厌代码维护者的十个标志之一…=)在C语言中,声明不是语句(C++在这里是不同的)。
struct ll
{
    int data;
    struct ll *next;
};

struct ll *ptr,*root;
root = (struct ll *)malloc(sizeof(struct ll)); /* This line */

if(root == NULL)
{
    printf("FAILURE");
    exit(0);
}


root = NULL;
ptr = NULL;