开发平台(Platform): (Ex: VC++, GCC, Linux, ...)
dev C++
额外使用到的函数库(Library Used): (Ex: OpenGL, ...)
string.h
问题(Question):
我用 memcpy (Ptr->ListString, CharPtr, Length);
去喂资料,然后用link list 去存值,在只有两个字符时,会多了残余值。
喂入的资料(Input):
char StringOriginalData[100] = "Abian is son of the bitch";
预期的正确结果(Expected Output):
Abian 5
is 2
son 3
of 2
the 3
bitch 5
请按任意键继续 . . .
错误结果(Wrong Output):
Abian 5
iss 2
son 3
ofs 2
the 3
bitch 5
请按任意键继续 . . .
程式码(Code):(请善用置底文网页, 记得排版)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char StringOriginalData[100] = "Abian is son of the bitch";
char StringChange[10] = "are";
typedef struct _VOCABULARY_LIST {
char *ListString;
struct _VOCABULARY_LIST *Node;
}VOCABULARY_LIST;
void TearOffAndAdd (VOCABULARY_LIST *Ptr);
int main (int argc, char *argv[]) {
VOCABULARY_LIST *FirstVocabulary;
VOCABULARY_LIST *PtrVocabulary;
FirstVocabulary = (VOCABULARY_LIST *)malloc (sizeof (VOCABULARY_LIST));
PtrVocabulary = FirstVocabulary;
PtrVocabulary->Node = NULL;
TearOffAndAdd (PtrVocabulary);
system("PAUSE");
return 0;
}
void TearOffAndAdd (VOCABULARY_LIST *Ptr) {
char *CharPtr;
VOCABULARY_LIST *NewList;
int Length;
CharPtr = StringOriginalData;
Length = strcspn (CharPtr, " ");
while (Length != 0) {
Ptr->ListString = (char *)malloc (Length * sizeof(char));
memcpy (Ptr->ListString, CharPtr, Length);
printf ("%s", Ptr->ListString);
printf (" %d\n",Length);
NewList = malloc (sizeof (VOCABULARY_LIST));
NewList->Node = NULL;
Ptr->Node = NewList;
Ptr = NewList;
CharPtr += (Length + 1);
Length = strcspn (CharPtr, " ");
}
}
补充说明(Supplement):
这到底怎么回事?