I am working on a C function to split a string of letters into an array, however it keeps seg faulting and I have no idea why.
I have run it through DDD a few times, it was not of much help.
char* stringToArray(const char *desc)
{
int i = 0;
int j = 0;
char *array[5][10] = {{0}};/*5 words, 10 chars each*/
while (desc[i] != '\0')
{
if (desc[i] != ' ') {
*array[j][i] = desc[i]; /*seg faults here*/
}
else {
j++;
}
i++;
}
return **array;
}
Any help would be greatly appreciated Thanks.
array[J][I]is a pointer. You haven't made any of all those pointers point to somewhere valid.array). That won't work.*works only locally, inside the function. Once the function returns the array ceases to exist and the return value (the pointer to the inexistent array) is unusable. Consider passing to the function the array that receives the words.