使用指针星号转移c语言,C中的指针:何时使用&号和星号?

本文详细解释了如何在C/C++中使用指针,并展示了如何将指针转换为值及将值转换为指针的方法。此外,还介绍了数组与指针之间的相似性,包括如何通过指针和索引运算符访问数组元素。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

你有指针和值:

int* p; // variable p is pointer to integer type

int i; // integer value

将指针转换为带*的值:

int i2 = *p; // integer i2 is assigned with integer value that pointer p is pointing to

您可以将值转换为带&amp ;:的指针

int* p2 = &i; // pointer p2 will point to the address of integer i

编辑:

在数组的情况下,它们非常类似于指针。如果你认为它们是指针,你将使用*来获取其中的值,如上所述,但是还有另一个更常见的方法使用[]运算符:

int a[2]; // array of integers

int i = *a; // the value of the first element of a

int i2 = a[0]; // another way to get the first element

获取第二个元素:

int a[2]; // array

int i = *(a + 1); // the value of the second element

int i2 = a[1]; // the value of the second element

所以[]索引操作符是一个特殊形式的*运算符,它的工作原理如下:

a[i] == *(a + i); // these two statements are the same thing

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值