char **rhyme; // init my double pointer
rhyme=inputrhyme(); // calls below function to input the rhyme
char** inputrhyme(){
char **rhyme, *temp, *token, BUFF[300], s[2]=" ";
int length=0;
rhyme=malloc(length * sizeof(char*));
printf("please enter a rhyme: \n");
scanf(" %[^\n]",BUFF);
token = strtok(BUFF, s);
while( token != NULL )
{
length++;
temp=realloc(rhyme, length* sizeof(char*));
if (temp==NULL){
printf("fatal error!\n");
exit(EXIT_FAILURE);
}
rhyme=temp;
rhyme[length-1]=token;
token = strtok(NULL, s);
}
length++;
temp=realloc(rhyme, length* sizeof(char*));
if (temp==NULL){
printf("fatal error!\n");
exit(EXIT_FAILURE);
}
rhyme=temp;
rhyme[length-1]=NULL;
return rhyme;
}
firstNode=sayRhyme(rhyme, firstNode); // goes through the rhyme saying each word
NodePtr sayRhyme(char **rhyme, NodePtr starting_player){
int index;
NodePtr temp=starting_player;
printf(" %s\n", rhyme[6]); // works fine for all valid values
for (index=0; index<9; index++){
printf("---------%d %s\n",index, rhyme[index]); // problem area
}
上記は、このリズムに関連するほとんどすべてのコードです。ダブルポインターを関数に渡すときは、それを読み取るだけでよいので、ポインターを渡しませんでした。韻の任意の値を読み取ることができますが、ループに入れようとすると、データが何らかの形で破損します。これは私が得る出力です:
please enter a rhyme:
help im stuck in a computer for real now *user input*
help
im
stuck
in
a
computer
for
real
now
first word was help
for
---------0 help
---------1 im
---------2 stuc$
---------3
---------4
▓
---------5
---------6 for
---------7 ▄²(
---------8 ≥7vÅ≥7vδ╛ΣW
player 0 is eliminated
ここで何が間違っているのかわかりません。ダブルポインターをトリプルとして渡し、同じ結果でそれを参照しようとしました。