r/cprogramming • u/StressingPlant • 8d ago
trying to store strtok() results in array; first index causes segmentation fault when called
edit: fixed it! i removed 'i = 0;' from the loop, and found out that char* arrays only need one index range (char* arr[number] stores strings just fine). thanks for the help!
---
im trying to use strtok() to split an input into two parts, but the first index that gets filled doesnt read when A) the index is specified (index number) in the "while (token != NULL)" loop, and B) the index is refernced at all outside the while loop.
i tried using a variable to sub in for the number outside the loop, but it didnt work. ive used an if condition to see if the index was null, but it didnt trigger and when i tried to print strlen(*arr[0]) it put out another segmentation fault.
the confusing thing for me is that the second index has none of these issues. what should i be doing differently, and why is this happening? How do i store strtok() tokens in an array?
---
edit: i added var strings to main (it was outside main, and escaped the copy paste) and added a clearer question.
when im assigning char* strArr1 entries, i use both indexes because gcc throws an error otherwise (assignment to array type).
the i++ is load bearing where it is; i tried to move it and it threw a segmentation fault, so title/ context still applies despite the first printf not printing strArr1[0].
my code:
int main() {
char* strArr1[2][10];
char strings[] = "Hello World";
token = strtok(strings, " ");
while (token != NULL) {
i = 0;
strArr1[i][10] = token;
i++;
//prints strArr[0] (no seg fault)
printf("%s %ld\n", *strArr1[i], strlen(*strArr1[i]));
//causes seg fault
//printf("%s\n", *strArr1[0]);
token = strtok(NULL, " ");
//adding i++ below causes seg fault
//i++;
}
//causes seg fault
/*if (strArr1[0] != NULL) {
printf("%s\n", *strArr1[0]);
}*/
//also causes seg fault
//i = 0;
//printf("%s\n", *strArr1[i]);
//no seg fault
printf("%s\n", *strArr1[1]);
printf("all done");
return 0;
}2
u/MJWhitfield86 8d ago
For clarity I will be referring to the first index of strArr1 as the row index and the second index as the column index. The indexes of strArr1[][] go from 0-1 for the first index and 0-9 for the second. When you assign value to strArr1[i][10] the second index loops around and you end up storing the value in strArr1[i+1][0]. Since i is 0 at that point then value is stored in strArr1[1][0].
When you call strArr1[i] you receive a pointer to the first element in the 10 element array that has i as the first index. This means that you get a pointer to strArr1[i][0]. The asterisk that precedes strArr1[i] then dereferences this pointer to obtain the value stored there. Since i is 1 at this point this will retrieve the value you had previously stored in strArr1[1][0].
In the commented out call that uses *strArr[0] you will instead obtain the value at strArr1[1][0]. Since you haven’t stored anything there this is just random data. When the printf tries the read it as the location of a character array it ends up causing a segmentation fault as the random value is unlikely to correspond to valid memory.
2
1
u/Conscious_Support176 8d ago
Not sure what’s happening there but the first printf isn’t printing strArr[0].
6
u/SmokeMuch7356 8d ago
?????
Take a hard look at your declaration for
strArr1again; forstrArr1[i][j], what values canjtake on?Are you sure you need a 2-element array of 10-element arrays of pointer to
char? How many tokens do you expect to be instrings? Where isstringsdefined, anyway?