tree errors

B

Ben Bacarisse

osmium said:
"Bill Cunningham" wrote:
Most news readers have a method of reading old posts so YOU can see who
helped you. If your newsreader can't do that, Google does and is available
to anyone in the world with an internet connection.

It was originally from another group which slightly complicates
finding it. Bill would find the ultimate parent article to be his own
re-posting of the code here. I posted the example in response to a
request in comp.programming.

<snip>
 
B

Ben Bacarisse

Bill Cunningham said:
Ben Bacarisse said:
[Aside: I have used the technique where a tree pointer must always be
"valid" so that every recursive type has its own empty pointer:

struct tree { struct tree *left, *right; int num; };

static struct tree empty_tree = { &empty_tree, &empty_tree };
struct tree *tree_null = &empty_tree;

but I though that would over-complicate things here.]

Ben were you the one that gave me the original code I've been working
with.

Yes. You asked in comp.programming and then posted again here (that's
fine, by the way, I'm just explaining the situation).
When I run into trouble is with main. I get much from the tree and
tree functions set up but your main confuses me.

I can see it might. It was very terse.

int main(int argc, char *argv[])
{
struct tree *tp = NULL;
while (--argc > 0) {
int v = atoi(*++argv);
if (strchr(*argv, '?'))
printf("%d%s found\n", v, tree_has(tp, v) ? "" : " not");
else {
tp = tree_insert(tp, atoi(*argv));
printf("T = ");
tree_print(tp);
putchar('\n');
}
}
return 0;
}

This compiles no problem and you countdowns of argc I've never seen before.
The use of a poiunter in atoi knocks me off a bit. This code reaks of
experience.

Kind of you to say so but to me, if it reeks of anything, it is the
classroom. Too many years getting short examples that fit on one OHP
slide. Terse, dense examples are good to talk over but a proper
programmer would never be so compact in real code.

The idea was to provide a simple way to test "tree_insert" and
"tree_has" from the same program so I test if the argument has a '?'
in it. If so (strchr returns non-NULL) I print a string that depends
on the result of tree_has. If not, the numerical value of the argument
is added to the tree and the new tree is printed. I notice that I
should have used v again:

tp = tree_insert(tp, v);

rather than convert the string twice.

The pattern:

while (--argc > 0) {
++argv;
/* use *argv now, or maybe use or store *++argv */
}

is an old one. I was not sure where I saw it first but I see (having
just had a look) that it is in K&R1 so I'd lay odds on it being from
there.

int v = atoi(*++argv);

is horribly terse but the loop pattern is so well know that I bet most
readers don't miss a beat. ++argv yields a pointer to the next
argument (argv includes the program name which we don't want so
pre-increment is the right thing to do here). * of that is that the
argument itself (a pointer to the start of a string) and atoi of that
is a reasonable way to get a int from a string in test code like
this.

I spent years drawing diagrams like this one to get the hang of
pointers and pointers to pointers. This one shows the situation at
the first call to atoi after running the command "./t 3 1 2 2?"

+-----+ +-----+-----+-----+-----+
argv | --|--------------------->| '.' | '/' | 't' | 0 |
+-----+ +-----+ +-----+-----+ +-----+-----+-----+-----+
| --|----->| x--|----->| '3' | 0 |
+-----+ +-----+ +-----+-----+ +-----+-----+
| --|--------------------->| '1' | 0 |
+-----+ +-----+ +-----+-----+ +-----+-----+
| 4 | | --|----->| '2' | 0 |
+-----+ +-----+ +-----+-----+ +-----+-----+-----+
argc | --|--------------------->| '2' | '?' | 0 |
+-----+ +-----+-----+-----+
| 0 |
+-----+

argc has been decremented and argv incremented once. The box with the
x in it is *argv and the one with the '3' in it is **argv. The tall
column of pointers is the anonymous argument array to whose first
element (thanks, Keith!) argv initially points.

[If you don't use a fixed-width font, the above will just look like a
mess.]
 
B

Bill Cunningham

I will repost this code and ask this question. I want to add two members
to the tree struct. char *color and char *name for my cat tree. It adds a
little to the code you posted that has two members in the struct.

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

struct tree
{
int val;
struct tree *left, *right;
};

struct tree *tree_insert(struct tree *tp, int v)
{
if (tp) {
if (tp->val > v)
tp->left = tree_insert(tp->left, v);
else if (tp->val < v)
tp->right = tree_insert(tp->right, v);
}
else if (tp = malloc(sizeof *tp)) {
tp->val = v;
tp->left = tp->right = NULL;
}
return tp;
}

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

void tree_print(struct tree *tp)
{
if (tp) {
putchar('(');
tree_print(tp->left);
printf("%d", tp->val);
tree_print(tp->right);
putchar(')');
}
}

int main(int argc, char *argv[])
{
struct tree *tp = NULL;
while (--argc > 0) {
int v = atoi(*++argv);
if (strchr(*argv, '?'))
printf("%d%s found\n", v, tree_has(tp, v) ? "" : " not");
else {
tp = tree_insert(tp, atoi(*argv));
printf("T = ");
tree_print(tp);
putchar('\n');
}
}
return 0;
}

Now how would the insert, delete and print functions look in an example like
this? I'm getting the struct and the meaning of some of the functions my
real sticking points here is the call to malloc() and when and where in a
tree it's needed. Also that insert is kind of complicated. If I can get that
down I think I might be good to go.

Bill
 
O

osmium

"Bill Cunningham"
I will repost this code and ask this question. I want to add two
members to the tree struct. char *color and char *name for my cat tree. It
adds a little to the code you posted that has two members in the struct.

Not a complete answer, please, people, chime in.
Bill, please note you were warned that the post was overly concise, suitable
for an overhead projector. I would hope that projector was at MIT or some
similar place. I know i wouldn't want to go to school there.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct tree
{
int val;
struct tree *left, *right;
};

/* add v at the appropriate place in the tree. Do this by calling
recursively (if necessary) until that place is found.*/
struct tree *tree_insert(struct tree *tp, int v)
{
if (tp)
// tp points at a "nice" node, that is, it has at least one
child
{
if (tp->val > v)
tp->left = tree_insert(tp->left, v);
else if (tp->val < v)
tp->right = tree_insert(tp->right, v);
}

// tp points at the proper place to insert a new node

The first call of insert made during the program goes here because of the
way tp was initialized
else if (tp = malloc(sizeof *tp))

sizeof is an *operator* but it doesn't look like one!
malloc wants an integer of some kind to indicate the amount of space
needed. the parameter says: "size of the thing pointed at by tp" After the
statement, tp points at a new, empty, node.
{
tp->val = v;

after perhaps several recursive calls we finally found where v belongs.
Put it here
tp->left = tp->right = NULL;

if v was already in the tree, do nothing. simply return tp without a
recursive call, thus indicating sucess.
return tp;
}

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

void tree_print(struct tree *tp)
{
if (tp) {
putchar('(');
tree_print(tp->left);
printf("%d", tp->val);
tree_print(tp->right);
putchar(')');
}
}

int main(int argc, char *argv[])
{

note that this tree has no name
 
B

Ben Bacarisse

Bill Cunningham said:
I will repost this code and ask this question. I want to add two members
to the tree struct. char *color and char *name for my cat tree. It adds a
little to the code you posted that has two members in the struct.

If you want to add members, then just add them. If you add a lot of
members then insert will end up with a lot of parameters and most
people would then alter the design a little, but adding to is just
about OK.

See later, though for why this is not going to work out for you.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct tree
{
int val;
struct tree *left, *right;

just add them here:

char *color, *name;
};

struct tree *tree_insert(struct tree *tp, int v)

and add parameters to tree_insert so you can pass in the color and the
name.
{
if (tp) {
if (tp->val > v)
tp->left = tree_insert(tp->left, v);
else if (tp->val < v)
tp->right = tree_insert(tp->right, v);
}
else if (tp = malloc(sizeof *tp)) {
tp->val = v;

And here you would add code to set tp->color and tp->name.

This is where the "key" is set. This tree indexes by number, so you
can't use this tree to find a cat by name. That would be quite a
different program.
tp->left = tp->right = NULL;
}
return tp;
}

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

void tree_print(struct tree *tp)
{
if (tp) {
putchar('(');
tree_print(tp->left);
printf("%d", tp->val);

You could print the other fields here, though printing a whole tree
with all these brackets is useful only to see what is going on.
tree_print(tp->right);
putchar(')');
}
}

int main(int argc, char *argv[])
{
struct tree *tp = NULL;
while (--argc > 0) {
int v = atoi(*++argv);
if (strchr(*argv, '?'))
printf("%d%s found\n", v, tree_has(tp, v) ? "" : " not");
else {
tp = tree_insert(tp, atoi(*argv));
printf("T = ");
tree_print(tp);
putchar('\n');
}
}
return 0;

You'd need a whole new main to do whatever you need to do but you
don't say what that is so I can't even hazard a guess.
}

Now how would the insert, delete and print functions look in an
example like this? I'm getting the struct and the meaning of some of
the functions my real sticking points here is the call to malloc()
and when and where in a tree it's needed.

I think you also don't know what sort of tree this is. You asked for
some tree code I posted some, but now you want to change the code to
do other things but (a) you don't know what this program does, and (b)
you don't say what you want the new program to do. You can add as
many members to the struct as you like, but what are they for?

So, to sort out (a): this program shows a simple binary search tree of
integers. Such things behave a little like sets: you can add number
to a set and efficiently answer the question "is X in the set"? That
is all it can do, but I posted it because it answered your request for
"some tree code".

Changing it to do something with names and colours will be much more
complicated than adding fields, and no one can help at all unless you
say what you want to do with the names and the colours.
Also that insert is kind
of complicated. If I can get that down I think I might be good to
go.

You haven't seen delete yet!
 
B

Ben Bacarisse

osmium said:
"Bill Cunningham"

Not a complete answer, please, people, chime in.
Bill, please note you were warned that the post was overly concise, suitable
for an overhead projector. I would hope that projector was at MIT or some
similar place. I know i wouldn't want to go to school there.

I don't know how to take that!
/* add v at the appropriate place in the tree. Do this by calling
recursively (if necessary) until that place is found.*/

If I were commenting the function, I'd want to say also:

/* If the number, v, is already in the tree, do nothing.
Return a pointer to the (possibly bigger) tree. */
// tp points at a "nice" node, that is, it has at least one
child

tp might point to a node with no children. Since it points to a node
we know that is has a "val" member with some value stored there but
that is all.

Students often want to write:

if (tp->val == v)
/* We've found v in the tree. Now what? Oh, do nothing.*/;
else if (tp->val > v)
/* v belongs in the left sub-tree. */
tp->left = tree_insert(tp->left, v);
else if (tp->val < v)
/* v belongs in the right sub-tree. */
tp->right = tree_insert(tp->right, v);

because it flags up the important case first. Most "experienced"
programmers don't like having pointless if branches so they end up
with this simpler patter I posted, but there is something to be said
for the pointless "if".
// tp points at the proper place to insert a new node

The first call of insert made during the program goes here because of the
way tp was initialized


sizeof is an *operator* but it doesn't look like one!
malloc wants an integer of some kind to indicate the amount of space
needed. the parameter says: "size of the thing pointed at by tp" After the
statement, tp points at a new, empty, node.

.... or tp has been set to a null pointer because the malloc failed.
after perhaps several recursive calls we finally found where v belongs.
Put it here



if v was already in the tree, do nothing. simply return tp without a
recursive call, thus indicating sucess.

.... or failure if the malloc failed. Unfortunately this function is
so primitive that the failure can't be reported back to the top-level
caller. This function could be said to take the "safe but unhelpful"
option on failure. You don't get UB or any such disaster, but the
function does not report anything useful other than a failure to
allocate the root node.

<snip>
 
B

Bill Cunningham

Ben Bacarisse said:
Bill Cunningham said:
I will repost this code and ask this question. I want to add two
members
to the tree struct. char *color and char *name for my cat tree. It adds a
little to the code you posted that has two members in the struct.

If you want to add members, then just add them. If you add a lot of
members then insert will end up with a lot of parameters and most
people would then alter the design a little, but adding to is just
about OK.

See later, though for why this is not going to work out for you.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct tree
{
int val;
struct tree *left, *right;

just add them here:

char *color, *name;
};

struct tree *tree_insert(struct tree *tp, int v)

and add parameters to tree_insert so you can pass in the color and the
name.
{
if (tp) {
if (tp->val > v)
tp->left = tree_insert(tp->left, v);
else if (tp->val < v)
tp->right = tree_insert(tp->right, v);
}
else if (tp = malloc(sizeof *tp)) {
tp->val = v;

And here you would add code to set tp->color and tp->name.

This is where the "key" is set. This tree indexes by number, so you
can't use this tree to find a cat by name. That would be quite a
different program.
tp->left = tp->right = NULL;
}
return tp;
}

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

void tree_print(struct tree *tp)
{
if (tp) {
putchar('(');
tree_print(tp->left);
printf("%d", tp->val);

You could print the other fields here, though printing a whole tree
with all these brackets is useful only to see what is going on.
tree_print(tp->right);
putchar(')');
}
}

int main(int argc, char *argv[])
{
struct tree *tp = NULL;
while (--argc > 0) {
int v = atoi(*++argv);
if (strchr(*argv, '?'))
printf("%d%s found\n", v, tree_has(tp, v) ? "" : " not");
else {
tp = tree_insert(tp, atoi(*argv));
printf("T = ");
tree_print(tp);
putchar('\n');
}
}
return 0;

You'd need a whole new main to do whatever you need to do but you
don't say what that is so I can't even hazard a guess.
}

Now how would the insert, delete and print functions look in an
example like this? I'm getting the struct and the meaning of some of
the functions my real sticking points here is the call to malloc()
and when and where in a tree it's needed.

I think you also don't know what sort of tree this is. You asked for
some tree code I posted some, but now you want to change the code to
do other things but (a) you don't know what this program does, and (b)
you don't say what you want the new program to do. You can add as
many members to the struct as you like, but what are they for?

So, to sort out (a): this program shows a simple binary search tree of
integers. Such things behave a little like sets: you can add number
to a set and efficiently answer the question "is X in the set"? That
is all it can do, but I posted it because it answered your request for
"some tree code".

Changing it to do something with names and colours will be much more
complicated than adding fields, and no one can help at all unless you
say what you want to do with the names and the colours.
Also that insert is kind
of complicated. If I can get that down I think I might be good to
go.

You haven't seen delete yet!

Well I just tried my latests attempt with this before I read your post.
I will post it anyway the code compiles but I get a segmentation fault. I
based it on your code. I think a linked list or binary search tree is what
I'm looking for. Some kind of database algorithm.

#include <stdio.h>

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

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

struct tree *prn(struct tree *tp, char *n, char *c, int nu) {
printf("%s %s %s %i\n", tp, n, c, nu);
return 0;
}
return 0;
}
int main(void) {
struct tree p;
struct tree *pt=&p;
pt=insert(pt,"striper","mau",1);
printf(*pt);} /* tried printf(pt); also */

Now after having read your post I can see that I am not referring to the
members color or name at all. Maybe that's what is causing the seg fault.

Bill
 
O

osmium

:
I don't know how to take that!

About the only thing I got out of the post is, "Boy this guy is really
sharp". I was also extremely impressed by the turnaround time between Bill's
post and your answer!! But exactly how does that help me? I like more
words. I think if newbies were to write a comment as to exactly what the
next function does, the world would be a better place.
If I were commenting the function, I'd want to say also:

/* If the number, v, is already in the tree, do nothing.
Return a pointer to the (possibly bigger) tree. */

Yes, that's better.
tp might point to a node with no children. Since it points to a node
we know that is has a "val" member with some value stored there but
that is all.

Yes, I goofed big time there.
Students often want to write:

if (tp->val == v)
/* We've found v in the tree. Now what? Oh, do nothing.*/;
else if (tp->val > v)
/* v belongs in the left sub-tree. */
tp->left = tree_insert(tp->left, v);
else if (tp->val < v)
/* v belongs in the right sub-tree. */
tp->right = tree_insert(tp->right, v);

because it flags up the important case first. Most "experienced"
programmers don't like having pointless if branches so they end up
with this simpler patter I posted, but there is something to be said
for the pointless "if".


... or tp has been set to a null pointer because the malloc failed.

I made a conscious choice to avoid that digression about malloc failing.
Bill gets overly fascinated with digressions.
 
B

Ben Bacarisse

<snip>
By the way, you did not comment on any of the stuff you quoted. It
would have been better to delete it all.
Well I just tried my latests attempt with this before I read your post.
I will post it anyway the code compiles but I get a segmentation fault. I
based it on your code. I think a linked list or binary search tree is what
I'm looking for. Some kind of database algorithm.

Those three -- a BST, and LL and a database algorithm are all very
different. I think you need to say what you want your program to do.
You can't do anything without an objective in mind.
#include <stdio.h>

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

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

struct tree *prn(struct tree *tp, char *n, char *c, int nu) {
printf("%s %s %s %i\n", tp, n, c, nu);
return 0;
}
return 0;
}
int main(void) {
struct tree p;
struct tree *pt=&p;
pt=insert(pt,"striper","mau",1);
printf(*pt);} /* tried printf(pt); also */

This is not C. It is a C-like language in which a function can be
defined inside another one. Also the changes you've made to insert
means that it no longer works. Do you know what your function does
and how that is different to what my function does? If you can't, how
will you make any progress in trying to write this program?
Now after having read your post I can see that I am not referring to the
members color or name at all. Maybe that's what is causing the seg
fault.

No. That has no effect of the seg fault.

I'll start you off. Your variable p is unset. p.num, p.left and
p.right are all indeterminate. You pass a pointer to this object to
inset which asks if the num in this struct is > nu. You can not say
is that test is true or not. You have a program whose execution is
not under your control.
 
M

Mark L Pappin

Every now and then I peek at articles my scorefile has eaten, just to
check that the score is appropriate.

Bill Cunningham said:
"Ben Bacarisse" <[email protected]> wrote:
[snipped; attribution left for context]
Well I just tried my latests attempt with this before I read your
post. I will post it anyway the code compiles

Not with a C compiler it doesn't.
struct tree *insert(struct tree *tp, char *n, char *c, int nu)
{
if (tp) {
if (tp->num > nu)
tp->left = insert(tp->left, n, c, nu);
if (tp->num < nu)
tp->right = insert(tp->right, n, c, nu);
else if (tp = malloc(sizeof *tp)) {
tp->num = nu;
tp->left = tp->right = NULL;
}
return tp;
}

Note: the '}' on the line above terminates the block beginning with
'if (tp) {', so you're still inside the definition of 'insert()' here.
struct tree *prn(struct tree *tp, char *n, char *c, int nu) {

This is the beginning of a function definition, which can't appear
nested inside another function definition in C. Other languages
(including, I believe, GNU C) allow nested function definitions but C
does not. Move this function definition outside the body of
'insert()'.


Pretending that you have done the above, I'll address just 2 other
issues with the code:
printf("%s %s %s %i\n", tp, n, c, nu);

1. Passing not-a-string (such as tp) when printf() expects a string
gives UB. Nothing good can come of this. Change this line to

printf("%p %s %s %i\n", (void*)tp, n, c, nu);

to eliminate this UB.
return 0;
}
return 0;
}
int main(void) {
struct tree p;
struct tree *pt=&p;
pt=insert(pt,"striper","mau",1);
printf(*pt);} /* tried printf(pt); also */

2. What did you expect printf() to do with *pt, or pt? Why did you
expect that?

(These are serious questions. If you can not answer both of them,
then you're not programming but being a monkey at a typewriter. To
write a program, you should start with what you want it to do, and
using the language specification you produce code that should do just
that.)

Now after having read your post I can see that I am not referring to
the members color or name at all. Maybe that's what is causing the
seg fault.

You can't get a seg fault from something that doesn't appear in your
code. The seg fault is from what you DO have, which is broken.

mlp
 
B

Barry Schwarz

On Sun, 30 Aug 2009 19:29:10 -0400, "Bill Cunningham"

snip
Well I just tried my latests attempt with this before I read your post.
I will post it anyway the code compiles but I get a segmentation fault. I

The code does not compile, or at least not without errors. You have
nested function definitions, an incomplete main, no prototype for
malloc, an invalid first argument to printf, etc. Why are you wasting
your time executing code that does not compile? Why are you wasting
our time either lying about it or posting code different than what you
execute?
 
K

Keith Thompson

Bill Cunningham said:
Well I just tried my latests attempt with this before I read your post.
I will post it anyway the code compiles but I get a segmentation fault. I
based it on your code. I think a linked list or binary search tree is what
I'm looking for. Some kind of database algorithm.
[...]

If you haven't decided yet whether you want a linked list, a binary
search tree, or "some kind of database algorithm" (!), you're not
ready to start writing code.

Just what are you trying to accomplish?
 
B

Bill Cunningham

Mark L Pappin said:
Every now and then I peek at articles my scorefile has eaten, just to
check that the score is appropriate.

Bill Cunningham said:
"Ben Bacarisse" <[email protected]> wrote:
[snipped; attribution left for context]
Well I just tried my latests attempt with this before I read your
post. I will post it anyway the code compiles

Not with a C compiler it doesn't.
struct tree *insert(struct tree *tp, char *n, char *c, int nu)
{
if (tp) {
if (tp->num > nu)
tp->left = insert(tp->left, n, c, nu);
if (tp->num < nu)
tp->right = insert(tp->right, n, c, nu);
else if (tp = malloc(sizeof *tp)) {
tp->num = nu;
tp->left = tp->right = NULL;
}
return tp;
}

Note: the '}' on the line above terminates the block beginning with
'if (tp) {', so you're still inside the definition of 'insert()' here.
struct tree *prn(struct tree *tp, char *n, char *c, int nu) {

This is the beginning of a function definition, which can't appear
nested inside another function definition in C. Other languages
(including, I believe, GNU C) allow nested function definitions but C
does not. Move this function definition outside the body of
'insert()'.

OK I see then. No definitions in function bodies.
Pretending that you have done the above, I'll address just 2 other
issues with the code:


1. Passing not-a-string (such as tp) when printf() expects a string
gives UB. Nothing good can come of this. Change this line to

printf("%p %s %s %i\n", (void*)tp, n, c, nu);

to eliminate this UB.

I don't know what UB is?.
2. What did you expect printf() to do with *pt, or pt? Why did you
expect that?

I wanted the value from insert printed and if pt didn't work then maybe a
dereference would. So I basically got excited and desperate and tried all I
knew to. And I hadn't even mentioned the new types I put in my struct, the
two char *'s.

Bill
 
B

Bill Cunningham

Barry Schwarz said:
On Sun, 30 Aug 2009 19:29:10 -0400, "Bill Cunningham"

snip


The code does not compile, or at least not without errors. You have
nested function definitions, an incomplete main, no prototype for
malloc, an invalid first argument to printf, etc. Why are you wasting
your time executing code that does not compile? Why are you wasting
our time either lying about it or posting code different than what you
execute?
This was posted before I read Ben's new post. I can't look at code, for
the most part, and make the judgments Barry you've made. Structs I am trying
to learn and don't know how to deal with them.

Bill
 
S

Squeamizh

This is a confusing way of posting.  This makes it look as if Richard  
Heathfield
wrote the /* */ comment above.  Put your own text on a different line from
what you're quoting.

The above line of code declares instance as a variable of type (struct  
tree),
and initializes it to contain all zeros.  It's equivalent to this:

    struct tree instance = {0, 0, 0};

which, since the 2nd and 3rd elements of the structure are pointers, might  
be
clearer as

    struct tree instance = {0, NULL, NULL};

If any members of a structure are explicitly initialized, any trailing  
members
without explicit initializers are set to 0 (as if 0 were assigned to them  
by
an assignment operator, so doubles will be set to 0.0, pointers will be set
to a null pointer, etc.  Structure members with non-integer types might end
up with values which are not all-zero-bits.

I'm sure Bill Cunningham will find the distinction between assigned-0
and all-bits-zero extremely useful.
 
B

Bill Cunningham

Just what are you trying to accomplish?
I want to learn about structs which I can understand. It's the algorithm
of the tree and recursion that I have problems with. I can write straight C
using members of a struct and also use pointers to it. As was pointed out
earlier you can't declare in a nested if statement. That's my basic lack of
knowledge of C. The syntax of the language overall gives me trouble even
though I can print members of structs via struct.member or struct->member.

Bill
 
B

Barry Schwarz

This was posted before I read Ben's new post. I can't look at code, for
the most part, and make the judgments Barry you've made. Structs I am trying
to learn and don't know how to deal with them.

Go back and read what I wrote again. Answer the questions I asked.

No one expects you to look at the code and see the errors that have
been mentioned. You claimed the code compiled. We do expect you to
read the diagnostics produced by your compiler and not confuse abysmal
failure with success. We also expect you to post the actual code, not
some retyped gibberish.

Where in my post did I mention structs? Your lack of knowledge about
structs is irrelevant until you clean up the errors in your code.
 
B

Barry Schwarz

I want to learn about structs which I can understand. It's the algorithm
of the tree and recursion that I have problems with. I can write straight C

You are having trouble with a lot more than tree algorithms.
using members of a struct and also use pointers to it. As was pointed out

Show us an example of your success with this since your previous post
claimed you didn't understand structs.
earlier you can't declare in a nested if statement. That's my basic lack of

You really do have a knack for misstating previous comments. If your
understanding of the previous comments was really this flawed you
would not be able to even post to Usenet.

No one ever used the word declare in relation to an if statement and
no one ever said that nested if statements were a problem. What you
have been told is that you cannot define one function within another,
no nested functions.
knowledge of C. The syntax of the language overall gives me trouble even
though I can print members of structs via struct.member or struct->member.

Show us one of your successful efforts. By now you should realize
that attention to detail is important and that struct->member is a
constraint violation. I hope you meant struct_pointer->member but
given other examples of your code (such as using a pointer to struct
and a struct where a pointer to char is required) I cannot be sure.
 
K

Keith Thompson

Barry Schwarz said:
Go back and read what I wrote again. Answer the questions I asked.

No one expects you to look at the code and see the errors that have
been mentioned. You claimed the code compiled. We do expect you to
read the diagnostics produced by your compiler and not confuse abysmal
failure with success. We also expect you to post the actual code, not
some retyped gibberish.

It's possible that his code actually did compile. gcc, by default,
quietly accepts C-like code with nested function definitions,
such as this:

int outer(void) {
int inner(void) {
}
}

What this means, Bill, is that if you're interested in learning to
write portable C, you must not invoke gcc without additional options.
Use "gcc -ansi -pedantic -Wall -Wextra" if you want to write C90
code, "gcc -std=c99 -pedantic -Wall -Wextra" if you want to write
C99 code (though of course gcc doesn't fully support C99). And you
need to do this in a way that doesn't let you forget to do it.

[snip]
 
H

Herbert Rosenau

Every now and then I peek at articles my scorefile has eaten, just to
check that the score is appropriate.
You should know that the OP has mental incapabilities! He knows of
that fact and has declared it as he was very new to this group. That
means that his memory likes to forget what he has learned, he learns -
but he learns slowly, really slowly and needs to repeat steps more
often than you may think of. We have already accepted his handicaps at
all.

Please be pationt with him. He does his best - even as this is not
much.

English is not my mothers language - and I'd no chance at scool to
learn it (learning in a pub from scottish peoples while drinking beer
is not the best choice - but the only chance to get the primitives
until I was too old to get something halfways right). Sometimes I'm
like Bill - I learn english and forgot something until I learn again.
Learning C was much easier - but only because I had some basics in
englich.

--
Tschau/Bye
Herbert

Visit http://www.ecomstation.de the home of german eComStation
eComStation 1.2R Deutsch ist da!
 

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,772
Messages
2,569,593
Members
45,112
Latest member
VinayKumar Nevatia
Top