今、例として"ABC,EFG,HIJ,KLM"を","区切りで配列に格納して出力するプログラムを書いて実行したのですが、
ABC
EFG
HIJ
KLM
となるはずのところが、セグメンテーション違反が出てしまいました。
どこがおかしいのでしょうか?
ご指摘お願いします。
#include <stdio.h>
#include <string.h>
int main(void)
{
char str[] = "ABC,EFG,HIJ,KLM";
char *tp;
int i, count = 1;
char str2[32][32];
tp = strtok(str, ",");
strcpy(str2[0], tp);
while(tp != NULL)
{
tp = strtok(NULL, ",");
strcpy(str2[count++], tp);
}
for(i = 0; i < 4; i++)
{
printf("%s\n", str2[i]);
}
return 0;
}