简体   繁体   English

(int *)p和&p之间的区别

[英]Difference between (int *)p and &p

what is the difference between the statements int *p = (int*) i; and int *q=&i; int *p = (int*) i; and int *q=&i;语句之间有什么区别int *p = (int*) i; and int *q=&i; int *p = (int*) i; and int *q=&i; Here is whole program 这是整个程序

#include<stdio.h>
int main(){
     int i;
     int *p = (int*) i; 
     printf("\n p is %d ",p);
     int *q = &i;
     printf("\n q is %d ",q);
     return 0;
 }

the output obtained is p is 22092 q is 1002476148 i think here both p and q are storing the address of i , but they are showing different values, can somebody explain why it is happenning? 得到的输出是p is 22092 q is 1002476148我想这里p和q都存储着i的地址,但是它们显示的值不同,有人可以解释为什么会这样吗?

First: pointers must be cast to (void*) and printed with %p . 首先:指针必须强制转换为(void*)并使用%p打印。 %d prints an int in base 10. That is, %d以10为基数打印一个int

#include <stdio.h>

int main(){
    int i;
    int *p = (int*) i; 
    printf("\n p is %p ", (void*)p);
    int *q = &i;
    printf("\n q is %p ", (void*)q);
}

Now let's try to compile the program with these changes: only 2 errors: 现在,让我们尝试通过以下更改来编译程序:仅2个错误:

% gcc ptr.c -Wall -Wextra
ptr.c: In function ‘main’:
ptr.c:5:14: warning: cast to pointer from integer of different size [-Wint-to-pointer-cast]
     int *p = (int*) i;
              ^
ptr.c:5:14: warning: ‘i’ is used uninitialized in this function [-Wuninitialized]
     int *p = (int*) i;
              ^~~~~~~~

Both pertanining to the int *p = (int*) i; 都与int *p = (int*) i; ; ; the second says that the value of i is used but we didn't set any value to i (this causes undefined behaviour ) and the other that we're trying to convert an integer to a pointer, and the integer doesn't have the same amount of bits as a pointer has on this platform. 第二个表示使用了i的值,但是我们没有将任何值设置为i (这会导致未定义的行为 ),另一个表示我们正在尝试将整数转换为指针,并且该整数没有与该平台上的指针具有相同数量的位。

Ie

int *q = &i;

initializes pointer to int q with the address of variable i , whereas 使用变量i的地址初始化指向int q指针,而

int *p = (int*) i; 

interprets the garbage value contained in i , in an implementation-defined manner, as an address, and initializes p with that. 以实现定义的方式将i中包含的垃圾值解释为地址,并以此初始化p

Not quite equal. 不完全相等。

Here 这里

int *p = (int*) i; /* i is not initialized */

pointer p gets assigned with value of i which is some garbage data & tries to cast some junk data to int* type and assigned to p . 指针p被分配了i值,它是一些垃圾数据,并尝试将一些垃圾数据转换为int*类型,并分配给p If you tries to dereference p it gives segmentation fault & causes undefined behavior . 如果尝试取消引用p则会出现分段错误并导致未定义的行为

And Here 和这里

int *q = &i;

pointer q is assigned with valid address. 指针q被分配了有效地址。

Also while printing pointer variable use %p instead of %d format specifier for eg 另外,在打印指针变量时,请使用%p代替%d格式说明符,例如

 printf("\n q is %p ",(void*)q);

About assigning integer values to a pointer like int *p = (int*) i , C standard says 关于将整数值分配给类似int *p = (int*) i指针C标准表示

6.3.2.3 Pointers 6.3.2.3指针

(5) An integer may be converted to any pointer type . (5) 整数可以转换为任何指针类型 Except as previously specified, the result is implementation-defined , might not be correctly aligned, might not point to an entity of the referenced type, and might be a trap representation. 除非先前指定,否则结果是实现定义的 ,可能未正确对齐,可能未指向引用类型的实体,并且可能是陷阱表示。

According to the C standard ISO/IEC 9899:2011, subclause 6.3.2.3: 根据C标准ISO / IEC 9899:2011,第6.3.2.3节:

An integer may be converted to any pointer type. 整数可以转换为任何指针类型。 Except as previously specified, the result is implementation-defined, might not be correctly aligned, might not point to an entity of the referenced type, and might be a trap representation. 除非先前指定,否则结果是实现定义的,可能未正确对齐,可能未指向引用类型的实体,并且可能是陷阱表示。

It follows that when you cast i to pointer-to-int type, what it holds is generally undefined. 因此,当您将ipointer-to-int类型时,其内容通常是不确定的。

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

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