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
我不确定我在这里做错了什么。我试图将双指针作为三元组传递并以相同的结果尊重它。