main.c

B

Bill Cunningham

I have written this in main and it compiles and gives a segmentation
fault. With all the previous code what could be wrong here?

#include "node.h"

int main() {
struct node *a;
int i=maxDepth(a);
printf("%i\n",i);
return 0;}

stdio.h and stdlib.h and the struct node are defined in "node.h"

Bill
 
U

user923005

    I have written this in main and it compiles and gives a segmentation
fault. With all the previous code what could be wrong here?

#include "node.h"

int main() {
 struct node *a;
 int i=maxDepth(a);
 printf("%i\n",i);
 return 0;}

    stdio.h and stdlib.h and the struct node are defined in "node.h"

Bill

This is doing something bad:

int i=maxDepth(a);
 
B

Bill Cunningham

This is doing something bad:

int i=maxDepth(a);

What is wrong? int maxDepth(struct node*) is the prototype of the
function.

Bill
 
K

Keith Thompson

Bill Cunningham said:
I have written this in main and it compiles and gives a segmentation
fault. With all the previous code what could be wrong here?

#include "node.h"

int main() {
struct node *a;

What is the value of a at this point?
int i=maxDepth(a);

What value are you passing to maxDepth?
 
B

Bill Cunningham

What is the value of a at this point?


What value are you passing to maxDepth?

Ok I think I see. struct node *a==NULL; would that be correct? Or root
would be correct. I'm not sure which.

Bill
 
B

Bill Cunningham

Keith Thompson said:
What is the value of a at this point?


What value are you passing to maxDepth?
I don't know anything about structs. I have never really used them
before. Especially with pointers. Hince I'm trying to learn.

Bill
 
K

Keith Thompson

Bill Cunningham said:
Ok I think I see. struct node *a==NULL; would that be correct? Or root
would be correct. I'm not sure which.

I'm not sure either. I think maxDepth was posted in another thread,
but I'm not going to take the time to go looking for it.

But is calling maxDepth(NULL) a sensible thing to do?
 
B

Bill Cunningham

Keith Thompson said:
But is calling maxDepth(NULL) a sensible thing to do?
I don't know I think maybe calling root is. If I can't fix this I'll
post the header and both master source files so no one will have to go back
and look at anything.

Bill
 
B

Bill Cunningham

I don't know I think maybe calling root is. If I can't fix this I'll
post the header and both master source files so no one will have to go
back and look at anything.
-----
build.c
#include "node.h"

int find(struct node *node, int target)
{
if (node == NULL) {
return (1);
} else {
if (target == node->count) {
return (0);
} else {
if (target < node->count) {
return (find(node->right, target)
);
} else
return (find(node->left, target)
);
}
}
}

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

struct node *insert(struct node *node, int count)
{
if (node == NULL) {
return (newNode(count));
} else {
if (count <= node->count)
node->left = insert(node->left, count);
else
node->right = insert(node->right, count);
return (node);
}
}

build2.c

#include "node.h"

struct node *build(void)
{
struct node *root = NULL;
root = (struct node *) insert(root, 2);
root = (struct node *) insert(root, 1);
root = (struct node *) insert(root, 3);
return (root);
};

int maxDepth(struct node *node)
{
if (node == NULL) {
return 0;
}

else {
int ldepth = maxDepth(node->left);
int rdepth = maxDepth(node->right);
if (ldepth > rdepth)
return (ldepth + 1);

else
return (rdepth + 1);
}
}

main.c

#include "node.h"

int main()
{
struct node *root;
int i = maxDepth(root);
printf("%i\n", i);
return 0;
}

I'll keep working on this anyway.

Bill
 
L

luserXtrog

-----
build.c
#include "node.h"

int find(struct node *node, int target)
{
    if (node == NULL) {
        return (1);
    } else {
        if (target == node->count) {

Why did you name this member variable "count".
Wouldn't 'data' be more appropriate?
Or 'value'?
            return (0);
        } else {
            if (target < node->count) {
                return (find(node->right, target)
                    );
            } else
                return (find(node->left, target)
                    );
        }
    }

}

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

}

struct node *insert(struct node *node, int count)
{
    if (node == NULL) {
        return (newNode(count));
    } else {
        if (count <= node->count)
            node->left = insert(node->left, count);
        else
            node->right = insert(node->right, count);
        return (node);
    }

}

build2.c

#include "node.h"

struct node *build(void)
{
    struct node *root = NULL;
    root = (struct node *) insert(root, 2);
    root = (struct node *) insert(root, 1);
    root = (struct node *) insert(root, 3);
    return (root);

};

int maxDepth(struct node *node)
{
    if (node == NULL) {
        return 0;
    }

    else {
        int ldepth = maxDepth(node->left);
        int rdepth = maxDepth(node->right);
        if (ldepth > rdepth)
            return (ldepth + 1);

        else
            return (rdepth + 1);
    }

}

main.c

#include "node.h"

int main()
{
    struct node *root;

That's a really nice variable name. So was the previous one.
But what's it mean? What's in there? I don't know. You don't
know. The compiler doesn't know.

You really, really need to put something there before you
pass it to a function. It's like mailing empty envelopes
and having the mailroom pick any old piece of paper off the
floor to serve as your mom's birthday card.

You may need to use an assignment operator (or an initializer,
but they look and work exactly the same).
    int i = maxDepth(root);

Now assuming you've done that and set root to contain an
address or NULL, then maxDepth will do its little thing
and return an integer which will then be printed.
A very interesting number. ;{>
    printf("%i\n", i);
    return 0;

}

I'll keep working on this anyway.

Keep up the work.
 
B

Bill Cunningham

-----
build.c
#include "node.h"

int find(struct node *node, int target)
{
if (node == NULL) {
return (1);
} else {
if (target == node->count) {

Why did you name this member variable "count".
Wouldn't 'data' be more appropriate?
Or 'value'?

I don't know.
return (0);
} else {
if (target < node->count) {
return (find(node->right, target)
);
} else
return (find(node->left, target)
);
}
}

}

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

}

struct node *insert(struct node *node, int count)
{
if (node == NULL) {
return (newNode(count));
} else {
if (count <= node->count)
node->left = insert(node->left, count);
else
node->right = insert(node->right, count);
return (node);
}

}

build2.c

#include "node.h"

struct node *build(void)
{
struct node *root = NULL;
root = (struct node *) insert(root, 2);
root = (struct node *) insert(root, 1);
root = (struct node *) insert(root, 3);
return (root);

};

int maxDepth(struct node *node)
{
if (node == NULL) {
return 0;
}

else {
int ldepth = maxDepth(node->left);
int rdepth = maxDepth(node->right);
if (ldepth > rdepth)
return (ldepth + 1);

else
return (rdepth + 1);
}

}

main.c

#include "node.h"

int main()
{
struct node *root;

That's a really nice variable name. So was the previous one.
But what's it mean? What's in there? I don't know. You don't
know. The compiler doesn't know.

Do I use one of these functions to put something in? This was copied off
a site for the most part. It was a c/c++ site.

You really, really need to put something there before you
pass it to a function. It's like mailing empty envelopes
and having the mailroom pick any old piece of paper off the
floor to serve as your mom's birthday card.

You may need to use an assignment operator (or an initializer,
but they look and work exactly the same).
int i = maxDepth(root);

Now assuming you've done that and set root to contain an
address or NULL, then maxDepth will do its little thing
and return an integer which will then be printed.
A very interesting number. ;{>
printf("%i\n", i);
return 0;

}

I'll keep working on this anyway.

Keep up the work.
 
N

Nick Keighley

But is calling maxDepth(NULL) a sensible thing to do?

    I don't know I think maybe calling root is. If I can't fix this I'll
post the header and both master source files so no one will have to go back
and look at anything.

what the <expletive> is "root". The variable "a" is undefined you dip
stick
 
D

Donkey Hottie

Bill Cunningham said:
I don't know anything about structs. I have never
really used them before. Especially with pointers. Hince
I'm trying to learn.
Bill

struct node *a = newNode(0);
int i=maxDepth(a);

That might survive.
 

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

No members online now.

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,282
Latest member
RoseannaBa

Latest Threads

Top