c - 为什么打印出来的变量地址是0x7fff935ad2b0这样的形式?

标签 c

<分区>

我只是写了一个简单的程序来打印变量的地址。

#include <stdio.h>

void main(){
  int *a;
  int b;

  b=5;
  a=&b;

  printf("\n*a is the value of the integer:  %d\n",*a);
  printf("&a is the address of variable a: %p\n", &a);
  printf(" a is the address stored in a:   %p\n",a);
  printf("&b is the address of variable b: %p\n\n",&b);
}

结果是:

*a is the value of the integer:  5
&a is the address of variable a: 0x7fff935ad2b0
 a is the address stored in a:   0x7fff935ad2bc
&b is the address of variable b: 0x7fff935ad2bc

为什么打印的地址不是标准格式?

抱歉这个愚蠢的问题,但我就是不明白。

编辑

我有另一个程序

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct small{
  int a;
};

int main(){
   struct small *sm;

   sm = malloc(sizeof(struct small));
   memset(sm,0,sizeof(struct small));

   sm->a = 5;

   printf("The address of sm is: %p\n", &sm);
   printf("The address stored in sm is: %p\n", sm);

   return 0;
}

输出是:

The address of sm is: 0x7fffd1363158
The address stored in sm is: 0x17a3010

所以我希望格式 0x17a3010 对我来说有点标准。

为什么在这种情况下 %p 的格式不同?

最佳答案

标准没有说明 %p 应该使用什么格式。

The argument shall be a pointer to void. The value of the pointer is converted to a sequence of printable characters, in an implementation-defined manner.

因此,通常是十六进制,因为它在处理地址时最方便。

关于c - 为什么打印出来的变量地址是0x7fff935ad2b0这样的形式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9574603/

相关文章:

c - 将txt文件的结构加载到动态数组

c - Xcode5与C失去连接错误

在自己的网站上进行c编译器来测试用户输入

c - 获取一个子程序以在 C 中返回三个独立的随机数数组

c - 为什么 malloc 不是 "using up"我电脑上的内存?

与 memcpy 连接

c - valgrind memcheck 分配字符串内存时出错

c - 未初始化指针的可能输出

c - 赋值和指针,未定义的行为?

c - 为什么 print 语句根本没有被执行?