C语言 我如何从 void * 转换回 int
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3200294/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I convert from void * back to int
提问by w31
if I have
如果我有
int a= 5;
long b= 10;
int count0 = 2;
void ** args0;
args0 = (void **)malloc(count0 * sizeof(void *));
args0[0] = (void *)&a;
args0[1] = (void *)&b;
how can I convert from args[0] and args0[1] back to int and long? for example
如何将 args[0] 和 args0[1] 转换回 int 和 long?例如
int c=(something im missing)args0[0]
long d=(something im missing)args1[0]
回答by Arthur Shipkowski
Assuming that your &a0 and &b0 are supposed to be &a and &b, and that you mean args0[1] for setting up long d, you have stored a pointer to a in args0[0] and a pointer to b in args0[1]. This means you need to convert them to the correct pointer types.
假设您的 &a0 和 &b0 应该是 &a 和 &b,并且您的意思是 args0[1] 用于设置 long d,那么您已经在 args0[0] 中存储了一个指向 a 的指针,并在 args0[1] 中存储了一个指向 b 的指针. 这意味着您需要将它们转换为正确的指针类型。
int c = *((int *)args0[0]);
int d = *((long *)args0[1]);
回答by keithm
To literally answer your question, you'd write
要从字面上回答你的问题,你会写
int c = *((int *)args0[0]);
long d = *((long *)args[1]);
What might concern me about your code is that you have allocated space for the pointersto your locations, but you haven't allocated memory for the valuesthemselves. If you expect to persist these locations beyond the local scope, you have to do something like:
我可能对您的代码感到担忧的是,您已为指向位置的指针分配了空间,但尚未为值本身分配内存。如果您希望在本地范围之外保留这些位置,则必须执行以下操作:
int *al = malloc(sizeof(int));
long *bl = malloc(sizeof(long));
*al = a;
*bl = b;
void **args0 = malloc(2 * sizeof(void *));
args0[0] = al;
args0[1] = bl;
回答by Alok Singhal
While others have answered your question, I will make a comment about the last three lines in the first part of your code snippet:
虽然其他人已经回答了您的问题,但我将对您的代码片段第一部分的最后三行发表评论:
args0 = (void **)malloc(count0 * sizeof(void *));
args0[0] = (void *)&a;
args0[1] = (void *)&b;
The above is better written as:
上面最好写成:
args0 = malloc(count0 * sizeof *args0);
args0[0] = &a;
args0[1] = &b;
The malloc()call is easier to read this way, and less error-prone. You don't need a cast in the last two statements since C guarantees conversions to and from an object pointer and a void pointer.
通过malloc()这种方式调用更容易阅读,并且不易出错。您不需要在最后两个语句中进行强制转换,因为 C 保证与对象指针和 void 指针之间的转换。
回答by Nick Cuevas
If you're testing, i suggest use it an external function, to get more readability:
如果您正在测试,我建议将其用作外部函数,以获得更高的可读性:
int get_int(void* value){
return *((int*) value);
}
long get_long(void* value){
return *((long*) value);
}
then in your code:
然后在你的代码中:
int c = get_int(args0[0]);
long d = get_long(args0[1]);
That should work.
那应该工作。
回答by bits
Try this:
尝试这个:
int c = *( (int *) args0[0]);
long d = *( (long *) args0[1]);
回答by Maister
You need to tell it that the void* should be interpreted as an int* or long* when you dereference.
您需要告诉它,当您取消引用时,应将 void* 解释为 int* 或 long*。
int a = 5;
long b = 10;
void *args[2];
args[0] = &a;
args[1] = &b;
int c = *(int*)args[0];
long d = *(long*)args[1];

