简体   繁体   English

强制转换为int,无符号int用于c中的指针

[英]cast of int,unsigned int for pointers in c

I am new to pointers in C. Please help me with following code: 我是C语言中的新手。请通过以下代码帮助我:

void main()
{
    int i=3;
    printf("unsighed Address of i = %u \n",(unsigned int)&i);
    printf("int Address of i = %d\n",(int)&i);
    printf("pointer Address of i = %p\n",&i);
    printf("Value of i = %d\n",i);
}

Output: 输出:

unsighed Address of i = 3219915640 
int Address of i = -1075051656
pointer Address of i = 0xbfec0378
Value of i = 3

As you can see, I am just checking address of i through int , unsigned int , and pointer. 如您所见,我只是通过intunsigned int和指针检查i地址。 Unsigned and pointer values are same (0xbfec0378 of base16 = 3219915640 of base10). 无符号和指针值相同(base16的0xbfec0378 = base10的3219915640)。 But integer value is different. 但是整数值是不同的。 int range is (-32768 to 32767). int范围是(-32768至32767)。 So I don't know how I got -1075051656. 所以我不知道怎么得到-1075051656。 Can you explain this. 你能解释一下吗?

Where do you base int range is (-32768 to 32767) ? 您在哪里将int range is (-32768 to 32767) That implies that the size is 2 bytes. 这意味着大小为2个字节。 I don't think that is the case, since -1075051656 is an int (you converted the address to int ). 我认为情况并非如此,因为-1075051656int (您将地址转换为int )。

To check the size of an int use sizeof(int) , or to find the interval of values it represents, use INT_MIN, INT_MAX constants. 要检查int的大小,请使用sizeof(int) ,或查找它表示的值的间隔,请使用INT_MIN, INT_MAX常量。

The size of int in your system is probably 4 bytes (32 bits), with interval -2147483648 to 2147483647 . 系统中int的大小可能是4字节(32位),间隔为-21474836482147483647 With unsigned int it is from 0 to 4,294,967,295 . 使用unsigned int它是从0 to 4,294,967,295

Therefore you can print the value using unsigned int , but it overflows when converting to int since 4294967295 > 3219915640 > 2147483647 因此,您可以使用unsigned int打印该值,但是由于4294967295 > 3219915640 > 2147483647转换为int时,它将溢出。

If you want to cast a pointer value to an integer type, you should use intptr_t or uintptr_t , which warrantee that the integer is big enough to hold any possible pointer value. 如果要将指针值转换为整数类型,则应使用intptr_tuintptr_t ,这保证了整数足够大以容纳任何可能的指针值。

You may be compiling your code for a 32bit architecture, or you may be lucky enough so that your stack is located in the lower area of the address space (although this is unlikely), so that in your execution the most significant bits of the address are 0. 您可能正在为32位体系结构编译代码,或者可能很幸运,因此您的堆栈位于地址空间的下部区域(尽管这不太可能),以便在执行时,地址的最高有效位是0。

See https://en.cppreference.com/w/c/types/integer 参见https://en.cppreference.com/w/c/types/integer

Code ( https://ideone.com/2NDYnP ): 代码( https://ideone.com/2NDYnP ):

#include <stdio.h>    // printf
#include <stdint.h>   // intptr_t, uintptr_t
#include <inttypes.h> // PRIdPTR, PRIuPTR

int main() {
    int i=3;
    // Incorrect
    printf("unsighed value of &i = %u \n",(unsigned int)&i);
    printf("int value of &i = %d\n",(int)&i);

    // Correct
    printf("pointer Address of i = %p\n",&i);
    printf("uintptr_t value of &i = %"PRIuPTR"\n", (uintptr_t)&i);
    printf("intptr_t value of &i = %"PRIdPTR"\n", (intptr_t)&i);

    printf("Value of i = %d\n",i);

    return 0;
}

Result: 结果:

unsighed value of &i = 3131233804 
int value of &i = -1163733492
pointer Address of i = 0x7fffbaa2d60c
uintptr_t value of &i = 140736324621836
intptr_t value of &i = 140736324621836
Value of i = 3

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM