0

I've got some problem here with struct. I have create a struct and a function to try to understand how the tree data structure works.Now the problem is that when I try to compile the following code with the command gcc test.c -o test, the compiler always return the error message error: expected ‘;’, ‘,’ or ‘)’ before ‘&’ token, please give me some help.

#include <stdio.h>
#include <stdlib.h>

typedef struct BiTNode
{
    int data;
    struct BiTNode *lchild, *rchild;
}BiTNode, *BiTree;

void createTree(BiTree &T)
{
    int i;
    scanf("%d", &i);
    if(i == -1)
    {
        T = NULL;
    }
    else
    {
        T = (BiTNode *)malloc(sizeof(BiTNode));
        T->data = i;
        createTree(T->lchild);
        createTree(T->rchild);
    }
}

int main(void)
{
    BiTNode* root = NULL;
    createTree(root);
    return 0;
}
5
  • BTW, BiTNode in typedef struct BiTNode is redundant. Commented Oct 1, 2014 at 16:28
  • Do you expect root to be bon NULL after the createTree(root); statement ? Commented Oct 1, 2014 at 16:30
  • @HolyBlackCat, actually it is not redundant. If you paste the code in and compile (after fixing the declaration as @Ashalynd and @Elliott suggest) then the explicit BiTNode in the typedef struct BiTNode prevents the following compiler warning on the two lines: createTree(T->lchild); createTree(T->rchild); : warning: incompatible pointer types passing 'struct BiTNode *' to parameter of type 'BiTree'. At least that's what my compiler tells me. Give it a try. Commented Oct 1, 2014 at 16:38
  • What are you trying to achieve actually ? Commented Oct 1, 2014 at 16:49
  • @DarrenStone Oops, you're right. Commented Oct 1, 2014 at 16:54

2 Answers 2

5

The correct signature is:

void createTree(BiTree T)

and not:

void createTree(BiTree &T)

If fact, you can't use & in C function signatures. The way you did it would be valid in C++, that supports references. C does not do that.

You can use a pointer (e.g. createTree(BiTNode* T) ) in the signature, though, but in your case it's not needed, because you have already specified that BiTree is a pointer to BiTNode.

Important update: this change will make your function compile, but in order for it to work, you also need to make sure that you can actually use the pointer created inside that function. There are two ways to do that:

1) Return the new pointer as a result:

BiTree createTree()
{
    BiTree T;
    int i;
    scanf("%d", &i);
    if(i == -1)
    {
        T = NULL;
    }
    else
    {
        T = (BiTNode *)malloc(sizeof(BiTNode));
        T->data = i;
        T->lchild = createTree();
        T->rchild = createTree();
    }
    return T;
}

int main(void)
{
    BiTNode* root = NULL;
    root = createTree();
    return 0;
}

2) Give a pointer to BiTree as function argument (perhaps that was what you were meaning to do with the reference).

void createTree(BiTree *T)
{
    int i;
    scanf("%d", &i);
    if(i == -1)
    {
        *T = NULL;
    }
    else
    {
        *T = (BiTNode *)malloc(sizeof(BiTNode));
        (*T)->data = i;
        createTree(&(*T)->lchild);
        createTree(&(*T)->rchild);
    }
}

int main(void)
{
    BiTNode* root = NULL;
    createTree(&root);
    return 0;
}
Sign up to request clarification or add additional context in comments.

8 Comments

To be clear, C only has pointers. It does not support references.
Yes, that's what I meant to say.
@Ashalynd : Shouldn't the signature be void createTree(BiTree *T) ??
@Ashalynd : with your solution root will be NULL after the call to createTree and the call to createTree would be pointless. And also after createTree(T->lchild), T->lchild would be unchanged.
@Ashalynd : Obviously the program should also work and not only compile. But actually I'm not sure what the OP is trying to achieve exactly.
|
1

Your function

 void createTree(BiTree &T)

should be

void createTree(BiTree *T)

or

void createTree(BiTree T)

You can't pass by reference with that C++ syntax in plain C.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.