c语言打印 变量地址_在C中打印变量的地址

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”说明符。 有两种获取变量地址的方法:

  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: 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语言打印 变量地址

  • 6
    点赞
  • 26
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值