So, i was writing a a simple slice function in C, which takes an array of strings, the string that marks the beginning of the slice, and the size of the slice. in the function i malloc a new array, and then proceed to copy over each string in the slice to the new array. however, i get a segfault on the very first strcpy, even though i've already malloced space for the result array.
the code looks like this:
char** slice(char** args, char* start, int size){
int i = 0;
// need to find start first
char* cursor = args[0];
int j = 0;
while(cursor != NULL){
if(strcmp(cursor, start) == 0){
break;
}
j++;
cursor = args[j];
}
char** result = malloc(MAX_INPUT * size);
while(i < size){
strcpy(result[i], args[j+i]);
i++;
}
return result;
}
the line which casues the segfault is --
strcpy(result[i], args[j+i]);
i've used gdb to peek into the values of whats in result and args, result[i] is 0x0, which is NULL, but result itself is an address but i'm not sure why malloc isn't working. have i run out of stack space? does this mean i'm screwed?