--- In [email protected], "David Hamill" <[EMAIL PROTECTED]> wrote:
>
> Bill asked:
> > Does this complete the tree or node structure?
>
> K&R use typedefs to tidy things up:
>
>
> typedef struct tnode *Treeptr;
>
> typedef struct tnode { /* the tree node */
> char *word; /* points to the text */
> int count; /* number of occurrences */
> Treeptr left; /* left child */
> Treeptr right; /* right child */
> } Treenode;
I don't know who these K&R guys are :-) but declaring typedefs which
are pointers can cause problems. I would do:
typedef struct TreeNode_s TreeNode;
struct TreeNode_s
{
void *data;
TreeNode *left;
TreeNode *right;
};
The problem is that if you include the * in the typedef, you can't
then make a pointer to const using the typedef. For example, if you have:
typedef char *string; /* string type */
const char name[] = "john"; /* a constant string */
string nameStr = name; /* warning here */
Compiled under gcc you get the expected warning:
warning: initialization discards qualifiers from pointer target type
for the indicated line, because 'nameStr' is a pointer to non-const,
and it is being pointed at the const data 'name'. But if you do:
const string nameStr = name;
instead, this only makes the pointer 'nameStr' const, not what it is
pointing at, so the warning is still issued.
AFAIK the only way around this (without using casts) is to have 2
typedefs:
typedef char *string; /* non-const string */
typedef const char *constString; /* const string */
constString nameStr = name;
This removes the warning, but I think it's bad.
John