I've received a segmentation fault in this part of code:
Code:
for (; str[i] != '\0' || str[i] != ' '; i++)
{
result[result_pos] = str[i];
result_pos++;
}
which, should mean:
Code:
If str[i] is not equal to NULL character nor ' '; then, keep printing the chars within the word to variable result.
To understand what the function does, here's the whole code:
Code:
static char result[999];
/* char *split_word(const char *str, long n);
* Splits a string word-by-word. Where N is the word number. */
char *split_word(const char *str, long n)
{
size_t result_pos = 0;
long scr;
size_t i = 0;
for (scr = 0; scr < n; scr++)
{
for (; str[i] != ' '; i++) ; /* Scrolls forward to the desired word */
i++; /* Skip one char (the space). */
}
for (; str[i] != '\0' || str[i] != ' '; i++)
{
result[result_pos] = str[i];
result_pos++;
}
return result;
}
I'm trying to accomplish an easier way round to split string by word than using "strtok_r()". It might sound like "re-inventing the wheel", but... i will try to make it neat later.
The code looks okay, why would it segfault? how can i fix that?