取地址符和解引用符的区别
Here, we are discussing about the two most useful operators with the pointers, why and how they are used?
在这里,我们用指针讨论两个最有用的运算符 ,为什么以及如何使用它们?
1)运营商地址(&) (1 ) The Address of Operator (&))
It is an "address of" operator which returns the address of any variable. The statement &var1 represents the address of var1 variable. Since it can be used anywhere but with the pointers, it is required to use for initializing the pointer with the address of another variable.
它是一个“地址”运算符 ,它返回任何变量的地址。 语句&var1代表var1变量的地址。 由于它可以与指针一起在任何地方使用,因此需要使用另一个变量的地址来初始化指针 。
2)解除引用运算符(*) (2 ) The Dereference Operator (*))
It is used for two purposes with the pointers 1) to declare a pointer, and 2) get the value of a variable using a pointer.
它用于指针的两个目的:1) 声明一个指针 ,2) 使用指针获取变量的值 。
Read more: Accessing the value of a variable using pointer in C
: 使用C语言中的指针访问变量的值
Example:
例:
#include <stdio.h>
int main(void)
{
//normal variable
int num = 100;
//pointer variable
int *ptr;
//pointer initialization
ptr = #
//printing the value
printf("value of num = %d\n", *ptr);
//printing the addresses
printf("Address of num: %x\n", &num);
printf("Address of ptr: %x\n", &ptr);
return 0;
}
Output
输出量
value of num = 100
Address of num: 9505c134
Address of ptr: 9505c138
翻译自: https://www.includehelp.com/c/the-address-of-and-dereference-operators-with-the-pointers-in-c.aspx
取地址符和解引用符的区别