指针的传递


引用传递

#include <unistd.h>
#include <stdio.h>
#include <sys/stat.h>
#include <stdlib.h>


struct st
{
	int a;
	struct st * pst;
};

void fun(struct st *& pst)
{
	struct st * p;
	p=(struct st *)malloc(sizeof(struct st));
	if(NULL==p)
	{
		exit(-1);
	}
	p->a=2;
	p->pst=NULL;
	pst=p;
}

int main()
{

	struct st * root;
	fun(root);
	printf("result: %d\n",root->a);
	return 0;
}



指针的指针传递

#include <unistd.h>
#include <stdio.h>
#include <sys/stat.h>
#include <stdlib.h>


struct st
{
	int a;
	struct st * pst;
};

void fun(struct st ** pst)
{
	struct st * p;
	p=(struct st *)malloc(sizeof(struct st));
	if(NULL==p)
	{
		exit(-1);
	}
	p->a=2;
	p->pst=NULL;
	*pst=p;
}

int main()
{

	struct st * root;
	fun(&root);
	printf("result: %d\n",root->a);
	return 0;
}


你可能感兴趣的:(指针传递)