【案例1】【错误案例】
#include<iostream>
#include<cstddef>
#include<cstdio>
#include<cstdlib>
#include<cstring>
using namespace std;
typedef struct charlink
{
char ch;
struct charlink *next;
} CharLink;
void insertCharLink(CharLink *q,const char *chr)
{
printf("#3 void insertCharLink q %p\n",q);
CharLink *t = (CharLink*)malloc(sizeof(CharLink));
t->ch = *chr;
t->next = NULL;
q->next = t;
q = q->next;
printf("#4 void insertCharLink q %p\n",q);
}
int main()
{
const char str1[] = {'2','3','5','\0'};
CharLink *p = (CharLink*)malloc(sizeof(CharLink));
p->next=0;
p->ch = 'Q';
CharLink *h = p;
int i=0;
printf("#1 main--initial p %p\n",p);
printf("#1 main--initial h %p\n",h);
cout<< "#1 p->ch " << p->ch<<endl;
while(str1[i])
{
printf("#2--%d main--while p %p\n",i+1,p);
printf("#2--%d main--while h %p\n",i+1,h);
printf("#2--%d main--while p->ch %c\n",i+1,p->ch);
printf("#2--%d main--while h->ch %c\n",i+1,h->ch);
if(h->next)
printf("#2 h->next->ch %c\n",h->next->ch);
insertCharLink(p,&str1[i]);
++i;
}
printf("# h->ch %c\n",h->ch);
printf("# h->next->ch %c\n",h->next->ch);