0

I am working on the same project as in this question, however with a slightly different typedef:

typedef struct {
    char* word;
    int index;
} data_t;

typedef struct node node_t;

typedef node {
    void *data;
    node_t *left;
    node_t *right;
}

I am trying to split a string into individual words, sticking it into the data_t struct and then inserting it into a binary search tree. The idea is that, while looping across an input string, when it is found that the character is one which marks the end of a word, the number of characters between the start and end of the word is copied into a string, which I have attempted to do using:

strncpy(newstring, (in+wordstart), (i-wordstart));

where:

char* newstring, in;
int i, wordstart;

However, gcc gives a bus error when that particular line is called. How this bus error can be fixed and the current solution kept, or would it be wiser to look for a different solution?

The only idea I have been able to think of so far is to, one character at a time, put the characters into the string until the end of the word has been reached.

Community
  • 1
  • 1

2 Answers2

4

This doesn't declare two pointers, this is just one pointer and a char

char* newstring, in;

It should be declared like this:

char *newstring, *in;

Also, as someone else noted, it doesn't seem that you're allocating any memory for those pointers.

iabdalkader
  • 17,009
  • 4
  • 47
  • 74
  • 1
    Good catch! And another nice example why to always declare a variable on it's own line! ;-) – alk Oct 23 '12 at 08:01
  • haha very good point there and thanks for pointing it out! Honestly I put that there to indicate how the strncpy line was type defined. in is passed in to the function via `char *in`, though `newstring` was declared as above. Apologies for that though I will keep your point in mind :) – disgruntledperson Oct 23 '12 at 08:05
1

This:

typedef node {
    void *data;
    node_t *left;
    node_t *right;
}

doesn't make any sense at all. There's no struct keyword before the node, and no name for the type alias you're trying to introduce after the }, and no terminating semi colon. This shouldn't compile.

unwind
  • 391,730
  • 64
  • 469
  • 606