分配/铸造整数指针 [英] Assigning/Casting integers to pointers

查看:137
本文介绍了分配/铸造整数指针的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#include<stdio.h>

#define int int*

main(){
    int *p,q;
    p=(int *)5;
    q=10;
    printf("%d",q+p);
}

我的问题是,在行 P =(INT *)5 Q = 10 如何将其工作正是内部
因为 P 都是指针类型,怎么可能,我们可以指定一个整型值指针变量?还有一件事是如何铸造型 p =(INT *)5 在这里工作?

my question is that in line p=(int *)5,and q=10 how its working internally exactly because p and q are both pointer types, how is it possible that we can assign an integer value to pointer variable q?.One more thing how this type casting p=(int*)5 working here?

通过使用这个公式,我们可以回答

By using this formula we can answer

新地址=旧地址+电话号码* sizeof的数据类型到指针指向

new address = old address+number*sizeof data type to which pointer is pointing

推荐答案

的#define INT INT * 将取代为int * P,Q 为int * * p,q 。所以,在这里, P 是双指针 INT 是类型 INT

#define int int* will replace int *p, q as int* *p, q. So Here p is double pointer to int and q is of type int.

例如考虑你的逻辑同样的字符以下程序

For example consider the below program of your same logic in char

#include<stdio.h>  
#define char char*  
main()
{     
    char *p,q;     
    printf("%d, %d\n", sizeof(p), sizeof(q));
} 

输出为

4, 1

P =(INT *)5; - 这个声明也将被替换,比如 P =(INT *。*)5; 由preporcessor。因此,它不扔任何警告。

p=(int *)5; - This statement also will be replaced like p=(int* *)5; by preporcessor. So its not throwing any warning.

所以现在的printf(%d个,Q + P); 的意志带给您中的情况下,45 32位机或 85 柜面的64位机。

so now printf("%d",q+p); will gives you 45 in case of 32 bit machine or 85 incase of 64 bit machine.

这篇关于分配/铸造整数指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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