Typedef struct pointer to function (C)

I am trying to pass a typedef struct pointer to a function and the compiler is complaining with this error message: error: unknown type name ‘RootP’. Here is the code...

int main()
{
    typedef struct Root
    {
        struct Root *child;
    }*RootP;
    RootP rootNode = malloc(sizeof(struct Root));
    rootNode->child = NULL;
    ....

}

void mkdir(RootP rootNode, char param2[60], char pwd[200])
{
    ...
}

Answers


The struct should be outside of main, so move

typedef struct Root
{
    struct Root *child;
 }*RootP;

before the main function. If the program is big enough, consider moving that into some header file (*.h)

And I would avoid using the mkdir name. It is defined in Posix and on Linux refers to the mkdir(2) system call.

I don't feel that typedef struct Root *RootP; is pretty code: you usually want to see at a glance what C thing is a pointer. I would instead declare the struct root_st and have typedef struct root_st Root; (Gtk also uses that, or a very similar, coding convention). And code Root* rootnode. But it is debatable and a matter of taste.


Need Your Help

How do I maintain the 'correct' scope in javascript even listeners?

javascript typescript

I'm writing a geometry engine in Typescript, and I'm trying to implement drag&drop. I have a point object that inherets from a geometry object, and it has control of a circle element inside an ...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.