atof と strtok の使用に問題があります。
#include<stdio.h> // printf
#include<stdlib.h> // atof
#include<string.h> // strtok
int main()
{
char buf[256]="123.0 223.2 2314.2";
char* tp;
printf("buf : %s\n", buf);
tp = strtok(buf," ");
printf("tp : %g ", atof(tp));
while (tp!=NULL) {
tp = strtok(NULL," ");
printf("%g ", atof(tp));
}
return 0;
}
上記のコードをコンパイルでき、エラーや警告メッセージは返されません。しかし、「a.out」を実行すると、以下のようにセグメンテーション違反が返されます。
78746 Segmentation fault: 11 ./a.out
何が問題なのかわからない。ご覧のとおり、上記のコードは構文エラーを複雑にしません。