69,373
社区成员
发帖
与我相关
我的任务
分享
void GetMemory(char * p,int size)//不用二级指针
{
p = (char*) malloc(size);
}
void main()
{
char * pStr=NULL;
GetMemory(pStr,10);//此处调用完,会发现pStr仍为NULL。因为传值造成的。
//不要看见指针就认为传址,那要看操作的数是什么!
}
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef struct node{
char element;
struct node *next;
} LNode,*LinkList;
LNode *Creat(LNode **h) /*参数为2级指针*/
{
LNode /* *h,*/*p,*t;
int first = 1;
/*
h=(LinkList)malloc(sizeof(LNode));
h->next=NULL;
*/
*h = (LinkList)malloc(sizeof(LNode)); /*创建第一个结点*/
(*h)->next = NULL;
/*t=h;*/
t = *h; /*t指向当前创建链表的尾节点*/
char ch;
printf("请输入一个字符串,回车结束\n");
while((ch=getchar())!='\n')
{
if(first == 1) {
(*h)->element = ch; /*头结点里保存数据*/
first = 0;
continue;
}
p=(LinkList)malloc(sizeof(LNode));
p->element=ch;
p->next=NULL;
t->next=p;
t=p;
}
/*return h;*/
return *h;
}
void Show(LinkList h)
{
if(h==NULL) {
printf("链表为空!\n");
}
/*LNode *p=h->next;*/
LNode *p = h;
while(p!=NULL)
{
printf("%c ",p->element);
p=p->next;
}
}
void main()
{
/*LinkList h=Creat();*/
LinkList h; /*一级指针*/
Creat(&h); /*将一级指针的地址传入*/
Show(h);
}
InitList(Linklist *L)//Linklist为节点指针类型
{
*L=NULL;//L的值即头指针,初值为NULL
}