无效* [英] void *

查看:57
本文介绍了无效*的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,


我想调整我的链表实现来保存任何数据类型。因为我理解它是一个使用''void *''的好地方。这就是我所做的:


struct list_node

{

void * data;

struct list_node * next;

};


typedef struct list_node node;


node * list_add_head(node ** head,void * data)

{

node * n = malloc(sizeof * n);

/ * check如果n == NULL ... * /


n-> data =(void *)data; / *正确使用? * /

n-> next = * head;

...

}


这样使用''void *''保证我可以提供各种数据类型,

即整数。长。 chars等等?

提前致谢。


-

祝你好运,罗马

解决方案

6月29日凌晨2:05,Roman Mashak < m ... @ tusur.ruwrote:


你好,


我想调整我的链表实现保留任何数据类型。因为我理解它是一个使用''void *''的好地方。这就是我所做的:


struct list_node

{

void * data;

struct list_node * next;


};


typedef struct list_node node;


node * list_add_head(node ** head,void * data)

{

node * n = malloc(sizeof * n);

/ *检查n == NULL ... * /


n-> data =(void *)data; / *正确使用? * /

n-> next = * head;

...


}

这样使用''void *''保证我可以提供各种数据类型,

即整数。长。 chars等等?

提前致谢。


-

祝你好运,Roman


IIRC,将int / longs / chars打包成指针不是标准

符合规范。您可以将数据对象的指针放入列表

struct。功能指针也无法存储在空*中。


干杯,


TJ


On Thu,2007年6月28日11:05:42 -0700,Roman Mashak < mr*@tusur.ru>

写道:


>你好,

我想要调整我的链表实现以保存任何数据类型。我理解它是一个使用''void *''的好地方。这就是我所做的:

struct list_node

void * data;

struct list_node * next ;
};



如果你的结构有一个成员确定

指向的任何数据的真实类型,那么它可能会有所帮助。
< blockquote class =post_quotes>
>
typedef struct list_node node;

node * list_add_head(node ** head,void * data)
{

节点* n = malloc(sizeof * n);

/ *检查n == NULL ... * /


n-> data =(void *)数据; / *正确使用? * /



n->数据为空*。数据是无效*。你认为演员如何完成演出?


n-> next = * head;



您需要将* head设置为n。


...
}

这样使用''void *''保证我可以提供各种数据类型,
整数。长。 chars等等?



不,它保证你可以提供指向各种数据的指针

类型(只有对象指针,而不是函数指针)。 />
删除电子邮件的del




" Barry Schwarz" < sc ****** @ doezl.net写信息

新闻:2c ************************ ******** @ 4ax.com ...


>>
struct list_node
{
void * data;
struct list_node * next;
};



如果您的结构中有一个成员确定了指向的任何数据的实际类型,那么它可能会有所帮助。



但是该结构不能包含所有可能类型的成员。而且我预计

''void *''是在各种类型的对象上保持_pointers_(也许我在原帖中不是很清楚
)即我能给你一些像这样的东西:


int main(无效)

{

node * p = NULL;


list_add_head(& p,(int *)100);

list_add_head(& p,(char *)' 'X'');

...

返回0;

}


那个是我对''void *''概念的理解。


>


>>
typedef struct list_node node;

node * list_add_head(node ** head,void * data)
{* / node * n = malloc(sizeof * n);
/ *检查n == NULL ... * /

n->数据=(void *)数据; / *正确使用? * /



n->数据为空*。数据是无效*。你觉得演员

完成了什么?



那么,n-> data =数据就足够了?


> n-> next = * head;



您需要将* head设置为n。


> ......

这样使用''void *''保证我可以提供各种类型的数据,
整数。长。 chars等等?



不,它保证你可以提供指向各种数据的指针

类型(只有对象指针,而不是函数指针)。 />



-

祝你好运,Roman


Hello,

I want to adapt my linked list implementation to hold any data types. As I
understand it''s a good place to use ''void *''. Here''s what I''ve done:

struct list_node
{
void *data;
struct list_node *next;
};

typedef struct list_node node;

node* list_add_head(node **head, void *data)
{
node *n= malloc(sizeof *n);
/* check if n==NULL ...*/

n->data = (void *)data; /* is that correct use? */
n->next = *head;
...
}

Does such a use of ''void *'' guarantee that I can supply various data types,
i.e. ints. long. chars and so forth?
Thanks in advance.

--
Best regards, Roman

解决方案

On Jun 29, 2:05 am, "Roman Mashak" <m...@tusur.ruwrote:

Hello,

I want to adapt my linked list implementation to hold any data types. As I
understand it''s a good place to use ''void *''. Here''s what I''ve done:

struct list_node
{
void *data;
struct list_node *next;

};

typedef struct list_node node;

node* list_add_head(node **head, void *data)
{
node *n= malloc(sizeof *n);
/* check if n==NULL ...*/

n->data = (void *)data; /* is that correct use? */
n->next = *head;
...

}

Does such a use of ''void *'' guarantee that I can supply various data types,
i.e. ints. long. chars and so forth?
Thanks in advance.

--
Best regards, Roman

IIRC, packing ints/longs/chars into pointers is not standards
conforming code. You can put pointers to data objects into your list
struct. Pointers to functions are also not storeable in void*.

Cheers,

TJ


On Thu, 28 Jun 2007 11:05:42 -0700, "Roman Mashak" <mr*@tusur.ru>
wrote:

>Hello,

I want to adapt my linked list implementation to hold any data types. As I
understand it''s a good place to use ''void *''. Here''s what I''ve done:

struct list_node
{
void *data;
struct list_node *next;
};

It would probably help if your structure had a member which identified
the real type of whatever data pointed to.

>
typedef struct list_node node;

node* list_add_head(node **head, void *data)
{
node *n= malloc(sizeof *n);
/* check if n==NULL ...*/

n->data = (void *)data; /* is that correct use? */

n->data is a void*. data is a void*. What do you think the cast
accomplishes?

n->next = *head;

You need to set *head to n.

...
}

Does such a use of ''void *'' guarantee that I can supply various data types,
i.e. ints. long. chars and so forth?

No, it guarantees that you can supply pointers to the various data
types (only object pointers, not function pointers).
Remove del for email



"Barry Schwarz" <sc******@doezl.netwrote in message
news:2c********************************@4ax.com...

>>
struct list_node
{
void *data;
struct list_node *next;
};


It would probably help if your structure had a member which identified
the real type of whatever data pointed to.

But the structure can''t have members for all possible types. And I expected
''void *'' is to hold _pointers_ on the objects of various types (perhaps I
wasn''t very clear in my original post), i.e. I could you something like
this:

int main(void)
{
node *p = NULL;

list_add_head(&p, (int *)100);
list_add_head(&p, (char *)''X'');
...
return 0;
}

That was my understanding of ''void *'' concept.

>

>>
typedef struct list_node node;

node* list_add_head(node **head, void *data)
{
node *n= malloc(sizeof *n);
/* check if n==NULL ...*/

n->data = (void *)data; /* is that correct use? */


n->data is a void*. data is a void*. What do you think the cast
accomplishes?

So, n->data = data is enough?

> n->next = *head;


You need to set *head to n.

> ...
}

Does such a use of ''void *'' guarantee that I can supply various data
types,
i.e. ints. long. chars and so forth?


No, it guarantees that you can supply pointers to the various data
types (only object pointers, not function pointers).

--
Best regards, Roman


这篇关于无效*的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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