这是一个链表有关的练习
struct node{
void *pData;
int Dtype; //标志节点类型,是1表示是int,是0表示是char
struct node *pNext;
}Node;
typedef struct node *PNode;
然后这个函数中
PNode CreatList(){
……
……
有这么一段
scanf("%s",&datac);
p->pData = (char*)datac;
p->Dtype = 0;
printf("input is char %s\n", p->pData);
【此时print的pData是正常字符串不是乱码】
……
}
==
然而我遍历链表的时候
void Print(PNode head){
PNode tmp;
tmp = head;
if (head != NULL){ //只要不是空链表,就输出链表中所有节点
printf("head is %o\n", head); //输出头指针指向的地址
do{
switch(tmp->Dtype){
case 1:
printf("%d\n",tmp->pData);【这里正常输出int类型的数字】
break;
case 0:
printf("%s\n",tmp->pData); 【这里输出的是乱码,求解】
break;
}
tmp = tmp->pNext; //移到下一个节点
}
while (tmp != NULL);
}
}