4 Replies - 1358 Views - Last Post: 29 April 2009 - 05:54 AM Rate Topic: -----

#1 rymonroe  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 27-April 09

Core Dumped when strcmp called in Tree

Post icon  Posted 27 April 2009 - 08:43 PM

I am trying to sort my tree while I put it into the tree but I keep on getting the error "insert.c:9: warning: passing arg 2 of `strcmp' from incompatible pointer type
Segmentation Fault (core dumped)". Please tell me where I am going wrong. Thanks

int main() {
char *n,c;
tnode * t = NULL, *x;
		while (scanf("%s",n) != EOF) {t=insert(n,t);printf("%s ",n);}
		printf("\n");
		inorder(t);
		printf("\n");
		printf("count: %d\n",c = count(t));
return 0;
}

/*insert.c*/

#include "tree.h"

tnode * insert(char * x, tnode * t) {
if (t == NULL) return makenode(x);
if (strcmp(x,t->right)) {t->left = insert(x,t->left); return t;}
t->right = insert(x,t->right); return t;
}

/*tree.h*/

#ifndef TREE_H
#define TREE_H

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

typedef struct node {
		char * info;
		struct node * right, *left;
} tnode;

tnode * insert(char * target,tnode * t);
tnode * makenode(char * x);
tnode * tsearch(char * x,tnode * t);
void inorder(tnode * t);
int height(tnode * t);
int count(tnode * t);
#endif



This post has been edited by rymonroe: 27 April 2009 - 08:46 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Core Dumped when strcmp called in Tree

#2 no2pencil  Icon User is offline

  • Admiral Fancy Pants
  • member icon

Reputation: 5364
  • View blog
  • Posts: 27,325
  • Joined: 10-May 07

Re: Core Dumped when strcmp called in Tree

Posted 27 April 2009 - 08:46 PM

I don't think you should do a variable assignment within the printf statement.

printf("count: %d\n",c = count(t));


I would set the value for c & then print it, rather than cramming them all into one instruction.


c = count(t);
printf("count: %d\n",c);


Not only that, but you also define c as a char, & you send printf a %d argument, indicating digit.
Was This Post Helpful? 0
  • +
  • -

#3 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6079
  • View blog
  • Posts: 23,550
  • Joined: 23-August 08

Re: Core Dumped when strcmp called in Tree

Posted 28 April 2009 - 04:41 AM

t->right is not a string, it's a struct node *. You need to access the info member:
if (strcmp(x,t->right->info))


Also, in case you didn't realize it, strcmp returns 0 if the strings match.
Was This Post Helpful? 0
  • +
  • -

#4 rymonroe  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 27-April 09

Re: Core Dumped when strcmp called in Tree

Posted 28 April 2009 - 08:51 PM

Thanks alot for the help. I am still geting a segfault. When I take the strcmp(x,t->info)) out of the program I dont get a segfault. I don't understand. Any insight would be appreciated.

#include "tree.h"

int main() {
int c;
char *n, *a="hello";
tnode * t = NULL, *x; printf("hello");
		while (scanf("%s",&n) != EOF) {printf("%s ",&n);t=insert(n,t);}
		printf("\n");
		inorder(t);
		printf("\n");
		printf("count: %d\n",c = count(t));
		printf("\n");
return 0;
}
 /*insert.c*/
#include "tree.h"

tnode * insert(char* x, tnode * t) {
if (t == NULL) return makenode(x);
if (strcmp(x,t->info)<0) {t->left = insert(x,t->left); return t;}
t->right = insert(x,t->right); return t;
}
~
~
~
/*tree.h*/
#ifndef TREE_H
#define TREE_H
#include <string.h>
#include <stdio.h>


typedef struct node {
		char * info;
		struct node * right, *left;
} tnode;

tnode * insert(char * target,tnode * t);
tnode * makenode(char * x);
tnode * tsearch(char * x,tnode * t);
void inorder(tnode * t);
int height(tnode * t);
int count(tnode * t);
#endif
~
~
~


Was This Post Helpful? 0
  • +
  • -

#5 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6079
  • View blog
  • Posts: 23,550
  • Joined: 23-August 08

Re: Core Dumped when strcmp called in Tree

Posted 29 April 2009 - 05:54 AM

Get rid of the &s here:
while (scanf("%s",&n) != EOF) {printf("%s ",&n);

You only need it for scanf, and only when you're reading into a value that's not already a pointer.
The problem could be in your makenode function, but why not investigate what the values of x and t->info are just before you call strcmp?
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1