简体   繁体   English

c,将void *指向的值赋给void * array

[英]c, assigning value pointed to by void * to void * array

I am trying to assign a value pointed at by a void * in a void * array. 我试图在分配一个值指出void *一个void *阵列。 Here is what I have so far: 这是我到目前为止:

23 void queue_enqueue(void *q, void *item) {
24 int len = 0 ;
25 
26 len = sizeof(q) ;
27 q[len-1] = 
28     item ;
29     
30 return ;
31 
32 }

With this compiler error: 有了这个编译错误:

myqueue.c:27: warning: dereferencing ‘void *’ pointer
myqueue.c:28: error: invalid use of void expression

I've been searching for an answer for about a hour now, but I haven't seen an implementation similar to what I have been given here. 我一直在寻找一个小时的答案,但我没有看到类似于我在这里给出的实现。 In case you were wondering, yes this is homework and the prototype was given by the instructor. 如果你想知道,是的,这是家庭作业,原型是由教练给出的。

Because they are passed as void * I have been unable to use the gcc typeof operator. 因为它们被传递为void *我无法使用gcc typeof运算符。

I'm okay with the warning. 我对这个警告没问题。 I know it's just gcc telling me that "be careful we're not going to check this for you", but I can't get rid of the error on line 28. 我知道这只是gcc告诉我“小心我们不会为你检查这个”,但我无法摆脱第28行的错误。

Thank you for your help in advance. 提前谢谢你的帮助。

That's not an array of void* s, it's an array of void (which doesn't really exist), and dereferencing it gets you a void , which is "not a real type", so you can't assign to it. 这不是一个void* s的数组,它是一个void数组(实际上并不存在),并且取消引用它会得到一个void ,这是“不是真正的类型”,因此你无法分配它。

If you're trying to work with an array of void* , change it to 如果您正在尝试使用void*数组,请将其更改为

void queue_enqueue(void** q, void* item)

or 要么

void queue_enqueue(void* q[], void* item) // same as above

Also realise that your program will do 也意识到你的程序会做

q[sizeof(void**) - 1] = item;

every time. 每次。 sizeof gives you the size of the pointer type, not the array. sizeof为您提供指针类型的大小,而不是数组。 You have to pass the length of an array as a separate argument if you want to know how long an array is. 如果要知道数组的长度,则必须将数组的长度作为单独的参数传递。

你需要一个void*数组:

void queue_enqueue(void *q[], void *item)

void is a type with no size. void是一种没有大小的类型。 Thus an array of void cannot exist. 因此,不存在一系列空隙。 (because an array defines the amount of units and then jumps using the sizeof() operator. (因为数组定义了单位数量,然后使用sizeof()运算符跳转。

you would have to cast the void* to something*, something having the same size of what you actually have stored there, so say you have type T, which is of same SIZE( I'm not saying same thing ), 你必须将void *转换成某些东西*,这些东西的大小与你实际存储的大小相同,所以说你有类型T,它的大小相同(我不是说同样的东西),

T* q2 = (T*)q;
T* item2 = (T*)item;
q2[len-1] = &item;

This will work because C passes pointers by reference. 这将起作用,因为C通过引用传递指针。
since T will be a real type the compiler lets you do it 因为T将是一个真正的类型,编译器允许你这样做
since T is same size as your actual stored values, you will wind up with the correct value storage location. 由于T与实际存储值的大小相同,因此您将获得正确的值存储位置。

note that void* as an array, will contain a pointer to an array of type void. 请注意,void *作为一个数组,将包含一个指向void类型数组的指针。 void* as a single value will contain a single value of void. void *作为单个值将包含单个void值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM