I'm getting a segmentation fault for this code and I can't, for the life of me, figure out why. I'm rather new to pointers so it could be something obvious. The code is supposed to take two lines of integers from stdin and print them out in an alternating fashion. I haven't finished the last part of the code which prints out the alternating numbers.
int main(){
char *str1 = NULL;
char *str2 = NULL;
size_t sz = 0;
int i;
int x;
char *rp1 = NULL;
char *rp2 = NULL;
getline(&str1, &sz, stdin);
getline(&str2, &sz, stdin);
char *result1;
result1 = malloc(sizeof(*str1));
char *result2;
result2 = malloc(sizeof(*str2));
for(i = 0; (x = sscanf(str1, "%s ", &result1[i])) > 0; i++){
if(x == EOF){
return 0;
}
if(!isdigit(result1[i])){
fprintf(stderr, "Error: invalid non-integer input\n");
return 1;
}
}
rp1 = malloc(i);
rp1 = result1;
for(i = 0; sscanf(str2, "%s ", &result2[i]) > 0; i++){
if(!isdigit(result2[i])){
fprintf(stderr, "Error: invalid non-integer input\n");
return 1;
}
}
rp2 = malloc(i);
rp2 = result2;
return 0;
}
When I run gdb, it says I'm getting the segmentation fault at line 25, which is the first for loop.
Edit: So I've fixed one issue which was in the malloc function before the for loops, but I'm still getting the segmentation fault.