转换泛型或void指针指向结构... [英] Casting a generic or void pointer to point to a struct...

查看:82
本文介绍了转换泛型或void指针指向结构...的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,我要感谢所有花时间回答我关于根据存储在

变量中的数值创建数组的问题的问题。


我在阅读完回复并进行更多研究后意识到,我真正需要的东西在C中被称为动态数组。基本上,

你超越数组表示法并使用带有内存的指针获得带有malloc()或calloc()的
。我认为这将完全符合我的需要。


这提出了另一个问题。我不确定我用什么语法将b
用于将一个gneeric或void指针强制转换为我已定义的
结构。例如,如果我已经定义了new_data_type,那么结构

之前在源代码文件中,以下代码是否有效?


/ *创建一个通用指针,指向块中的第一个元素br />
使用calloc()函数获得的内存。 * /

void generic_pointer = calloc(number_of_elements,size_of_element);


/ *如果内存已成功分配,则转换为通用

指向正确数据类型的指针。 * /

if(generic_pointer!= NULL)

(struct new_data_type *)generic_pointer

else

/ *我们遇到了问题。 * /


再次感谢大家的帮助。


Scott Huey

First, I would thank all of those that took the time to answer my
question about creating an array based on a numeric value stored in a
variable.

I realize after reading the responses and doing some more research,
that what I really need is known in C as a "dynamic array". Basically,
you surpass the array notation and use pointers with memory obtained
with malloc() or calloc(). I think this will do just what I needed.

That has brought up another question though. I''m not sure what syntax I
would use to cast a gneeric or void pointer to a struct that I have
defined. For example, if I have defined the "new_data_type" struct
previously in the source code file, would the following code be valid?

/* Create a generic pointer to the first element in the block of
memory obtained with the calloc() function. */
void generic_pointer = calloc(number_of_elements, size_of_element);

/* If the memory has been sucessfully allocated, cast the generic
pointer to the correct data type. */
if(generic_pointer != NULL)
(struct new_data_type *)generic_pointer
else
/* We''ve got problems. */

Thanks again for the help everyone.

Scott Huey

推荐答案


re ***** ***********@gmail.com 写道:

首先,我要感谢所有花时间的人回答我的

关于根据存储在

变量中的数值创建数组的问题。


我读完之后意识到回答并做了一些更多的研究,我真正需要的是在C中称为动态数组。基本上,

你超越数组表示法并使用带有内存的指针获得带有malloc()或calloc()的
。我认为这将完全符合我的需要。


这提出了另一个问题。我不确定我用什么语法将b
用于将一个gneeric或void指针强制转换为我已定义的
结构。例如,如果我已经定义了new_data_type,那么结构

之前在源代码文件中,以下代码是否有效?


/ *创建一个通用指针,指向块中的第一个元素br />
使用calloc()函数获得的内存。 * /

void generic_pointer = calloc(number_of_elements,size_of_element);


/ *如果内存已成功分配,则转换为通用

指向正确数据类型的指针。 * /

if(generic_pointer!= NULL)

(struct new_data_type *)generic_pointer

else

/ *我们遇到了问题。 * /


再次感谢大家的帮助。


Scott Huey
First, I would thank all of those that took the time to answer my
question about creating an array based on a numeric value stored in a
variable.

I realize after reading the responses and doing some more research,
that what I really need is known in C as a "dynamic array". Basically,
you surpass the array notation and use pointers with memory obtained
with malloc() or calloc(). I think this will do just what I needed.

That has brought up another question though. I''m not sure what syntax I
would use to cast a gneeric or void pointer to a struct that I have
defined. For example, if I have defined the "new_data_type" struct
previously in the source code file, would the following code be valid?

/* Create a generic pointer to the first element in the block of
memory obtained with the calloc() function. */
void generic_pointer = calloc(number_of_elements, size_of_element);

/* If the memory has been sucessfully allocated, cast the generic
pointer to the correct data type. */
if(generic_pointer != NULL)
(struct new_data_type *)generic_pointer
else
/* We''ve got problems. */

Thanks again for the help everyone.

Scott Huey



struct new_data_type * ptr = calloc(number_of_elements,

size_of_element);

if(ptr == NULL)

{

/ *错误* /

}

其他

{

/ *做点什么* / < br $>
}


MQ

struct new_data_type * ptr = calloc(number_of_elements,
size_of_element);
if(ptr == NULL)
{
/*error */
}
else
{
/*do something */
}

MQ




MQ写道:

MQ wrote:
re ****** **********@gmail.com 写道:

首先,我要感谢所有花时间回答的人我的

关于根据存储在

变量中的数值创建数组的问题。


读完回复后我才意识到并做了一些更多的研究,我真正需要的是在C中称为动态数组。基本上,

你超越数组表示法并使用带有内存的指针获得带有malloc()或calloc()的
。我认为这将完全符合我的需要。


这提出了另一个问题。我不确定我用什么语法将b
用于将一个gneeric或void指针强制转换为我已定义的
结构。例如,如果我已经定义了new_data_type,那么结构

之前在源代码文件中,以下代码是否有效?


/ *创建一个通用指针,指向块中的第一个元素br />
使用calloc()函数获得的内存。 * /

void generic_pointer = calloc(number_of_elements,size_of_element);


/ *如果内存已成功分配,则转换为通用

指向正确数据类型的指针。 * /

if(generic_pointer!= NULL)

(struct new_data_type *)generic_pointer

else

/ *我们遇到了问题。 * /


再次感谢大家的帮助。


Scott Huey
First, I would thank all of those that took the time to answer my
question about creating an array based on a numeric value stored in a
variable.

I realize after reading the responses and doing some more research,
that what I really need is known in C as a "dynamic array". Basically,
you surpass the array notation and use pointers with memory obtained
with malloc() or calloc(). I think this will do just what I needed.

That has brought up another question though. I''m not sure what syntax I
would use to cast a gneeric or void pointer to a struct that I have
defined. For example, if I have defined the "new_data_type" struct
previously in the source code file, would the following code be valid?

/* Create a generic pointer to the first element in the block of
memory obtained with the calloc() function. */
void generic_pointer = calloc(number_of_elements, size_of_element);

/* If the memory has been sucessfully allocated, cast the generic
pointer to the correct data type. */
if(generic_pointer != NULL)
(struct new_data_type *)generic_pointer
else
/* We''ve got problems. */

Thanks again for the help everyone.

Scott Huey



struct new_data_type * ptr = calloc(number_of_elements,

size_of_element);

if(ptr == NULL)

{

/ *错误* /

}

其他

{

/ *做点什么* / < br $>
}


MQ


struct new_data_type * ptr = calloc(number_of_elements,
size_of_element);
if(ptr == NULL)
{
/*error */
}
else
{
/*do something */
}

MQ



更正:size_of_element应替换为sizeof(struct

new_data_type)


MQ


correction: size_of_element should be replaced by sizeof(struct
new_data_type)

MQ


re **************** @ gmail.com 写道:

首先,我要感谢所有花时间回答我关于根据存储在
变量。


我在阅读完回复并做了一些研究后意识到,

我真正需要的是C中已知的动态阵列。基本上,

你超越数组表示法并使用带有内存的指针获得带有malloc()或calloc()的
。我认为这将完全符合我的需要。


这提出了另一个问题。我不确定我用什么语法将b
用于将一个gneeric或void指针强制转换为我已定义的
结构。例如,如果我已经定义了new_data_type,那么结构

之前在源代码文件中,以下代码是否有效?


/ *创建一个通用指针,指向块中的第一个元素br />
使用calloc()函数获得的内存。 * /

void generic_pointer = calloc(number_of_elements,size_of_element);


/ *如果内存已成功分配,则转换为通用

指向正确数据类型的指针。 * /

if(generic_pointer!= NULL)

(struct new_data_type *)generic_pointer

else

/ *我们遇到了问题。 * /
First, I would thank all of those that took the time to answer my
question about creating an array based on a numeric value stored in a
variable.

I realize after reading the responses and doing some more research,
that what I really need is known in C as a "dynamic array". Basically,
you surpass the array notation and use pointers with memory obtained
with malloc() or calloc(). I think this will do just what I needed.

That has brought up another question though. I''m not sure what syntax I
would use to cast a gneeric or void pointer to a struct that I have
defined. For example, if I have defined the "new_data_type" struct
previously in the source code file, would the following code be valid?

/* Create a generic pointer to the first element in the block of
memory obtained with the calloc() function. */
void generic_pointer = calloc(number_of_elements, size_of_element);

/* If the memory has been sucessfully allocated, cast the generic
pointer to the correct data type. */
if(generic_pointer != NULL)
(struct new_data_type *)generic_pointer
else
/* We''ve got problems. */



不要转换malloc()或calloc()的结果。这是不必要的,并且它可以掩盖某些错误。


BTW,它是void * generic_pointer,而不是void generic_pointer"。


另外,请记住,calloc()并不一定像你想象的那样有用。
可能认为它是。它将分配的内存初始化为

all-bits-zero。对于指针或浮点数据,这不是必须有意义的b $ b;无法保证null

指针或浮点0.0表示为全位零。


通常它''更容易使用malloc()(它没有初始化

分配的对象),只是确保你不使用任何部分

你没有赋值的对象。


为什么你想要一个通用指针而不是一个指向你的指针

" new_data_type"结构?例如:


struct new_data_type {

/ *会员声明在这里* /

};


struct new_data_type * ptr;


ptr = malloc(number_of_elements * sizeof * ptr);


有时您确实需要使用void *指针,但通常使用特定的指针类型更好地使用



-

Keith Thompson(The_Other_Keith) ks***@mib.org < http://www.ghoti.net/~kst>

圣地亚哥超级计算机中心< *< http://users.sdsc.edu/~kst>

我们必须做点什么。这是事情。因此,我们必须这样做。

Don''t cast the result of malloc() or calloc(). It''s unnecessary, and
it can mask certain errors.

BTW, it''s "void *generic_pointer", not "void generic_pointer".

Also, keep in mind that calloc() isn''t necessarily as useful as you
might think it is. It initializes the allocated memory to
all-bits-zero. For pointers or floating-point data, this is not
necessarily meaningful; there''s no guarantee that either a null
pointer or a floating-point 0.0 is represented as all-bits-zero.

Usually it''s easier to use malloc() (which doesn''t initialize the
allocated object), and just make sure that you don''t use any portion
of the object that you haven''t assigned a value to.

Why do you want a generic pointer rather than a pointer to your
"new_data_type" struct? For example:

struct new_data_type {
/* member declarations here */
};

struct new_data_type *ptr;

ptr = malloc(number_of_elements * sizeof *ptr);

Sometimes you do need to use void* pointers, but quite often it''s
better to use a specific pointer type.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.


这篇关于转换泛型或void指针指向结构...的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆