namesake22 namesake22 - 4 years ago 73
C Question

C type * (blank) declaration (binary tree, case in point)

Here is a piece of code that's confusing me.

struct tNode {
int key; // search key for this item
int data; // data for this item
struct tNode *left, *right; // children
};

typedef struct tNode tree_type;

/**************** tree_new() ****************/
/* Create a new tree */
tree_type *
tree_new(const int key, const int data)
{
tree_type *node = malloc(sizeof(struct tNode));

...
}
}


So I'm not quite getting the syntax for
tree_type *
right above tree_new function. Doesn't seem like a pointer... not sure what it's doing there.

--------------------- post-edit
Thanks to all who helped out!

Answer Source

You have to read both lines together:

tree_type *
tree_new(const int key, const int data)

is just another legal way to write the function definition with its return type:

tree_type *tree_new(const int key, const int data)

since it doesn't end with a semicolon, the statement continues on the next line(s).

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download