So I’m doing what the title says with this code
else if(input[0] == 'i'){
printf("This works");
if(isspace(input[1])){
x = 2;
while(input[x] != '\0'){
tempS[x - 2] = input[x];
}
inp = atoi(tempS);
for(x = x-2; x >= 0; x--){
tempS[x] == NULL;
}
insert(&linkedList, inp);
printf("%i %s %c", inp, " sucessfully inserted.", '\n');
}
}
You can ignore the insert, basically I get user input which should be i and some number.
For example: i 27
Then the program should recognize that it’s i in that else if, and take the number after the i.
Variable initialization is as follows.
char *input;
int inp = 0;
int x = 0;
char *tempS[255];
Thanks in advance.
Edit: The problem is that I get a segmentation fault before even the printf.
You are stuck in an infinite loop.
Add the instruction
x++;So you can read the next element in the array input, otherwise, you are stuck in the position x = 2.This
char *input; it is only a pointer, you have to allocate memory to use it like you are currently using. Do the following:N is the number of elements that you want to have in the input array.
You are getting
segmentation faultbecause you are trying to access memory, which was not allocated before.