0

I need help with breaking a string into an array. I got it to work without storing the info and just printing the tokens. But for this prog, I need to store the tokens strtok made and use a binary search to do a strncmp with 2 elements each being from a different array.

./file "Example input: 'Cause I'm Batman"

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

int main(int argc, char *argv[])
{
  char delims[] = " ";
  char *result = NULL;

  int i = 1;
  int j = 0;

  char sent[1000];
  result = strtok(argv[1], delims);
  sent[0] = *result;
  while(result != NULL)
  {
    result = strtok(NULL, delims);
    sent[i] = *result;
    i++;
  }

  while(j < i)
  {
    printf(" %p\n", &sent[j]);
    j++; //Forgot to add it in first time around
  }
return 0;
}

Problem is I'm getting a segmentation fault and I can't seem to get it to store the tokens into an array and I don't understand why. Is it a pointer issue? Passing incompatible data types? Something else?

Edit: Wanted output: "Example" "input:" "'Cause" "I'm" "Batman"

Any help would be great.

5
  • sent[i] = *result; attempts to read NULL Commented May 27, 2013 at 7:16
  • 2
    The line sent[i] = *result; stores the first character of each token in the sent array. It doesn't seem like what you want. Also, while (j < i) is an endless loop. Commented May 27, 2013 at 7:17
  • Woops... Forgot to add the j incrementer... But yes, I am trying store a word into the array. So like the input, it would break the string into individual words which I'm trying to store. I got it to work when I was just printing the words not storing them. So I'm not sure what to do with the pointer if sent[i] = *result. How can I get it to store the word and not the first char? Commented May 27, 2013 at 7:28
  • try it: string str ="something"; use char * ch = st.c_str(); Commented May 27, 2013 at 8:11
  • C doesn't have string data types as far as I'm aware, just char arrays with multiple chars in an element. But I'm trying to have it take in arguments from the command line not have it coded in. I just don't know how to get the token stored and not the NULL value... Commented May 27, 2013 at 8:33

3 Answers 3

1

In your case it's very easy to figure out what's causing the crash: You dereference the NULL pointer.

Think for a minute what will happen with the expression *result when result is NULL.


Also, this error would have taken you less than a minute to find if you used a debugger. When a program crashes, a programmers first instinct should always be to run the program in a debugger.

Sign up to request clarification or add additional context in comments.

1 Comment

So... Since I'm setting the *result to NULL, does that mean that value of result is NULL as well? I thought this was so I could use a loop so that the strtok would skip over the chars I set to null and then to the next delimiter. But I'm guessing I'm still looking at it the wrong way.
0

fix like this

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

int main(int argc, char *argv[]){
    char delims[] = " ";
    char *result = NULL;
    int i = 0, j;
    char sent[1000];
    char *parray[32];//32 word

    strcpy(sent, argv[1]);//strtok destory string, argv[n] is read only.
    result = strtok(sent, delims);
    parray[i++] = result;
    while(NULL!= (result=strtok(NULL, delims))){
        parray[i++] = result;
    }

    for(j=0; j < i;++j)
        printf("%s\n", parray[j]);
    return 0;
}

1 Comment

Ok, this makes more sense than what I was trying to do and way simpler than me trying to do pointer manipulation. Thank you.
0

When strtok reaches '\0' NUL character in the string, it will return NULL pointer.

In your code 'result' is de-referenceed after making strtok(NULL, delims) call. As a result you will be de-referencing result without making a sanity check for its validity and will end up accessing NULL pointer.

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.