Click here to Skip to main content
15,895,606 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
This is an awkward question, but if I don't ask I will never know what's going on, so thanks for your help in advance. I'm attempting exercise 6-2 in K&R (binary tree). It requires me to write a program that reads a C program and prints in alphabetical order each group of variable names that are identical in the first 'X' characters, but different somewhere thereafter. My approach is to define a struct that holds an array of words rather than just one word (as the example given in the book), so words that have the same 'X' characters can be stored in the same node. So I tried testing this concept before address the entire. But when I try running the following code, while the addtree function seems to work (according to the debugger), the treeprint doesn't. It prints the variables that have the same first 3 characters before crashing (segmentation error, so it doesn't print the words in the other nodes). Not really sure what's going on. Thanks!

By the way, I find the binary tree a really intriguing concept!

What I have tried:

struct tnode *addtree(struct tnode *p, char *w){
	int cond;
	
	if(p==NULL){
		p=talloc();
		p->np=p->word;
		*(p->np)=strdup(w);
		p->count=1;
		p->left=p->right=NULL;
	}
	else if((cond=strncmp(w,*(p->np),3))==0){
		(p->np)++;
		*(p->np)=strdup(w);
	    p->count++;
	}
	else if(cond<0)
		p->left=addtree(p->left,w);
	else 
		p->right=addtree(p->right,w);
	return p;
}

void treeprint(struct tnode *p){
	if(p!=NULL){
		treeprint(p->left);
		for(int i=0;(p->word[i])!="";i++)
			printf("%4d %s\n",p->count,p->word[i]);
		treeprint(p->right);
	}
}
Posted
Updated 7-Mar-17 21:23pm

When you don't understand what your code is doing or why it does what it does, the answer is debugger.
Use the debugger to see what your code is doing. Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute, it is an incredible learning tool.

Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]

The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.
 
Share this answer
 
This can't be answered in detail without knowing the struct tnode and the talloc() function.

Some suspicious instructions:
if(p==NULL){
    /* What is talloc() doing? */
    /* Ensure that it allocates memory to hold a struct tnode */
    p=talloc();
    /* p has just been allocated. */
    /* So p->word is undefined if not set by talloc(). */
    p->np=p->word;
    *(p->np)=strdup(w);
    p->count=1;
    p->left=p->right=NULL;
    /* p->word is undefined if not set by talloc(). */
}
else if((cond=strncmp(w,*(p->np),3))==0){
    /* This points behind allocated memory when p->count is >= */
    /*  the size of the p->word array */
    (p->np)++;
    *(p->np)=strdup(w);
    p->count++;
}
/* ... */

/* Compares a string pointer with a pointer to an empty string which is */
/*  usually always false (pointers have never the same value). */
/* To check for an end of a list of strings compare with NULL. */
/* But this requires that the list is initialised with NULL pointers. */
/* I guess that using p->count is what you want to use here. */
for(int i=0;(p->word[i])!="";i++)
    printf("%4d %s\n",p->count,p->word[i]);
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900