简体   繁体   English

为什么它用相同的变量给出不同的地址?

[英]Why it gives different address with same variable?

The both line of printf should print the address of variable,right? printf的这两行应该打印变量的地址,对吧? but they both give different value... Why it so, both should give same value coz memory location allocated to variable a is fixed.但它们都给出不同的值......为什么会这样,两者都应该给出相同的值,因为分配给变量a的 memory 位置是固定的。

#include <stdio.h> 

void main()
{
    int a = 5;
    int *ptra = &a;
    printf("%d", &a);
    printf("%p", ptra);
}

Here's the output of your program on my system:这是我系统上您的程序的 output:

-12115289320x7ffeb7c9891c

The two outputs are jammed together on one line, making it difficult to tell which is which.两个输出在一条线上卡在一起,很难分辨哪个是哪个。

Adding newlines to the output, I get:向 output 添加换行符,我得到:

344225740
0x7ffc148477cc

(There's no guarantee that a variable will have the same address from one run of a program to the next, and some systems deliberately change memory allocations.) (不能保证一个变量从一个程序运行到下一个程序都具有相同的地址,并且一些系统故意更改 memory 分配。)

The %d format specifier requires an argument of type int . %d格式说明符需要int类型的参数。 The correct format specifier for a pointer value is %p , and it requires an argument of type void* .指针值的正确格式说明符是%p ,它需要void*类型的参数。 Using inconsistent types, as you've done in your program, causes undefined behavior .正如您在程序中所做的那样,使用不一致的类型会导致未定义的行为 At best, on many systems pointers are 64 bits and int is 32 bits, so %d can't possibly show an entire pointer value.充其量,在许多系统上,指针是 64 位,而int是 32 位,因此%d不可能显示整个指针值。 It might show part of the pointer value, it might show garbage, or it might crash.可能会显示指针值的一部分,可能会显示垃圾,或者可能会崩溃。

The %p format typically uses hexadecimal, but the format is implementation-defined. %p格式通常使用十六进制,但格式是实现定义的。

Here's a corrected version of your program:这是您的程序的更正版本:

#include <stdio.h> 
int main(void)
{
    int a = 5;
    int *ptra = &a;
    printf("%p\n", (void*)&a);
    printf("%p\n", (void*)ptra);
}

and the output on my system:和我系统上的 output :

0x7ffd62a733ec
0x7ffd62a733ec

You'll see different output, but the two lines should match each other.您会看到不同的 output,但这两行应该相互匹配。

Note: void main() is incorrect;注意: void main()不正确; the correct declaration is int main(void) .正确的声明是int main(void)

They are the same number.它们是相同的数字。 You're printing one in decimal form, the other in hexadecimal form.您正在以十进制形式打印一个,另一个以十六进制形式打印。

#include<stdio.h> 
void main()
{int a=5;
int *ptra=&a;
printf("%d",&a);
printf("%p",ptra);
}

Using %d escape sequence in your first printf is converting your reference from hexadecimal to decimal, ie, (x) 16 --> (x) 10 .在您的第一个printf中使用%d转义序列会将您的引用从十六进制转换为十进制,即 (x) 16 --> (x) 10

NOTE: Don't use C++ tag because this question does not depend on any C++ feature.注意:不要使用 C++ 标签,因为这个问题不依赖于任何C++功能。

So your code should be:所以你的代码应该是:

#include<stdio.h>

int main(void)
{
    int a=5;
    int *ptra=&a;
    printf("%p\n",&a);
    printf("%p",ptra);
    return 0;
 }

Output: (may vary on your machine) Output:(可能因您的机器而异)

000000000061fe14
000000000061fe14

This happens due to the difference in format strings.这是由于格式字符串的差异而发生的。

%d    takes 32 bits and displays it as a signed value  123
%p    takes a pointer and display it in address format    0fef:0004

Actual value is same but there is difference of interpreting the value.实际值相同,但对值的解释存在差异。

As already mentioned in the answer from Keith Thompson ( https://stackoverflow.com/a/70982344/4386427 ) the reason is that you are printing an integer value by using %d but the argument isn't an integer - it is a pointer value. As already mentioned in the answer from Keith Thompson ( https://stackoverflow.com/a/70982344/4386427 ) the reason is that you are printing an integer value by using %d but the argument isn't an integer - it is a指针值。 Providing an argument with a type that doesn't match the format specifier (ie providing a pointer value for %d ) leads to undefined behavior which means that it may print any value, text or even nothing.提供与格式说明符不匹配的类型的参数(即为%d提供指针值)会导致未定义的行为,这意味着它可以打印任何值、文本甚至什么都没有。

All that is already well covered by Keith Thompson ( https://stackoverflow.com/a/70982344/4386427 ) Keith Thompson 已经很好地涵盖了所有这些内容( https://stackoverflow.com/a/70982344/4386427

I just like to add that in case you actually want to print a decimal integer representation of the pointer value, you can use the types intptr_t or uintptr_t .我想补充一点,如果你真的想打印指针值的十进制 integer 表示,你可以使用类型intptr_tuintptr_t Like:喜欢:

#include <stdio.h> 
#include <inttypes.h> 
int main(void)
{
    int a = 5;
    int *ptra = &a;
    printf("%p\n", (void*)&a);
    printf("%p\n", (void*)ptra);
    
    intptr_t dptr = (intptr_t)(void*)ptra;
    uintptr_t uptr = (uintptr_t)(void*)ptra;

    printf("A signed integer representation of the same pointer in decimal        : %" PRIdPTR "\n", dptr);
    printf("An unsigned integer representation of the same pointer in decimal     : %" PRIuPTR "\n", uptr);
    printf("An unsigned integer representation of the same pointer in hexadecimal : %" PRIxPTR "\n", uptr);

}

Possible output:可能的 output:

0x7ffcaceba19c
0x7ffcaceba19c
A signed integer representation of the same pointer in decimal        : 140723209609628
An unsigned integer representation of the same pointer in decimal     : 140723209609628
An unsigned integer representation of the same pointer in hexadecimal : 7ffcaceba19c

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么这个变量位于同一个地址 - Why this variable lies on same address 静态变量的地址相同,但局部变量的地址不同 - Same address for static variable but different for local variable 为什么和如何返回静态变量的地址可以,而对非静态变量执行相同的操作却得到错误的结果? - Why and how returning the address of a static variable is OK while doing the same with non-static variable gives wrong result? 为什么编译器重新定义变量后,将不同的 memory 地址分配给同一个变量? - Why did the compiler assign different memory address to the same variable, after re-defining the variable? 为什么localtime()在同一台计算机上给出不同的时区? - Why localtime() gives different timezone on the same machine? 为什么虚拟内存地址在不同的进程中是相同的? - Why Virtual Memory Address is the same in different process? 为什么同一地址返回不同的值? - Why does the same address return different values? 为什么变量在递归调用中获得相同的地址? - Why the variable getting same address in recursive calls? 为什么数组变量的值和地址处的值相同? - Why is the value and value at address of a array variable same? 为什么两个变量共享相同的地址? - Why do two variable share the same address?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM