[英]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.