当我们谈论解除引用时,是否有必要使用*
呢?如果我们访问指针所指的一些其他的方式,可以将其视为解引用指针或没有,如:为什么printf(“%s”,ptr)能够解引用void *?
char *ptr = "abc" ;
printf("%c" , *ptr); // Here pointer is dereferenced.
printf("%s" , ptr); // What about this one?
这是我的问题的第一部分。
现在如果printf("%s" , ptr)
是解引用的示例,那么也请回答我的问题的以下部分。
ķ& [R说
a "pointer to void" is used to hold any type of pointer but cannot be dereferenced itself
因此,
char a = 'c' ;
char *p = &a ;
void *k = &a;
printf("\n%c\n" , *p);
printf("\n%c\n" , *k);
不能编译,编译器给出错误
In function ‘main’: warning: dereferencing ‘void *’ pointer error: invalid use of void expression
但是如果我们用
char *a = "c" ;
char *p = a ;
void *k = a;
printf("\n%c\n" , *p);
printf("\n%s\n" , k);
它编译和工作。这意味着无效指针可以被解除引用 - 我们已经得到了对象指针指向的内容。
如果是这种情况,那么上面提到的K & R是什么意思?
谢谢你的时间。
我没有看到你在哪里解除引用'void *'成功。请重新阅读您的代码。 – leppie 2010-07-28 10:53:27