开发者

Two or more data types in declaration specifiers error [closed]

开发者 https://www.devze.com 2022-12-17 02:40 出处:网络
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.

This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.

Closed 6 years ago.

Improve this question

I am pretty new to C.

I am getting this error:

incompatible implicit declaration of built-in function ‘malloc’

Even when I fix the code based on the answers to include <stdlib.h>, I still get:

two or more data types in declaration specifiers

开发者_StackOverflow

When trying to do this:

struct tnode
{
    int data;
    struct tnode * left;
    struct tnode * right;
}

struct tnode * talloc(int data){
    struct tnode * newTnode;
    newTnode = (struct tnode *) malloc (sizeof(struct tnode));
    newTnode->data = data;
    newTnode->left = NULL;
    newTnode->right = NULL;
    return newTnode;
}

How do I fix it?


You have to put ; behind the struct declaration:

struct tnode
{
    int data;

    struct tnode * left;
    struct tnode * right;
}; // <-- here


Your original error was because you were attempting to use malloc without including stdlib.h.

Your new error (which really should have been a separate question since you've now invalidated all the other answers to date) is because you're missing a semicolon character at the end of the struct definition.

This code compiles fine (albeit without a main):

#include <stdlib.h>

struct tnode
{
    int data;

    struct tnode * left;
    struct tnode * right;
};

struct tnode * talloc(int data){
    struct tnode * newTnode;
    newTnode = (struct tnode *) malloc (sizeof(struct tnode));
    newTnode -> data = data;
    newTnode -> left = NULL;
    newTnode -> right = NULL;
    return newTnode;
}


"Implicit declaration" means that you're trying to use a function that hasn't been formally declared.

You probably forgot: #include <stdlib.h> which includes the function declaration for malloc.


Do you have the appropriate header file included?

That is, is there a line at the top of your file that says

#include <stdlib.h>

Hope this helps.


Ensure you have included the header file that contains the definition for malloc():

#include "stdlib.h"
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号