[英]Converting a int pointer to a void pointer and back to use the value in another function?
I would like to convert a int pointer to a void pointer and pass that void pointer to a function and then back to an int pointer to use that value in another function.我想将 int 指针转换为 void 指针,并将该 void 指针传递给 function,然后返回到 int 指针以在另一个 function 中使用该值。
void main(){
int newSize = size;
void *newSizePtr = &newSize;
someFunc(newSizePtr);
}
void someFunc(void *newSizePtr){
int actualValue = *((int *) newSizePtr);
}
Is this the right way to convert a int ptr to a void ptr and then back to use the value?这是将 int ptr 转换为 void ptr 然后返回使用该值的正确方法吗?
i am unable to dynamically allocate memory to the pointer itself because of restrictions with my program that i cannot use malloc.我无法将 memory 动态分配给指针本身,因为我的程序限制我不能使用 malloc。 ie
IE
int *newSize = malloc(sizeof(int));
which is why i did it this way.这就是我这样做的原因。
i also need to pass in a void* argument because in my program i am using pthread_create().我还需要传入一个 void* 参数,因为在我的程序中我使用的是 pthread_create()。 This function requires me to pass in an argument of a void* to the function which is why i casted it to a void* and then back when i needed to use it
这个 function 要求我将 void* 的参数传递给 function 这就是为什么我将它转换为 void* 然后在我需要使用它时返回
The conversion you are doing is explicitly allowed by the C standard . C 标准明确允许您进行的转换。 Section 6.3.2.3p1 regarding pointer conversions states:
第 6.3.2.3p1 节关于指针转换状态:
A pointer to
void
may be converted to or from a pointer to any object type.指向
void
的指针可以转换为指向任何 object 类型的指针或从指针转换。 A pointer toa ny object type may be converted to a pointer tovoid
and back again;指向任何 object 类型的指针可以转换为指向
void
的指针并再次返回; the result shall compare equal to the original pointer.结果应与原始指针比较。
It's also not necessary to explictily cast to or from a void *
.也没有必要显式地转换为 void * 或从
void *
转换。 So you can do something like this:所以你可以做这样的事情:
void someFunc(void *newSizePtr){
int *actualValuePtr = newSizePtr;
}
int main(){
int newSize = size;
pthread_t tid;
pthread_create(&tid, NULL, someFunc, &newSize);
}
#include <stdio.h>
#include <memory>
void someFunc(void*);
int main() {
int size = 4;
int newSize = size;
void* newSizePtr = &newSize;
someFunc(newSizePtr);
// void* -> int*, before using
int* newSize = (int*) malloc(sizeof(int));
}
void someFunc(void* newSizePtr) {
int actualValue = *((int*)newSizePtr);
printf("%d", actualValue);
}
Yes you can cast void* to int*, and int* to void *,是的,您可以将 void* 转换为 int*,将 int* 转换为 void *,
Because, void * is 'generic' pointer.因为, void * 是“通用”指针。
malloc returns generic pointer (void*) because malloc does not know what 'type' of return you need. malloc 返回通用指针 (void*) 因为 malloc 不知道您需要什么“类型”的返回。
So, you need to convert to the type you need.所以,你需要转换成你需要的类型。
(In the above code, you need to convert to void* -> int*) (在上面的代码中,需要转换为void* -> int*)
For more information about usage of generic pointer, below link may help you有关使用通用指针的更多信息,下面的链接可能会对您有所帮助
https://codexpart.com/what-is-generic-pointer-difference-between-generic-pointer-and-void-pointer/ https://codexpart.com/what-is-generic-pointer-difference-between-generic-pointer-and-void-pointer/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.