读书人

麻烦高手帮忙看个有关问题感谢感谢!

发布时间: 2012-03-18 13:55:39 作者: rapoo

麻烦高手帮忙看个问题,感谢感谢!!!!

C/C++ code
void test(){       char tempBuffer[] = "- This, a sample string.";    char *ptoken;    ptoken = strtok(tempBuffer, " ,.-");    struct wordInfo *ppWordInfoList;    struct wordInfo *pWordInfo;    while(ptoken != NULL)    {         struct wordInfo *pWordInfo = (struct wordInfo*)realloc(pWordInfo, sizeof(wordInfo));    [color=#FF0000]   //为什么在上一行报错,应该怎么改?[/color]       pWordInfo->pstr = (char*)realloc(pWordInfo->pstr, strlen(ptoken)+1);       strcpy(pWordInfo->pstr, ptoken);       printf("%s\n", pWordInfo->pstr);       ptoken = strtok(NULL, " ,.-");    }}


[解决办法]
C/C++ code
#include<vector>#include<iostream>using namespace std;struct wordInfo{     char *pstr;   struct wordInfo *next;};void test(){       char tempBuffer[] = "- This, a sample string.";    char *ptoken;    int i = 0,j;    ptoken = strtok(tempBuffer, " ,.-");    struct wordInfo **ppWordInfoList = (struct wordInfo**)malloc(sizeof(struct wordInfo*));    while(ptoken != NULL)    {         if( i != 0)       {           ppWordInfoList = (struct wordInfo**)realloc(ppWordInfoList, (i+1)*sizeof(struct wordInfo*));       }       ppWordInfoList[i] = (struct wordInfo*)malloc(sizeof(struct wordInfo));       ppWordInfoList[i]->pstr = (char*)malloc(strlen(ptoken) + 1);       strcpy(ppWordInfoList[i]->pstr, ptoken);       printf("%s\n", ppWordInfoList[i]->pstr);       ptoken = strtok(NULL, " ,.-");       i++;    }    printf("aaaaaaaaaaaaaa\n");    for(j = 0; j < i; j++)    {               printf("%s\n", ppWordInfoList[j]->pstr);    }}int main(){    test();    return 0;} 

读书人网 >C语言

热点推荐