c语言打印 变量地址
To print the address of a variable, we use "%p" specifier in C programming language. There are two ways to get the address of the variable:
要打印变量的地址 ,我们使用C编程语言中的“%p”说明符。 有两种获取变量地址的方法:
By using "address of" (&) operator
通过使用“地址的”( & )运算符
By using pointer variable
通过使用指针变量
1)通过使用“地址”(&)运算符 (1) By using "address of" (&) operator)
When we want to get the address of any variable, we can use “address of operator” (&) operator, it returns the address of the variable.
当我们想要获取任何变量的地址时,可以使用“运算符的地址”(&)运算符,它返回变量的地址。
Program:
程序:
#include <stdio.h>
int main(void)
{
// declare variables
int a;
float b;
char c;
printf("Address of a: %p\n", &a);
printf("Address of b: %p\n", &b);
printf("Address of c: %p\n", &c);
return 0;
}
Output
输出量
Address of a: 0x7ffd3d518618
Address of b: 0x7ffd3d51861c
Address of c: 0x7ffd3d518617
2)通过使用指针变量 (2) By using pointer variable)
A pointer is the type of a variable that contains the address of another variable, by using the pointer; we can also get the address of another variable.
指针是变量的类型,通过使用指针,该变量包含另一个变量的地址; 我们还可以获取另一个变量的地址。
Read more: Pointers in C language
阅读更多: C语言指针
Program:
程序:
#include <stdio.h>
int main(void)
{
// declare variables
int a;
float b;
char c;
//Declare and Initialize pointers
int *ptr_a = &a;
float *ptr_b = &b;
char *ptr_c = &c;
//Printing address by using pointers
printf("Address of a: %p\n", ptr_a);
printf("Address of b: %p\n", ptr_b);
printf("Address of c: %p\n", ptr_c);
return 0;
}
Output
输出量
Address of a: 0x7ffd3d518618
Address of b: 0x7ffd3d51861c
Address of c: 0x7ffd3d518617
Note: At every run output may change.
注意:每次运行时输出可能会改变。
翻译自: https://www.includehelp.com/c-programs/printing-an-address-of-a-variable.aspx
c语言打印 变量地址