首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C语言 >

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

2012-03-18 
麻烦高手帮忙看个问题,感谢感谢!!!!C/C++ codevoid test(){char tempBuffer[] - This, a sample string

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

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;} 

热点排行