将void *转换为void **? [英] Casting void * to void ** ?
问题描述
大家好,
我有一个问题听起来很基本。
我的结构很简单:
struct simple {
void * buffer;
};
typedef struct simple简单;
在我的函数中我这样做:
void do_Something(){
Simple * simp_struct;
simp_struct-> buffer = malloc(10 * sizeof(int *));
call_func((void **)((int **)(simp_struct - >缓冲区)));
....
}
函数call_func有这个原型:
call_func(void ** buf);
我对这段代码感到困惑:
call_func((void **) ((int **)(simp_struct-> buffer)));
这个结构是什么意思?如何将simp_struct->缓冲区
(这是一个void *)强制转换为int **,然后将
强制转换为void **并传递给call_func?
Rgds。
Mirage
Twister写道:< blockquote class =post_quotes>大家好,
我有一个听起来很基本的问题。
我的结构很简单:
struct simple {
void * buffer;
};
typedef struct simple简单;
在我的函数中我这样做:
void do_Something(){
简单* simp_struct;
simp_struct-> buffer = malloc(10 * sizeof(int *));
call_func((void * *)((int **)(simp_struct-> buffer)));
....
函数call_func有这个原型:
call_func(void ** buf);
我对这段代码很困惑:
call_func((void **)((int **)(simp_struct-> buffer)) );
这是什么缺点结合意味着什么?如何将simp_struct->缓冲区
(这是一个void *)强制转换为int **,后跟
强制转换为void **并传递给call_func?
Rgds。
幻影
我错误输入了以前邮件的一部分:
这段代码:我很困惑这段代码:
call_func((void **)((int **)(simp_struct-> buffer)));
应该是这个:
for(i = 0; i< 10; i ++)
call_func((void **)((int **)simp_struct-> ;缓冲区+ i));
我的问题依然如故。以上
构造的含义是什么意思?
Rgds。
Mirage
Twister说:
< snip>
simp_struct-> buffer = malloc(10 * sizeof(int *)) ; call_func((void **)((int **)(simp_struct-> buffer)));
为什么不这样做:
call_func(& simp_struct-> buffer);
强制转换几乎总是错误的。
函数call_func有这个原型:
call_func(void ** buf);
我是与这段代码混淆:
call_func((void **)((int **)(simp_struct-> buffer)));
这个构造意味着什么?
这意味着你不会(或者写不出来的人)了解什么铸件
for。
< br $>
-
Richard Heathfield
Usenet是一个奇怪的地方 - dmr 29/7/1999
http://www.cpax.org.uk
电子邮件:rjh在上面的域名(但显然放弃了www)
在文章< k8 **** *********@news.oracle.com>,
Twister< tw ***** @ nospam.com>写道:
struct simple {
void * buffer;
};
typedef struct simple Simple;
void do_Something(){
Simple * simp_struct;
simp_struct是该语句后的未初始化指针。
simp_struct-> buffer = malloc(10 * sizeof(int *));
但是你正在使用它,就像它被初始化一样。
simp_struct->缓冲区首先解除引用simp_struct和
然后在那里访问名为buffer的结构组件,所以
首先需要给simp_struct一个值。
call_func((void **)((int **)(simp_struct) - >缓冲区)));
....
}
-
原型是其克隆的超类型。 - maplesoft
Hi All,
I have a question which might sound very basic.
I have a simple structure:
struct simple{
void *buffer;
};
typedef struct simple Simple;
In my function I do this:
void do_Something(){
Simple *simp_struct;
simp_struct->buffer = malloc(10 * sizeof(int *));
call_func((void **)((int **)(simp_struct->buffer)));
....
}
The function call_func has this prototype:
call_func(void **buf);
I am confused with this piece of code:
call_func((void **)((int **)(simp_struct->buffer)));
What does this construct mean? How is that simp_struct->buffer
(which is a void *) is being cast to a int** followed by a
cast to void ** and passed to call_func ?
Rgds.
Mirage
Twister wrote:Hi All,
I have a question which might sound very basic.
I have a simple structure:
struct simple{
void *buffer;
};
typedef struct simple Simple;
In my function I do this:
void do_Something(){
Simple *simp_struct;
simp_struct->buffer = malloc(10 * sizeof(int *));
call_func((void **)((int **)(simp_struct->buffer)));
....
}
The function call_func has this prototype:
call_func(void **buf);
I am confused with this piece of code:
call_func((void **)((int **)(simp_struct->buffer)));
What does this construct mean? How is that simp_struct->buffer
(which is a void *) is being cast to a int** followed by a
cast to void ** and passed to call_func ?
Rgds.
Mirage
I mistyped part of my previous mail:
This piece of code: I am confused with this piece of code:
call_func((void **)((int **)(simp_struct->buffer)));
should be this:
for(i=0; i<10 ;i++)
call_func((void **)((int **)simp_struct->buffer + i));
My question remains the same. What does the above
construct mean?
Rgds.
Mirage
Twister said:
<snip>
simp_struct->buffer = malloc(10 * sizeof(int *));
call_func((void **)((int **)(simp_struct->buffer)));
Why not just do this:
call_func(&simp_struct->buffer);
Casts are almost always wrong.
The function call_func has this prototype:
call_func(void **buf);
I am confused with this piece of code:
call_func((void **)((int **)(simp_struct->buffer)));
What does this construct mean?
It means you don''t (or whoever wrote it doesn''t) understand what casting is
for.
--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
In article <k8*************@news.oracle.com>,
Twister <tw*****@nospam.com> wrote:
struct simple{
void *buffer;
};
typedef struct simple Simple; void do_Something(){
Simple *simp_struct;
simp_struct is an uninitialized pointer after that statement.
simp_struct->buffer = malloc(10 * sizeof(int *));
But there you are using it as if it was initialized.
simp_struct->buffer involves dereferencing simp_struct first and
then accessing the structure component named buffer there, so
simp_struct needs to be given a value first.
call_func((void **)((int **)(simp_struct->buffer)));
....
}
--
Prototypes are supertypes of their clones. -- maplesoft
这篇关于将void *转换为void **?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!