void *指针转换问题。 [英] void * pointer convert problem.

查看:197
本文介绍了void *指针转换问题。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我有以下代码,想做指针转换。它总是抱怨:


vcnvt.c:在函数`main''中:

vcnvt.c:20:警告:取消引用`void *''指针

vcnvt.c:20:在非结构或联合的东西中请求成员`key''

-------------- -------------

#include< stdio.h>

#include< stdlib.h>


typedef struct

{

u_long key;

u_long transCmdAdd;

u_long transCmdMod ;

u_long transCmdSub;

u_long transCount;

} counting_bin_t,* counting_bin_p;


int main()

{

void * vbPtr;

counting_bin_t cBin [9];

cBin [0] .key = 9;


vbPtr = cBin;

printf(" key is%d \ n",vbPtr-> key); < br $>
}


谢谢,

Eric

解决方案

< blockquote> Eric J.Hu< eh*@lucent.com>写道:

vcnvt.c:在函数`main'':
vcnvt.c:20:警告:取消引用`void *''指针


相当不言自明。你无法拒绝无效的指针。你可以,但是,b $ b,然后将它们转回真实类型然后取消引用它们。

int main()
{
void * vbPtr;
counting_bin_t cBin [9];
cBin [0] .key = 9;
vbPtr = cBin;
printf(" key is%d \ n",vbPtr-> key);


/ *喜欢这样* /


printf(" key is%d \ n",((counting_bin_t *)vbPtr) - > key);

}




-

Christopher Benson-Manica |我*应该*知道我在说什么 - 如果我

ataru(at)cyberspace.org |不,我需要知道。火焰欢迎。




Eric J.Hu写道:



我有以下代码,想做指针转换。它总是抱怨:

vcnvt.c:在函数`main'':
vcnvt.c:20:警告:解除引用`void *''指针
vcnvt.c: 20:在非结构或联合的东西中请求成员密钥
---------------------------
#include< stdio.h>
#include< stdlib.h>

typedef struct
{
u_long key;
u_long transCmdAdd;
u_long transCmdMod;
u_long transCmdSub;
u_long transCount;
} counting_bin_t,* counting_bin_p;

int main()


使用int main(无效)

{
void * vbPtr;
counting_bin_t cBin [9];
cBin [0] .key = 9 ;

vbPtr = cBin;
你告诉vbPtr指向cBin

printf(" key is%d \ n",vbPtr-> key);



vbPtr是一个void指针,所以它不知道谁是键。你这个事实是:
做:vbPtr = cBin并没有将vbPtr的类型从void *更改为

counting_bin_t *。您可以投射(counting_bin_t *)vbPtr以获得您需要的

。即使有演员,你应该知道

(counting_bin_t *)vbPtr->键可能与

((counting_bin_t *)vbPtr) - >相同。关键。


Eric J.Hu写道:



我有以下代码,想做指针转换。它总是抱怨:


英语可能不是你的第一语言,但这对我来说太差了。

知道你真正想做什么。 />
vcnvt.c:在函数`main'':
vcnvt.c:20:警告:取消引用`void *''指针


那'很简单。您不能在void *

指针上使用*运算符。原因是,void *是一个通用的指针类型,那么

编译器如何知道你希望获得什么样的值呢?是吗?
指向一个int,一个char还是什么?


注意:

x-> y表示与(* x)相同.y

即,它意味着取消引用x然后访问其中的字段y。

vcnvt.c:20:请求成员`key' '不是结构或联合的东西


那也很简单,取消引用void *指针不会给你

带有结构类型的东西(因为它无效),所以很明显

试图访问其中一个名为key的字段是不可能的。

------------ ---------------
#include< stdio.h>
#include< stdlib.h>


您的示例不使用stdlib.h中的任何内容,为什么要包含它?

typedef struct
{
u_long key; < br_> u_long transCmdAdd;
u_long transCmdMod;
u_long transCmdSub;
u_long transCount;
} counting_bin_t,* counting_bin_p;


很多人认为最好不要隐藏指针或结构

在typedef后面。

int main()


如果你没有使用参数,最好明确表示习惯为



int main(void)

{
void * vbPtr;
counting_bin_t cBin [9];
cBin [0] .key = 9;

vbPtr = cBin;


这是完全有效的,但在你的例子中它毫无意义,所以我不能确定你想要了解的内容。

printf(" key is%d \ n",vbPtr-> key);


上面这行有问题,因为你试图将
取消引用一个void *指针(这是非法的)然后访问一个字段
结果值中的
(这显然是不可能的)。


void *指针的重点是它们可以指向任何东西。


Main返回一个int,实际执行此操作不是更好吗? (C99

允许主要结束的下降表现为好像返回0,但大多数

编译器不是C99编译器)。


返回0;

}



-

Flash Gordon

住在有趣的时间。

虽然我的电子邮件地址说垃圾邮件,但这是真的,我读了它。


Hi,

I have following code, want do pointer convert. It always complain:

vcnvt.c: In function `main'':
vcnvt.c:20: warning: dereferencing `void *'' pointer
vcnvt.c:20: request for member `key'' in something not a structure or union
---------------------------
#include <stdio.h>
#include <stdlib.h>

typedef struct
{
u_long key;
u_long transCmdAdd;
u_long transCmdMod;
u_long transCmdSub;
u_long transCount;
} counting_bin_t, *counting_bin_p;

int main()
{
void * vbPtr;
counting_bin_t cBin[9];
cBin[0].key = 9;

vbPtr = cBin;
printf("key is %d\n", vbPtr->key);
}

Thanks,
Eric

解决方案

Eric J.Hu <eh*@lucent.com> wrote:

vcnvt.c: In function `main'':
vcnvt.c:20: warning: dereferencing `void *'' pointer
Pretty self-explanatory. You can''t derefence void pointers. You can,
however, cast them back to their real type and then dereference them.
int main()
{
void * vbPtr;
counting_bin_t cBin[9];
cBin[0].key = 9; vbPtr = cBin;
printf("key is %d\n", vbPtr->key);
/* Like so */

printf( "key is %d\n", ((counting_bin_t*)vbPtr)->key );
}



--
Christopher Benson-Manica | I *should* know what I''m talking about - if I
ataru(at)cyberspace.org | don''t, I need to know. Flames welcome.



Eric J.Hu wrote:

Hi,

I have following code, want do pointer convert. It always complain:

vcnvt.c: In function `main'':
vcnvt.c:20: warning: dereferencing `void *'' pointer
vcnvt.c:20: request for member `key'' in something not a structure or union
---------------------------
#include <stdio.h>
#include <stdlib.h>

typedef struct
{
u_long key;
u_long transCmdAdd;
u_long transCmdMod;
u_long transCmdSub;
u_long transCount;
} counting_bin_t, *counting_bin_p;

int main()
use int main(void)
{
void * vbPtr;
counting_bin_t cBin[9];
cBin[0].key = 9;

vbPtr = cBin; you tell vbPtr to point to cBin
printf("key is %d\n", vbPtr->key);


vbPtr is a void pointer so it has no idea who key is. The fact that you
do: vbPtr=cBin doesn''t change the type of vbPtr from void * to
counting_bin_t *. You could cast (counting_bin_t *)vbPtr to obtain what
you need. Even with the cast you should be aware that
(counting_bin_t*)vbPtr->key may not be the same as
((counting_bin_t*)vbPtr)->key.


Eric J.Hu wrote:

Hi,

I have following code, want do pointer convert. It always complain:
English may not be your first language, but that is too poor for me to
know what you actualy want to do.
vcnvt.c: In function `main'':
vcnvt.c:20: warning: dereferencing `void *'' pointer
That''s simple. You are not allowed to use the * operator on a void*
pointer. The reason being, void* is a generic pointer type so how is the
compiler meant to know what sort of value you expect to get? Does it
point to an int, a char or what?

Note that:
x->y means the same as (*x).y
I.e., it means dereference x and then access field y within it.
vcnvt.c:20: request for member `key'' in something not a structure or union
That''s simple as well, dereferencing a void* pointer does not give you
something with a struture type (since it is invalid), so obviously
trying to access a field named key within it is not possible.
---------------------------
#include <stdio.h>
#include <stdlib.h>
Your example does not use anything from stdlib.h so why include it?
typedef struct
{
u_long key;
u_long transCmdAdd;
u_long transCmdMod;
u_long transCmdSub;
u_long transCount;
} counting_bin_t, *counting_bin_p;
Many people think it is better not to hide either pointers or structs
behind typedefs.
int main()
If you are not using parameters, it is better to be explicit as a matter
of habit.

int main(void)
{
void * vbPtr;
counting_bin_t cBin[9];
cBin[0].key = 9;

vbPtr = cBin;
This is perfectly valid, but in your example it is pointless so I can''t
really see what you want to know about.
printf("key is %d\n", vbPtr->key);
The above line is the one with the problem, since you are trying to
dereference a void* pointer (which is illegal) and then access a field
within the resultant value (which is again obviously impossible).

The whole point of void* pointers is they can point to anything.

Main returns an int, wouldn''t it be better to actually do this? (C99
allows falling of the end of main to act as if 0 was returned, but most
compilers are not C99 compilers).

return 0;
}


--
Flash Gordon
Living in interesting times.
Although my email address says spam, it is real and I read it.


这篇关于void *指针转换问题。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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