什么是浮动指针和INT指针地址之间的区别? [英] What is the difference between float pointer and int pointer address?

查看:148
本文介绍了什么是浮动指针和INT指针地址之间的区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图运行这个code,

  INT * P;
浮q;
Q = 6.6;
P =&放大器;问;

虽然这将是一个警告,但我认为&放大器,Q P 是相同的大小,所以 p 可以有的地址。但是,当我打印&放大器,Q P 我得到不同的结果。
这是我的输出

  * P = 6.600000
 Q = 0.000000,P = 0x40d33333,&安培; Q = 0x7fffe2fa3c8c

什么是我失踪?
P &放大器;:Q 是一样的当两个指针变量类型相同。

我的完整code是

 #包括LT&;&stdio.h中GT;
无效的主要()
{
    INT * P;
    浮q;
    Q = 6.6;
    P =&放大器;问;
    的printf(* p值=%F \\ N,Q =%F,P =%P,放大器; Q =%P \\ n,* P,Q,P,放大器; Q);
}


解决方案

您需要采取的编译器警告更为严重。

C不要求编译器的拒绝的无效程序,它仅仅要求对违反规则的诊断。诊断可以是一个致命的错误消息或警告。

不幸的是,这是常见的编译器发出警告不兼容的指针类型的任务。

 无效的主要()

这是错误的;它应该是 INT主要(无效)。你的编译器可以让你摆脱它,它可能不会造成任何明显的问题,但有一个在没有正确写它没有任何意义。 (这不是的非常的那么简单,但是这是足够接近。)

  INT * P;
浮q;
Q = 6.6;

这是确定。

  P =&放大器;问;

P 的类型为为int * ; &放大器,Q 的类型为浮法* 。分配一个其他(没有投)是的约束违反的。看它最简单的方法是,它只是违法的。

如果你真的想这样做任务,你可以使用一个转换:

  P =(INT *)及;问; / *合法的,但丑* /

但有很少一个很好的理由这样做。 P 是一个指向 INT ;除非你有一个的非常的很好的理由,使其指向别的东西应该指向一个 INT 对象。在某些情况下,转换本身也有不确定的操作。

 的printf(* P =%F \\ NQ =%F,P =%P,放大器; Q =%P \\ N,* P,Q,P,放大器; q);

%F 格式要求双击的参数(浮动参数在这方面提升到双击所以浮动将是确定的)。但 * P 的类型为 INT 。调用的printf 与错误类型的参数会导致不确定的程序的行为。

%P 需要类型的参数无效* ,不是任何指针类型。如果您想打印一个指针的值,你应该把它转换为无效*

 的printf(&安培; Q =%P \\ N(无效*)及Q);

很可能没有投工作,但同样,该行为是不确定的。

如果您在编译程序得到任何警告,甚至不打扰运行它。首先修复警告。

至于在您的标题问题,类型的指针为int * 浮法* 是不同类型的。一个为int * 应指向 INT 对象;一个浮法* 应指向浮动对象。你的编译器可以让你混合使用它们,但这样做的结果是既实现定义或定义。 C语言,特别是许多C编译器,会让你得逞的很多东西没有多大意义。

这他们不同类型的原因是为了(试图)prevent,或至少检测,错误在它们的用途。如果你声明类型的对象为int * ,你说,你打算为它指向一个 INT 对象(如果它不是一个空指针)。在你的存储浮动对象的地址为int * 对象几乎可以肯定是一个错误。强制类型安全允许尽可能早地发现这样的错误(当你的编译器输出警告,而不是当一个重要的客户演示时你的程序崩溃)。

很可能(但不能保证)的为int * 浮法* 的大小相同,并且具有相同内部重组presentation。但是的意味着一个为int * 对象的不是包含一个虚拟地址32(或64)位的集合,而是事指向一个 INT 对象。

I tried to run this code,

int *p;
float q;
q = 6.6;
p = &q;

Though it will be a warning, but i think &q and p are of same size, so p can have an address of q. But when I print &q and p I am getting different output. This is my output

*p =  6.600000 
 q = 0.000000, p = 0x40d33333, &q = 0x7fffe2fa3c8c 

What is that I am missing? And p and &q is same when both pointer and variable type is same.

My complete code is

#include<stdio.h>
void main()
{   
    int *p;
    float q;
    q = 6.6;
    p = &q;
    printf("*p =  %f \n q = %f, p = %p, &q = %p \n",*p,q,p,&q);
}

解决方案

You need to take compiler warnings more seriously.

C doesn't require compilers to reject invalid programs, it merely requires "diagnostics" for rule violations. A diagnostic can be either a fatal error message or a warning.

Unfortunately, it's common for compilers to issue warnings for assignments of incompatible pointer types.

void main()

This is wrong; it should be int main(void). Your compiler may let you get away with it, and it may not cause any visible problems, but there's no point in not writing it correctly. (It's not quite that simple, but that's close enough.)

int *p;
float q;
q = 6.6;

That's ok.

p = &q;

p is of type int*; &q is of type float*. Assigning one to the other (without a cast) is a constraint violation. The simplest way to look at it is that it's simply illegal.

If you really want to do this assignment, you can use a cast:

p = (int*)&q; /* legal, but ugly */

but there's rarely a good reason to do so. p is a pointer to int; it should point to an int object unless you have a very good reason to make it point to something else. In some circumstances, the conversion itself can have undefined behavior.

printf("*p =  %f \n q = %f, p = %p, &q = %p \n",*p,q,p,&q);

The %f format requires a double argument (a float argument is promoted to double in this context so float would be ok). But *p is of type int. Calling printf with an argument of the wrong type causes your program's behavior to be undefined.

%p requires an argument of type void*, not just of any pointer type. If you want to print a pointer value, you should cast it to void*:

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

It's likely to work without the cast, but again, the behavior is undefined.

If you get any warnings when you compile a program, don't even bother running it. Fix the warnings first.

As for the question in your title, pointers of type int* and float* are of different types. An int* should point to an int object; a float* should point to a float object. Your compiler may let you mix them, but the result of doing so is either implementation-defined or undefined. The C language, and particularly many C compilers, will let you get away with a lot of things that don't make much sense.

The reason that they're distinct types is to (try to) prevent, or at least detect, errors in their use. If you declare an object of type int*, you're saying that you intend for it to point to an int object (if it's not a null pointer). Storing the address of a float object in your int* object is almost certainly a mistake. Enforcing type safety allows such mistakes to be detected as early as possible (when your compiler prints a warning rather than when your program crashes during a demo for an important client).

It's likely (but not guaranteed) that int* and float* are the same size and have the same internal representation. But the meaning of an int* object is not "a collection of 32 (or 64) bits containing a virtual address", but "something that points to an int object".

这篇关于什么是浮动指针和INT指针地址之间的区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆