tree errors

B

Bill Cunningham

I have written these functions and the source code compiles but my
problem is in main(). I get either segmentation fault or a dereferncing
incomplete type error. I basically don't know how to call these functions
from main. I've tried all I know to try and it's beyond me.

Thanks for help.

Bill

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

struct tree {
char *name;
int age;
int num;
char *color;
struct tree *right, *left;
};

struct tree *insert(struct tree *tp, int v, char *n, int a, char
*c)
{
if (tp) {
if (tp->num > v)
tp->left = insert(tp->left, v, n, a, c);
if (tp->num < v)
tp->right = insert(tp->right, v, n, a, c);
} else if (tp = malloc(sizeof *tp)) {
tp->num = v;
tp->right = tp->left = NULL;
}
return tp;
}

int has(struct tree *tp, int v)
{
if (tp) {
if (tp->num < v)
return has(tp->right, v);
if (tp->num > v)
return has(tp->left, v);
else
return 1;
}
return 0;
}

void print(struct tree *tp)
{
if (tp) {
print(tp->left);
printf("%i\n", tp->num);
print(tp->right);
}
}

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

int main(void) {
struct tree *p;
has(p,1);}
 
K

Keith Thompson

Bill Cunningham said:
I have written these functions and the source code compiles but my
problem is in main(). I get either segmentation fault or a dereferncing
incomplete type error. I basically don't know how to call these functions
from main. I've tried all I know to try and it's beyond me.

Thanks for help.

Bill

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

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

Repeating these #include directives shouldn't cause any problems, but
it's pointless.
int main(void) {
struct tree *p;

p is uninitialized, so its value is garbage.
has(p,1);}

You're passing a garbage value to has().
 
B

Bill Cunningham

Keith Thompson said:
p is uninitialized, so its value is garbage.


You're passing a garbage value to has().

#include <stdio.h>

main(void) {
struct tree *p=NULL;
insert(*p,1,"snow",9,"white");
printf("%i\n",has(p,1));
return 0;
}

Well this coding isn't getting me anywhere. I keep getting seg faults or
nothing.

Bill
 
B

Ben Bacarisse

Bill Cunningham said:
#include <stdio.h>

main(void) {
struct tree *p=NULL;
insert(*p,1,"snow",9,"white");

p = insert(p, 1, "snow", 9, "white");
printf("%i\n",has(p,1));
return 0;
}

Well this coding isn't getting me anywhere. I keep getting seg faults or
nothing.

You should have got a very serious warning from the compiler. *p is
not of the right type to pass as the first argument to insert.
 
B

Bill Cunningham

You should have got a very serious warning from the compiler. *p is
not of the right type to pass as the first argument to insert.

I'm getting something about dereferencing.

Bill
 
T

Tom St Denis

    I'm getting something about dereferencing.

Bill

Honest question. Do you have a learning disability we should know
about?

If the compiler is telling you you're passing a wrong type [and GCC
will tell you which argument is wrong] maybe that should serve as a
hint.

But more importantly, I would not be working with anything of
complexity until you better understand C syntax fundamentals. I mean
if you don't even know how to use pointers how do you expect to
manipulate lists?

Tom
 
K

Keith Thompson

Bill Cunningham said:
I'm getting something about dereferencing.

Bill, that's an extraordinarily useless statement.

If you want to ask us about a compiler error message, show us the
message. Don't try to summarize it, don't tell us what it's *like*,
show us what the actual message *is*.
 
B

Bill Cunningham

Richard Heathfield said:
Bill Cunningham said:


The cause of your original problem was that p did not point to an
existing struct tree object. That is, it was uninitialised. Yes,
giving it the value NULL at definition does count as initialising it,
but still doesn't point it anywhere useful.


An instance of the struct tree type.

struct tree *p;

p=insert(?,1,"fluffy",3,"calico");

? I don't know what to pass to the first parameter. But I want to return to
*p right?

I think I mastered structs and members a long time ago while using my cat
examples. But pointing to things in this manner, I obviously need work with.

Bill
 
N

Nick Keighley

    struct tree *p;

p=insert(?,1,"fluffy",3,"calico");

post the codefor insert so I don't have to go back and look for it

struct tree *p = NULL;
p = insert (p, 1, "fluffy", 3, "calico"); /* assuming I got the
other
parameters right */

? I don't know what to pass to the first parameter.

a pointer to a struct tree
But I want to return to *p right?

repost that in english
I think I mastered structs and members a long time ago while using my cat
examples. But pointing to things in this manner, I obviously need work with.

try something simpler like pointers to ints first
 
B

Ben Bacarisse

Bill Cunningham said:
struct tree *p;

p=insert(?,1,"fluffy",3,"calico");

? I don't know what to pass to the first parameter.

Did you see that I told you what goes there several posts ago (maybe
in another thread)? If you did not know *why* you could have asked,
but it looks like you did not see that I corrected the code.

You write:

struct tree *p = NULL;
p = insert(p, 1, "fluffy", 3, "calico");
But I want to return to *p right?

That does not mean anything to me so I can't comment.
I think I mastered structs and members a long time ago while using my cat
examples. But pointing to things in this manner, I obviously need
work with.

I think it would help if you worte the code to fill in a struct tree
object first without and then via a pointer:

struct tree my_cat;

/* code to set every member of my_cat */

struct tree *p = /* make p point to my_cat -- what goes here? */;

/* code to set every member of my_cat using only the pointer p */
 
B

Bill Cunningham

Bill, that's an extraordinarily useless statement.

If you want to ask us about a compiler error message, show us the
message. Don't try to summarize it, don't tell us what it's *like*,
show us what the actual message *is*.

I have to log off. Reboot into linux and login and try to remember the
compiler warnings. Or redirect stderr to a text file and copy to NTFS and
boot XP and login and post the errors. That's why I seem to forget and not
get the errors right. I compile with linux and network with XP.

Bill
 
B

Bill Cunningham

post the codefor insert so I don't have to go back and look for it

struct tree *p = NULL;
p = insert (p, 1, "fluffy", 3, "calico"); /* assuming I got the
other
parameters right */


prototype of insert,

struct tree *tp insert(struct tree *tp, int value, char *name,int age, char
*color);

I can easily work with tree.color or tree.name if I can remember right. I
might have to go back and look to members. But these -> and * throw me off.

Bill
 
B

Bill Cunningham

Bill Cunningham said:
struct tree *p;

p=insert(?,1,"fluffy",3,"calico");

Ok then I should use this?

struct tree *pt;
insert(pt,1,"cocoa",2.5,"tabby");

right?

Bill
 
B

Bill Cunningham

Bill Cunningham said:
Ok then I should use this?

struct tree *pt;
insert(pt,1,"cocoa",2.5,"tabby");

right?
Oops no. int age needs to be 2. Not 2.5.

Bill
 
K

Keith Thompson

Bill Cunningham said:
I have to log off. Reboot into linux and login and try to remember the
compiler warnings. Or redirect stderr to a text file and copy to NTFS and
boot XP and login and post the errors. That's why I seem to forget and not
get the errors right. I compile with linux and network with XP.

That doesn't make the statement any more useful. The fact that
it's difficult for you to copy-and-paste the error message doesn't
diminish the need to do so. (Incidentally, I think you'll find
that Linux is also capable of networking.)
 
O

osmium

Bill Cunningham said:
But these -> and * throw me off.

K&R conveniently put an index in their book, it is at the back of the book.
For your problem they suggest looking at pages 94 and 131. If you don't
understand the descriptions provided there, you probably need a wordier
book. Or a tutor. Or an instructor. Or a simpler problem.

There is an old saying in my country, "Learn to crawl before you learn to
walk".
 
B

Bill Cunningham

There is an old saying in my country, "Learn to crawl before you learn to
walk".

<sigh> I know but I'm pretty excited about C. There are so many things I
want to do with it. Write a parser, eventually compiler, databases. I don't
see where a parser would be difficult especially using strtok().

Bill
 
B

Bill Cunningham

[snip]

Here is my latest attempt and error message.

#include <stdio.h>

int main(void) {
struct tree *p,*tp;
tp=insert(p,2,"name",3,"color");
printf("%s\n",(struct tree)tp);
}
a.c: In function `main':
a.c:5: warning: assignment makes pointer from integer without a cast
a.c:6: conversion to non-scalar type requested

Bill
 

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

Members online

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top