Say I have a void* named ptr. How exactly should I go about using ptr to store an int? Is it enough to write
假设我有一个名为ptr的void *。我应该如何使用ptr来存储int?写作是否足够
ptr = (void *)5;
If I want to save the number 5? Or do I have to malloc something to save it?
如果我想保存5号?或者我必须使用malloc来保存它?
4 个解决方案
#1
30
You're casting 5
to be a void pointer and assigning it to ptr
.
你将5转换为void指针并将其分配给ptr。
Now ptr points at the memory address 0x5
现在ptr指向内存地址0x5
If that actually is what you're trying to do .. well, yeah, that works. You ... probably don't want to do that.
如果这实际上是你想要做的......好吧,是的,这是有效的。你......可能不想这样做。
When you say "store an int" I'm going to guess you mean you want to actually store the integer value 5 in the memory pointed to by the void*
. As long as there was enough memory allocated ( sizeof(int)
) you could do so with casting ...
当你说“存储一个int”我猜你的意思是你想要将整数值5存储在void *指向的内存中。只要有足够的内存分配(sizeof(int))你就可以通过强制转换来实现...
void *ptr = malloc(sizeof(int));
*((int*)ptr) = 5;
printf("%d\n",*((int*)ptr));
#2
3
That will work on all platforms/environments where sizeof(void*) >= sizeof(int)
, which is probably most of them, but I think not all of them. You're not supposed to rely on it.
这将适用于sizeof(void *)> = sizeof(int)的所有平台/环境,这可能是其中的大多数,但我认为不是全部。你不应该依赖它。
If you can you should use a union instead:
如果可以,你应该使用工会代替:
union {
void *ptr;
int i;
};
Then you can be sure there's space to fit either type of data and you don't need a cast. (Just don't try to dereference the pointer while its got non-pointer data in it.)
然后你可以确定有适合任何类型数据的空间,你不需要演员表。 (当它的指针中有非指针数据时,不要试图取消引用指针。)
Alternatively, if the reason you're doing this is that you were using an int to store an address, you should instead use
size_t
intptr_t
so that that's big enough to hold any pointer value on any platform.
或者,如果你这样做的原因是你使用int来存储地址,你应该使用size_t intptr_t,这样它就足以在任何平台上保存任何指针值。
#3
0
A pointer always points to a memory address. So if you want to save a variable with pointer, what you wanna save in that pointer is the memory address of your variable.
指针始终指向内存地址。因此,如果要使用指针保存变量,那么您希望在该指针中保存的是变量的内存地址。
#4
0
The cast
is sufficient..................
演员阵容足够..................
#1
30
You're casting 5
to be a void pointer and assigning it to ptr
.
你将5转换为void指针并将其分配给ptr。
Now ptr points at the memory address 0x5
现在ptr指向内存地址0x5
If that actually is what you're trying to do .. well, yeah, that works. You ... probably don't want to do that.
如果这实际上是你想要做的......好吧,是的,这是有效的。你......可能不想这样做。
When you say "store an int" I'm going to guess you mean you want to actually store the integer value 5 in the memory pointed to by the void*
. As long as there was enough memory allocated ( sizeof(int)
) you could do so with casting ...
当你说“存储一个int”我猜你的意思是你想要将整数值5存储在void *指向的内存中。只要有足够的内存分配(sizeof(int))你就可以通过强制转换来实现...
void *ptr = malloc(sizeof(int));
*((int*)ptr) = 5;
printf("%d\n",*((int*)ptr));
#2
3
That will work on all platforms/environments where sizeof(void*) >= sizeof(int)
, which is probably most of them, but I think not all of them. You're not supposed to rely on it.
这将适用于sizeof(void *)> = sizeof(int)的所有平台/环境,这可能是其中的大多数,但我认为不是全部。你不应该依赖它。
If you can you should use a union instead:
如果可以,你应该使用工会代替:
union {
void *ptr;
int i;
};
Then you can be sure there's space to fit either type of data and you don't need a cast. (Just don't try to dereference the pointer while its got non-pointer data in it.)
然后你可以确定有适合任何类型数据的空间,你不需要演员表。 (当它的指针中有非指针数据时,不要试图取消引用指针。)
Alternatively, if the reason you're doing this is that you were using an int to store an address, you should instead use
size_t
intptr_t
so that that's big enough to hold any pointer value on any platform.
或者,如果你这样做的原因是你使用int来存储地址,你应该使用size_t intptr_t,这样它就足以在任何平台上保存任何指针值。
#3
0
A pointer always points to a memory address. So if you want to save a variable with pointer, what you wanna save in that pointer is the memory address of your variable.
指针始终指向内存地址。因此,如果要使用指针保存变量,那么您希望在该指针中保存的是变量的内存地址。
#4
0
The cast
is sufficient..................
演员阵容足够..................