任何类型的指针都可以指向任何东西? [英] Any type of pointer can point to anything?

查看:101
本文介绍了任何类型的指针都可以指向任何东西?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这句话是否正确?指针的任何TYPE都可以指向任何其他类型吗?

因为我相信,仍然有疑问。

那么为什么指定的指针是明确的类型?例如。 int或char的?

我可以得到的一个解释是因为如果一个int类型指针指向char数组,那么当指针递增时,指针将从0位置直接跳转到2位置,跳过它们之间的1个位置(因为int size = 2)。

并且可能因为指针只保存值的地址,而不是值本身,例如。 int或double。



那我错了什么?这个陈述是否正确?正如我在某人对stackoverflow问题的回答中所读到的那样?

Is this statement correct? Can any "TYPE" of pointer can point to any other type?
Because i believe so , still have doubts.
So why are then pointers declared for definite types? eg. int or char ones?
The one explanation i could get was because if an int type pointer was being pointed to char array, then when pointer will be incremented , the pointer will jump from 0 position to directly 2 position , skipping 1 position in between( because int size=2).
And maybe because pointer just holds the address of a value , not the value itself eg. the int or double one.

So at what i am wrong? Was that statement correct? As i read that at someone's answer to stackoverflow question?

推荐答案

是和否。所有指针都是相同的 - 它们在内存中包含一个地址。这就是为什么在C和C ++中合法地将任何指针(无论类型)转换为 void *



但是当你工作时有了指针,它们的类型确实有所作为。迭代数组中的元素时(为了使其工作,编译器必须知道元素的大小,以及我们告诉编译器的方式是通过指针的类型),并且当解除引用指向它指向的数据。这就是为什么不同类型之间的演员阵容很危险。



Yes and no. All pointers are the same - they contain an address in memory. That is why it is legal in C and C++ to convert any pointer, regardless of type, to a void*

But when you work with pointers, their type does make a difference. Both when iterating over elements in an array (for this to work, the size of the elements must be known to the compiler, and the way we tell the compiler is through the type of the pointer), and when dereferencing a pointer to get to the data it points to. That is why casts between different types is dangerous.

<br />
int a = 1234; // a value on the stack<br />
int* b = &a;  // points to a on the stack<br />
char* c = (char*)b; // the same address,<br />
                    // but the contents <br />
                    // will be interpreted<br />
                    // differently<br />
char d = *c; // What does d contain?<br />
long* e = (long*)b; // again<br />
long f = *e; // What does f contain?<br />
             // (This reads more than was <br />
             // originally put on the stack<br />
             // so will lead to undefined<br />
             // behaviour.)<br />


所有指针都包含一个(可能无效的)地址。但是 C ++ 编程语言是强类型的:编译器检测到指针类型不匹配。

和,是的,指针算术依靠尖头大小。



请参阅此页: cplusplus.com上的指针 [ ^ ],您也可以了解 void 指针的特殊规则。
All pointers hold a (possibly invalid) address. However the C++ programming language is strongly typed: the compiler detects pointer type mismatches.
And, yes, pointer arithmetic relies on pointed type size.

Please see this page: "Pointers" at cplusplus.com[^], you may learn also about the special rule of the void pointer.


#include <stdio.h>
#include<string.h>

char f[10000];
char factorial[1010][10000];

void multiply(int k)
{
int cin,sum,i;
int len = strlen(f);
cin=0;
i=0;
while(i<len)
{
sum=cin+(f[i] - '0') * k;
f[i] = (sum % 10) + '0';
i++;
cin = sum/10;
}
while(cin>0)
{
f[i++] = (cin%10) + '0';
cin/=10;
}
f[i]='\0';
for(int j=0;j<i;j++)
factorial[k][j]=f[j];

factorial[k][i]='\0';
}
void fac()
{
int k;
strcpy(f,"1");
for(k=2;k<=1000;k++)
multiply(k);
}
void print(int n)
{
int i;
int len = strlen(factorial[n]);
printf("%d!\n",n);
for(i=len-1;i>=0;i--)
printf("%c",factorial[n][i]);
printf("\n");
}
int main()
{
int n;
factorial[0][0]='1';
factorial[1][0]='1';
fac();
while(scanf("%d",&n)==1){
print(n);
}
return 0;
}


这篇关于任何类型的指针都可以指向任何东西?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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