c中类型转换问题

demogorgon 2002-03-28 08:30:01
typedef enum conf_type {
CONF_ITEM_PAIR,
CONF_ITEM_SECTION
} CONF_ITEM_TYPE;

struct conf_item {
struct conf_item *next;
struct conf_part *parent;
int lineno;
CONF_ITEM_TYPE type;
};
struct conf_pair {
CONF_ITEM item;
char *attr;
char *value;
LRAD_TOKEN operator;
};
struct conf_part {
CONF_ITEM item;
char *name1;
char *name2;
struct conf_item *children;
};

有这几个结构,他们之间进行类型转换,请问是怎么执行的?直接转换不会产生不安全或
错误么?

CONF_PAIR *cf_itemtopair(CONF_ITEM *ci)
{
if (ci == NULL)
return NULL;
rad_assert(ci->type == CONF_ITEM_PAIR);
return (CONF_PAIR *)ci;
}
CONF_SECTION *cf_itemtosection(CONF_ITEM *ci)
{
if (ci == NULL)
return NULL;
rad_assert(ci->type == CONF_ITEM_SECTION);
return (CONF_SECTION *)ci;
}
CONF_ITEM *cf_pairtoitem(CONF_PAIR *cp)
{
if (cp == NULL)
return NULL;
return (CONF_ITEM *)cp;
}
CONF_ITEM *cf_sectiontoitem(CONF_SECTION *cs)
{
if (cs == NULL)
return NULL;
return (CONF_ITEM *)cs;
}
...全文
88 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
prototype 2002-03-29
  • 打赏
  • 举报
回复
ok, if they are sure the compiler doesn't screw up the data, we can understand it.

struct conf_item {
struct conf_item *next; // pointer
struct conf_part *parent; // pointer
int lineno; // int
CONF_ITEM_TYPE type; // int
};
struct conf_part {
CONF_ITEM item; // struct conf_item
char *name1; // pointer
char *name2; // pointer
struct conf_item *children; // pointer
};

if we expand conf_part, we will get:
struct conf_part {
struct conf_item *next; // pointer
struct conf_part *parent; // pointer
int lineno; // int
CONF_ITEM_TYPE type; // int
char *name1; // pointer
char *name2; // pointer
struct conf_item *children; // pointer
};

you will note that the address that a 'struct conf_item' pointer points to can be the same as that of a 'struct conf_part' pointer points to, because the 'CONF_ITEM' is the first field of the 'struct conf_part' (and we assume the members are organized in memory as they are in the code.).

so the following code is ok:

struct conf_part aaa;
CONF_ITEM* p = aaa.item;

(conf_part*) p; // ok!

prototype 2002-03-28
  • 打赏
  • 举报
回复
you cannot do that, simply because the organizations of data in memory for 'conf_part' and 'conf_item' might be completely different. the compiler has no way to convert the data organization from one struct to another. you have to rewrite the code. what on earth do you want?
demogorgon 2002-03-28
  • 打赏
  • 举报
回复
我注释,简化一下好了
typedef struct conf_item CONF_ITEM //CONF_ITEM即conf_item结构
typedef struct conf_part CONF_SECTION //CONF_SECTION即conf_part结构

struct conf_item { //定义conf_item结构
struct conf_item *next; //指向下一个conf_item结构的指针
struct conf_part *parent; //指向conf_part的指针
int lineno; //行号
CONF_ITEM_TYPE type; //类型
};
struct conf_part { //定义conf_part结构
CONF_ITEM item;
char *name1;
char *name2;
struct conf_item *children;
};
----------------------------------------------------
//函数调用
//把CONF_ITEM指针 转成 CONF_SECTION指针

CONF_SECTION *cf_itemtosection(CONF_ITEM *ci) {
if (ci == NULL)
return NULL;
rad_assert(ci->type == CONF_ITEM_SECTION);

return (CONF_SECTION *)ci;
}

heartlove 2002-03-28
  • 打赏
  • 举报
回复
你的代码太长,我都懒得看,结构转换时可以的,大结构向小结构的转换应该不会产生安全问题,如果不同结构类型(即不是继承而来的,结构再C++中和类等价的),可能数据不对,小结构向大结构转换会有安全问题产生。指针转换是最方便的
demogorgon 2002-03-28
  • 打赏
  • 举报
回复
我也觉得不好阿,但是GNU里的一个给RADIUS协议写的服务器就是这么写的,而且好像也不会出什么错吧,有没有高手解释一下?
wkoji 2002-03-28
  • 打赏
  • 举报
回复
同意楼上的
demogorgon 2002-03-28
  • 打赏
  • 举报
回复
有人给我的回复,不大明白,大家帮忙看看:
In general, they can't. C allows compilers to re-arrange the
elements of a data structure, so the structures can't be cast.

However, all sane C compilers DON'T screw up like that, so the type
conversion is safe.

Another answer is: The server works, doesn't it?

Alan DeKok.
kangjian1 2002-03-28
  • 打赏
  • 举报
回复
不行啊,结构会乱的,有指针就更危险啦。
要是真的想兼容多个结构,可以使用联合(union).
demogorgon 2002-03-28
  • 打赏
  • 举报
回复
没人回答啊?upupupup~~~
demogorgon 2002-03-28
  • 打赏
  • 举报
回复
up~~~
demogorgon 2002-03-28
  • 打赏
  • 举报
回复
这不是我写的代码,是支持RADIUS协议的一个服务器freeradius的
源码(http://freeradius.org),我也是觉得不可行才问各位DX的,但是这个服务器的确可以跑起来啊。

69,382

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧