c语⾔打印变量地址_在C中打印变量的地址
c语⾔打印 变量地址
print the address of a variable, we use "%p" specifier in C programming language. There are two ways to get the
To print the address of a variable
address of the variable:
打印变量的地址 ,我们使⽤C编程语⾔中的“%p”说明符。 有两种获取变量地址的⽅法:
要打印变量的地址
1. By using "address of" (&) operator
通过使⽤“地址的”( & )运算符
2. 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:
阅读更多:
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. Note:
注意:每次运⾏时输出可能会改变。
注意:
c语⾔打印 变量地址

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。