[英]C Compiler throwing error: called object ‘random’ is not a function or function pointer
[英]Compiler throwing error for function return type returning structure pointer
有人可以解释为什么我在下面的代码中得到编译错误。
错误提示:
“第7行的'struct'之前的预期非限定ID”。
我的代码:
struct node{
int data;
struct node *left;
struct node *right;
};
( struct node *) createNode(int num)
{
struct node *newNode;
newNode = (struct node *) malloc(sizeof(struct node));
newNode->data = num;
newNode->left = NULL;
newNode->right = NULL;
return newNode;
}
更改
( struct node *) createNode(int num)
至
struct node * createNode(int num)
记住,您要指定返回类型 。 您不是在typecast
。
那就是
C
中malloc()
和family的返回值。 malloc()
的返回值是否成功。 函数: ( struct node *) createNode(int num)
语法无效。 指向struct node*
指针是struct node*
的返回类型。 似乎您可能已经认为必须将其强制转换为struct类型。 那是没有必要的。 此外,无需在C中malloc
。将其更改为此。
struct node* createNode(int num)
{
/* ... */
}
不必要,但是为了节省每次键入struct
好处,更好的是,您可以使用typedef
定义一个新类型。
typedef struct node Node;
struct node{
int data;
Node *left;
Node *right;
};
返回结构时,您无需为此加上括号。 所以简单地你给
struct node * createNode(int num){
...
}
放置不正确的括号。
您不必在函数定义的返回类型中使用括号。 我认为您对函数定义感到困惑,这里的几个链接:
http://www.tutorialspoint.com/cprogramming/c_functions.htm
对于函数返回指针
http://www.tutorialspoint.com/cprogramming/c_return_pointer_from_functions.htm
类型转换只能用于常量,变量,函数调用(如果返回某些内容)。
显式类型转换规则:
http://www.tutorialspoint.com/cprogramming/c_type_casting.htm
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.