在C ++中,必须提到指针的[[type”。例如,
int a = 5;
int * p = &a;
这里,'int *'中的'int'是必需的。我发现的原因是需要解引用。(知道要读取的字节数及其解释)和指针算术。但是,如果我写:-
int a = 5; char * p = &a; float * p2 = &a; //etc
显示错误,表示不能将char *,float *等指针分配给int变量。好了..,所以编译器是我想要为其提供指针的变量的类型。这不同于普通变量,在普通变量中,编译器不确切知道我想要哪种类型。例如:-Knew
int a = 5;
float b = 5;
double c = 5;
long d = 5;
char e = 5;
所有这些都很好。我需要在此处编写类型,因为编译器将不知道我想要哪种类型,因为所有这些都可以使用。但是,对于指针,它知道要为其声明指针的变量的确切类型。
即使我使用显式的类型转换,我仍会以一种方式告诉我所需的指针类型,并且仍然必须指定正确的指针类型。
int a = 5;
char * b = (char*)&a;//This would work.
//However this,
int * b = (char*)&a;
//wouldn't, as even with typecasting, c++ knows exactly the type of pointer we need.
在C ++中,必须提到指针的类型。例如,int a = 5; int * p =&a;这里,“ int *”中的“ int”是必需的。我发现的原因是它需要取消引用。(...