麻烦高手帮忙看个有关问题,感谢感谢!
麻烦高手帮忙看个问题,感谢感谢!!!!
------解决方案--------------------
- 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; }