tree

  • Thread starter Bill Cunningham
  • Start date
B

Bill Cunningham

I have worked with this tree function some more and I have inserted
malloc. I don't know if I'm using it right as the compiler says.

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

struct node {
int data;
struct node *right, *left;
};

static int lookup(struct node *node, int target)
{
if (node == NULL)
return 1;
else {
if (target == node->data)
return printf("data found");
else {
if (target < node->data)
return (lookup(node->left, target));
else {
return (lookup(node->right, target));
}
}
}
}

struct node *NewNode(int data)
{
struct node *node = malloc(sizeof struct node *node);
node->data = data;
node->right = NULL;
node->left = NULL;
return (node);
}

b.c: In function `NewNode':
b.c:28: parse error before "struct"

Bill
 
B

Ben Bacarisse

Bill Cunningham said:
I have worked with this tree function some more and I have inserted
malloc. I don't know if I'm using it right as the compiler says.

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

struct node {
int data;
struct node *right, *left;
};

static int lookup(struct node *node, int target)
{
if (node == NULL)
return 1;
else {
if (target == node->data)
return printf("data found");
else {
if (target < node->data)
return (lookup(node->left, target));
else {
return (lookup(node->right, target));
}
}
}
}

This function returns either 1 or 10 (and in rare cases EOF). That's
an odd thing to do.
struct node *NewNode(int data)
{
struct node *node = malloc(sizeof struct node *node);

There are two forms of sizeof and this is neither. You need either an
expression or something called a "type name" in ()s. I.e. one of:

struct node *node = malloc(sizeof *node);

or

struct node *node = malloc(sizeof (struct node));
node->data = data;

And then you should test the result before using it:

if (node) {
node->data = data;
/* etc */
}
node->right = NULL;
node->left = NULL;
return (node);
}

It does not help that you are using the same name ("node") for a
struct and for several pointer to one of these structs.
 
B

Bill Cunningham

struct node *node = malloc(sizeof *node);

or

struct node *node = malloc(sizeof (struct node));

The source on the page says new and I know that's not right. So I used
malloc. In the source struct node is passed to malloc but there is no inner
parenthesis.

Bill
 
B

Ben Bacarisse

Bill Cunningham said:
The source on the page says new and I know that's not right. So I used
malloc. In the source struct node is passed to malloc but there is no inner
parenthesis.

You say the page uses new rather than malloc. They have different
syntax so you can't change new into malloc and expect it to work.

If you are looking at some page where "struct node is passed to
malloc" (with or without parentheses) then the page is simply wrong
and you should not copy code from it.
 
F

Frank

In Dread Ink, the Grave Hand of Bill Cunningham Did Inscribe:
The source on the page says new and I know that's not right. So I used
malloc. In the source struct node is passed to malloc but there is no inner
parenthesis.

Bill

Well, Bill, it all looks like the same gobbledygook to me, but the point is
that that's not the case with Ben. Adapt his. Copy the darn thing if
adaptation is beyond your game.
--
Frank

Most of us here in the media are what I call infotainers...Rush Limbaugh is
what I call a disinfotainer. He entertains by spreading disinformation.
~~ Al Franken
 
N

Nick Keighley

    I have worked with this tree function some more and I have inserted
malloc. I don't know if I'm using it right as the compiler says.

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

struct node {
    int data;
    struct node *right, *left;

};

static int lookup(struct node *node, int target)
{
    if (node == NULL)
        return 1;
    else {
        if (target == node->data)
            return printf("data found");
        else {
            if (target < node->data)
                return (lookup(node->left, target));
            else {
                return (lookup(node->right, target));
            }
        }
    }

}

struct node *NewNode(int data)
{
    struct node *node = malloc(sizeof struct node *node);
    node->data = data;
    node->right = NULL;
    node->left = NULL;
    return (node);

}

b.c: In function `NewNode':
b.c:28: parse error before "struct"

lookup sizeof in K&R
 
F

Fred

You say the page uses new rather than malloc.  They have different
syntax so you can't change new into malloc and expect it to work.

If you are looking at some page where "struct node is passed to
malloc" (with or without parentheses) then the page is simply wrong
and you should not copy code from it.

If the page says "new" rather than "malloc", then the code being
quoted is likely to be in C++, not C.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

error in binary tree 13
tree 9
Infinite loop problem 1
BST insertion 1
a simple struct code failing 12
nodes 4
Queue in C 25
simple BST implementation 18

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,066
Latest member
VytoKetoReviews

Latest Threads

Top