當我們談論解除引用時,是否有必要使用*
呢?如果我們訪問指針所指的一些其他的方式,可以將其視爲解引用指針或沒有,如:爲什麼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