为什么我们制作一个char,double和float的指针。 ?如果指针持有地址..无关紧要它是浮点数还是字符? [英] why we made a pointer of char , double and float. ? if the pointer holds the adress.. doesnt matter it is float or char ?
本文介绍了为什么我们制作一个char,double和float的指针。 ?如果指针持有地址..无关紧要它是浮点数还是字符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
解决方案
因为指针算法很重要(例如,参见C ++指针算术 [ ^ ])。尝试:
#include < stdio.h >
int main()
{
double d = < span class =code-digit> 5 。 0 ;
double * pd;
char * pc;
pd =& d;
pc =( char *)& d;
printf( pd =%p,pc =%p \ n,pd,pc); // 同一地址,此处
++ pd;
++ pc;
printf( pd =%p,pc =%p \ n,pd ,pc); // OOOPS!
返回 0 ;
}
每当你引用一个内存地址时,你必须决定如何解释你在那里找到的数据,这就是指针的含义 - 输入类型,并输入...
(有些语言没有定义类型,环境将决定如何解释该类型 - 偶尔出现错误)
你应该在这里阅读:指针 - C ++教程 [ ^ ]
clear me plz. bcoz if pointer holds an adress so doesnot matter it is the adress of char , int or float.
解决方案
It matters because of pointer arithmetic (see, for instance "C++ pointer arithmetic"[^]). Try:
#include <stdio.h> int main() { double d = 5.0; double * pd; char * pc; pd = &d; pc = (char *) &d; printf("pd = %p, pc = %p\n", pd, pc); // same address, here ++pd; ++pc; printf("pd = %p, pc = %p\n", pd, pc); // OOOPS! return 0; }
Whenever you reference a memory address, you have to decide how to interpret the data you find there, that's the meaning of the pointer-type, and type at all...
(There are languages where you do not define type and the environment will decide for you how to interpret that type - with occasional errors)
You should read here: Pointers - C++ Tutorials[^]
这篇关于为什么我们制作一个char,double和float的指针。 ?如果指针持有地址..无关紧要它是浮点数还是字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文