指针的传递


引用传递

#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;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
指针传递和引用传递是C语言中两种不同的参数传递方式。在指针传递中,通过将变量的地址作为参数传递给函数,函数可以直接访问和修改该变量的值。而在引用传递中,通过将变量的引用作为参数传递给函数,在函数内部对该变量的操作会直接影响到原始变量的值。 在引用传递中,使用指针的引用传递可以实现对指针变量进行修改。比如,在函数中可以通过传入指针的引用来修改指针的指向,从而改变指针变量所指向的内存地址。这样可以方便地在函数中动态分配内存或者改变指针的指向。 在C语言中,引用传递可以通过指针来实现,因为指针也是一个变量,它存储的值是一个地址。通过将指针的地址作为参数传递给函数,函数可以通过修改指针的值来改变指针所指向的变量。 总结起来,指针传递和引用传递都可以用于在函数中修改变量的值,但是引用传递更加灵活,可以方便地修改指针变量的指向。在C语言中,可以通过指针的引用传递来实现引用传递的效果。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [C/C++的指针传递和引用传递](https://blog.csdn.net/weixin_43340455/article/details/124134347)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* [C++中引用传递指针传递的区别](https://blog.csdn.net/qq_42955211/article/details/113742661)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值