4

i'm currently doing a simulation of adt in c, and i'm supposed to make a binary search tree invloving strings, i'm currently starting coding but i get this error and it doesnt say where the error comes from, here's the code, can somebody help me.

tree.h

#ifndef tree_h
#define tree_h
#include <stdbool.h>
#include <stdlib.h>

typedef struct tree_node* node_ptr;

struct tree_node {
    char* word;
    node_ptr leftNode, rightNode;
};

node_ptr start = NULL;

void addItem(char*, int);
void display();

#endif

tree.c

#include "tree.h"
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>


void addItem(char arr[], int mode) {
    node_ptr temp, temp2;
     
    temp = (node_ptr)malloc(sizeof(struct tree_node));
    temp->leftNode=NULL;
    temp->rightNode=NULL;
    
    if(mode == 1){
        temp->word = arr;
        start = temp;
        }
    }  



void display() {
    node_ptr temp;
    temp = start;
    printf("%s", start->word);       
}

main.c

#include "tree.h"
#include <stdio.h>
#include <conio.h>

int main() {
    char word[31];
    
    printf("Enter Root Word: ");
    gets(word);
    addItem(word, 1);
}
6
  • On this page we confirmed that this is indeed homework and found the problem. Commented Jul 24, 2012 at 0:14
  • 1
    thanks for helping, combining stackoverflow and piratepad is very helpful for newbies like me. i'll keep in mind to add the needed tags next time. thanks. :) Commented Jul 24, 2012 at 0:17
  • 1
    @cjBucketHead As you can see, you can post the code here itself rather than on some other site. Commented Jul 24, 2012 at 0:23
  • 1
    You will probably want to go ahead and "accept" one of the answers below by clicking on the check mark to award some reputation points to the person you feel answered your question best. For doing so, you will get some reputation points as well. Commented Jul 24, 2012 at 0:27
  • @Mahesh i'm a bit confused as to how to put codes here. :\ Commented Jul 24, 2012 at 0:27

4 Answers 4

19

The problem is with the statement in tree.h.

node_ptr start = NULL;

And you are including tree.h in both main.c and tree.c. This gives the multiple definition error for the variable start which is at global scope. What you actually need is,

// tree.h

extern node_ptr start;  

And have the definition in a single source file like -

// tree.c

#include "tree.h"
........
node_ptr start = null;
Sign up to request clarification or add additional context in comments.

3 Comments

To more easily see these types of problems, it can be helpful to run only the pre-processor and examine the output manually.
@bta: I have almost never found that to help me, but I code in C++, so maybe that's related.
Simplest explanation and solution I've found. There are a lot of more technical explanations (it feels like I've read them all), but this is the one that brought it all to a very practical point.
7

The error that you're talking about is:

... multiple definition of `start'

Since you have node_ptr start = NULL; in tree.h, each compilation unit that includes tree.h will have their own definition of the variable start. When it comes time to linking, the linker will see multiple definitions of the variable start and throw an error.

To avoid this, define start in tree.c:

node_ptr start;

And then declare start as extern, so that other compilation units know about it but won't try and define it themselves, in your header file tree.h:

extern node_ptr start;

Comments

3

Your error is that you define:

node_ptr start = NULL;

In your header file, thus when you import it (regardless of the macro guard) you're going to get two redefinitions of start.

Comments

1
if(mode == 1)
{
temp->word = arr;/*here is another error*/
    start = temp;
    }

arr is local to main and i think for more nodes you must reuse the arr array for getting input.at that time it reflect in all the nodes as your word of each node point to same memory location.So it is not a option to directly assign.

You have to allocate memory dinamically for the string also.and then use strcpy() function for copying data to dynamic memory location.

ok use this code at that place:-

if(mode==1)
 {
  w_len=sizeof(char)*(strlen(arr)+1);//one extra character for null character
  temp->word=malloc(w_len);
  strcpy(temp->word,arr);
  start=temp;
 }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.