[英]Regarding pointers of c programing
Please, if anyone know what's wrong in this tell me请,如果有人知道这有什么问题,请告诉我
#include"stdio.h"
void main() {
int a = 10;
int* p = &a;
void** q = &p;
printf("memory address=%d\n",(int*)*q);
}
Here is the compiler error:这是编译器错误:
warning: initialization from incompatible pointer type[-Wincompatible- pointer-types] void **q=&p;
警告:从不兼容的指针类型初始化[-Wincompatible-pointer-types] void **q=&p;
ptr3.c:7:25: warning: format '%d' expects argument of type 'int', but argument 2 has type 'int ' [-Wformat=] printf("memory address=%d\\n",(int )*q);
ptr3.c:7:25: 警告:格式 '%d' 需要类型为 'int' 的参数,但参数 2 的类型为 'int ' [-Wformat=] printf("memory address=%d\\n",(int )*q); ~^ ~~~~~~~~ %ls
~^ ~~~~~~~ %ls
Is compiler wants to say that I cannot point a int
type pointer using void
pointer?编译器是否想说我不能使用
void
指针指向int
类型指针?
Is compiler wants to say that I cannot point a int type pointer using void pointer?
编译器是否想说我不能使用 void 指针指向 int 类型指针?
void ** q
is type void **
while int * p
is type int *
. void ** q
是void **
类型,而int * p
是int *
类型。 Hence this line: void ** q = &p;
因此这一行:
void ** q = &p;
warrants a compiler warning as the types are not compatible.保证编译器警告,因为类型不兼容。
If you typecast &p
to a void *
then the warning in your question will go away: void ** q = (void *)&p;
如果您将
&p
类型转换为void *
那么您问题中的警告将消失: void ** q = (void *)&p;
This line: printf("memory address=%d\\n",(int*)*q);
这一行:
printf("memory address=%d\\n",(int*)*q);
should also cause a compiler warning because (int *)*q
is type int *
but the %d
format specifier is expecting an int
.还应该导致编译器警告,因为
(int *)*q
是int *
类型,但%d
格式说明符需要一个int
。 If you use the format specifier %p
then this warning will go away as well.如果您使用格式说明符
%p
那么这个警告也会消失。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.