私はlac(Ubuntu)のemacsテキストエディタを使って、渡されたdelimeterの文字列を分割することになっていました。私はそれを実行したときにsegfaultedを実行しましたが、GDBを実行しましたが、strcpy(私は呼び出さない)でエラーを出しますが、おそらくsprintfで暗黙的に行われます。私は何かが間違っているとは思わなかったので、私はWindowsにブートし、Visual Studioを走り、それはうまくいきました。LinuxでCを書くのが初めてで、問題はWhileループでsprintf()(ループ外の呼び出しがエラーを起こさずに書き込むため、奇数です)、トークンを配列に書き込むことができます。誰かが私がどこに間違っているか教えてもらえれば、私はそれを高く評価します。ここでは、コードlinuxのコンパイルの問題
/* split()
Description:
- takes a string and splits it into substrings "on" the
<delimeter>*/
void split(char *string, char *delimiter)
{
int i;
int count = 0;
char *token;
//large temporary buffer to over compensate for the fact that we have
//no idea how many arguments will be passed with a command
char *bigBuffer[25];
for(i = 0; i < 25; i++)
{
bigBuffer[i] = (char*)malloc(sizeof(char) * 50);
}
//get the first token and add it to <tokens>
token = strtok(string, delimiter);
sprintf(bigBuffer[0], "%s", token);
//while we have not encountered the end of the string keep
//splitting on the delimeter and adding to <bigBuffer>
while(token != NULL)
{
token = strtok(NULL, delimiter);
sprintf(bigBuffer[++count], "%s", token);
}
//for(i = 0; i < count; i++)
//printf("i = %d : %s\n", i, bigBuffer[i]);
for(i = 0; i< 25; i++)
{
free(bigBuffer[i]);
}
} //end split()
あなたの問題はlinuxでのコンパイルではなく、クラッシュしているプログラムが原因です。 – Perry