Reputation: 1843
I am reading the input from stdin using fgets and then saving the integer values using atoi.
input: 1 2 3 4
int main()
{
char line[100];
char *token;
int row,column[10];
const char split[2] = " ";
fgets(line,100,stdin);
token=strtok(line,split);
row = atoi(token); //not getting any error
while( token != NULL )
{
token=strtok(NULL,split);
// printf("column %d",atoi(token)); //getting an exception
// column[0]= atoi(token);//error
}
}
Output:
a.exe
1 2
> 1 [main] a 16776 cygwin_exception::open_stackdumpfile: Dumping stack trace to a.exe.stackdump
Upvotes: 1
Views: 223
Reputation: 206577
You are using token
in the wrong order to extract the values.
When you use:
while( token != NULL )
{
token=strtok(NULL,split);
// What happens you reach the end of the tokens and
// token is NULL? You are still using it in atoi.
printf("column %d",atoi(token));
column[0]= atoi(token);
}
You need to use:
while( token != NULL )
{
printf("column %d",atoi(token));
column[0]= atoi(token);
token=strtok(NULL,split);
}
Upvotes: 4