指针的一些问题 [英] Some issue with pointers
问题描述
您好,
我需要一些帮助来了解我在做什么:)
我正在为嵌入式系统编写代码,我需要创建一个可导航的
菜单系统。我打算使用这些结构:
typedef struct {
char * name [2];
int type ;
int num_param;
void * ptr;
} menuitem_t;
typedef struct {
char * name [2];
int num_items;
menuitem_t * items;
} menu_t;
后者定义每个菜单或子菜单的名称,项目数量
和指向项目结构的指针。
前者定义每个菜单项的名称,类型(见下文)
参数数量 - 如果有的话 - 最后是一个通用指针(见下面
再次) 。
通过这种方式,我可以生成任何复杂且非常灵活的菜单。
如果项目超前,则void指针应指向另一个menu_t结构/>
到一个子菜单,如果它导致一个参数列表到一个数组。但它可以
也可以导致一个功能,如果这个项目的选择需要一个动作
来执行。
其实我有两个问题:
1)我无法正确地投射虚空* ptr。让我们说我想访问:
char * par [] = {" First"," Second"};
ot调用:
void myfunc();
请你帮助我理解如何施放然后使用那些
指针在这些情况下?
2)如果我能知道用户关注的路径会很好。对于
示例:
mainmenu -seconditem -firstitem
其中每个实体都是menu_t结构。
你会如何实现这样一条动态路径?
谢谢你,我为我可怜的英语道歉
Marco / iw2nzm
Hello,
I need some help to understand what I''m doing :)
I''m writing code for an embedded system and I need to create a navigable
menu system. I''m going to use these structures:
typedef struct {
char *name[2];
int type;
int num_param;
void *ptr;
} menuitem_t;
typedef struct {
char *name[2];
int num_items;
menuitem_t *items;
} menu_t;
The latter defines each menu or submenu with a name, a number of items
and a pointer to the items structure.
The former defines each menu item with a name, a type (see below) the
number of parameters - if any - and finally a generic pointer (see below
again).
In this way I can generate menus of any complexity and very flexible.
The void pointer should point to another menu_t struct if the item leads
to a submenu, to an array if it leads to a parameter list. But it could
also leads to a function if the selection of this item needs an action
to be executed.
Actually I have two questions:
1) I can''t cast correctly the void *ptr. Let''s say I want to access to:
char *par[] = {"First", "Second"};
ot invoke:
void myfunc();
Please, may you help me to understand how to cast and then to use that
pointer in these cases?
2) It would be nice if I can know the path the user is following. For
example:
mainmenu -seconditem -firstitem
where each of these entities are menu_t structs.
How would you implement such a dynamic path?
Thank you and I apologize for my poor English
Marco / iw2nzm
推荐答案
Marco Trapanese< ma ****************** @ gmail.comwrites:
Marco Trapanese <ma******************@gmail.comwrites:
我正在为嵌入式系统编写代码,我需要创建一个
可导航菜单系统。我打算使用这些结构:
typedef struct {
char * name [2];
int type ;
int num_param;
void * ptr;
} menuitem_t;
typedef struct {
char * name [2];
int num_items;
menuitem_t * items;
} menu_t;
后者定义了每个菜单或子菜单,其中包含名称,多个项目
以及指向项目结构的指针。
前者定义每个菜单项的名称,类型(见下文)
参数数量 - 如果有的话 - 最后是通用指针(参见
再次下面)。
I''m writing code for an embedded system and I need to create a
navigable menu system. I''m going to use these structures:
typedef struct {
char *name[2];
int type;
int num_param;
void *ptr;
} menuitem_t;
typedef struct {
char *name[2];
int num_items;
menuitem_t *items;
} menu_t;
The latter defines each menu or submenu with a name, a number of items
and a pointer to the items structure.
The former defines each menu item with a name, a type (see below) the
number of parameters - if any - and finally a generic pointer (see
below again).
我不明白为什么你会有两个。我会像这样合并它们:
typedef struct menu {
char * name [2];
int type ;
int num_param;
union {
void * vp;
struct menu * items;
} ptr;
} menu_t;
菜单中只有一种类型(在C意义上)必须更简单/>
项目,当然?
I can''t see why you''d have two. I''d merge them like this:
typedef struct menu {
char *name[2];
int type;
int num_param;
union {
void *vp;
struct menu *items;
} ptr;
} menu_t;
It must be simpler to have only one type (in the C sense) for menu
items, surely?
通过这种方式,我可以生成任何复杂的菜单,并且非常灵活。 void指针应该指向另一个menu_t结构,如果该项导致一个子菜单,则指向一个数组,如果它导致一个参数
list。但是如果选择这个
项目需要执行一个动作,它也可能导致一个功能。
In this way I can generate menus of any complexity and very
flexible. The void pointer should point to another menu_t struct if
the item leads to a submenu, to an array if it leads to a parameter
list. But it could also leads to a function if the selection of this
item needs an action to be executed.
函数指针是特殊的因为你不能保证在一个空格*转换为函数指针(反之亦然)。
你的实现可能会提供这个作为扩展,但是如果你使用一个
联合,你可以添加一个函数指针类型,并且没有任何
可移植性的担忧。
Function pointers are "special" in that there is no guarantee in C
that you can convert a void * to function pointer (nor vice versa).
Your implementation may offer this as an extension, but if you use a
union, you can add a function pointer type and not have any
portability worries.
实际上我有两个问题:
1)我无法正确地投射虚空* ptr。让我们说我想访问:
char * par [] = {" First"," Second"};
Actually I have two questions:
1) I can''t cast correctly the void *ptr. Let''s say I want to access to:
char *par[] = {"First", "Second"};
让我们说ptr设置如下:''ptr = par;''。 ptr然后是伪装的一个
char **,所以简单写作:
char ** strings = ptr;
会让你访问字符串[0]和字符串[1]。
Let''s say that ptr got set like this: ''ptr = par;''. ptr is then a
char ** in disguise so simply writing:
char **strings = ptr;
would let you access strings[0] and strings[1].
ot invoke:
void myfunc( );
请你帮助我理解如何在这些情况下施放然后使用那个
指针?
ot invoke:
void myfunc();
Please, may you help me to understand how to cast and then to use that
pointer in these cases?
最简单的不使用强制转换。只需将ptr分配给
正确类型的变量,然后离开即可。
但是,由于这需要扩展到标准C,你可能会/>
想要考虑使用包含每个指针的元素的联合
类型。这意味着您不必回复将
void *转换为函数指针,它可以保持代码非常干净(在一个混乱的联盟中花费的代价) )。
It is easiest not to use a cast. Just assign ptr to a variable of the
right type and off you go.
However, since this requires an extension to standard C, you might
want to consider using a union containing an element for each pointer
type you need. This means you don''t have to reply on converting a
void * to a function pointer and it keeps the code very clean (at the
expense of a messy union).
2)如果我能知道用户关注的路径,那就太好了。对于
示例:
mainmenu -seconditem -firstitem
其中每个实体都是menu_t结构。
你会如何实现这样的动态路径?
2) It would be nice if I can know the path the user is following. For
example:
mainmenu -seconditem -firstitem
where each of these entities are menu_t structs.
How would you implement such a dynamic path?
我会使用堆栈,可能是作为链表实现的。
-
Ben。
I''d use a stack, probably implemented as a linked list.
--
Ben.
Ben Bacarisse ha scritto:
Ben Bacarisse ha scritto:
我不明白为什么你'我有两个。我会像这样合并它们:
typedef struct menu {
char * name [2];
int type ;
int num_param;
union {
void * vp;
struct menu * items;
} ptr;
} menu_t;
菜单中只有一种类型(在C意义上)必须更简单/>
项目,当然可以吗?
I can''t see why you''d have two. I''d merge them like this:
typedef struct menu {
char *name[2];
int type;
int num_param;
union {
void *vp;
struct menu *items;
} ptr;
} menu_t;
It must be simpler to have only one type (in the C sense) for menu
items, surely?
你是对的,这是我的错。我不是那么有经验(还)这种东西
的东西!
You''re right, it''s my fault. I''m not so experienced (yet) in this kind
of thing!
函数指针是特殊的 ;因为你不能保证在一个空格*转换为函数指针(反之亦然)。
Function pointers are "special" in that there is no guarantee in C
that you can convert a void * to function pointer (nor vice versa).
多可惜了! :)
What a pity! :)
您的实现可能会将此作为扩展,但如果您使用
联合,则可以添加函数指针类型并没有任何
可移植性的担忧。
Your implementation may offer this as an extension, but if you use a
union, you can add a function pointer type and not have any
portability worries.
所以,你建议在联盟中为每种类型添加一个指针吗?我需要你这么多吗?不是吗?或者至少,函数指针因为被区别对待
。
So, are you suggesting to add in the union a pointer for each type I
need, aren''t you? Or at least, the function pointer because is treated
differently.
> char * par [] = {" First"," Second"};
>char *par[] = {"First", "Second"};
让我们说ptr设置如下:''ptr = par;''。 ptr然后是伪装的一个
char **,所以简单写作:
char ** strings = ptr;
可以让你访问字符串[0]和字符串[1]。
Let''s say that ptr got set like this: ''ptr = par;''. ptr is then a
char ** in disguise so simply writing:
char **strings = ptr;
would let you access strings[0] and strings[1].
好的,我试图通过演员直接从ptr访问。但它更可以解释你的解决方案。
Ok, I was trying to access directly from ptr with a cast. But it''s more
readable you solution.
最简单的不使用强制转换。只需将ptr分配给
正确类型的变量,然后离开即可。
但是,由于这需要扩展到标准C,你可能会/>
想要考虑使用包含每个指针的元素的联合
类型。这意味着您不必回复将
void *转换为函数指针,它可以保持代码非常干净(在一个混乱的联盟中花费的代价) )。
It is easiest not to use a cast. Just assign ptr to a variable of the
right type and off you go.
However, since this requires an extension to standard C, you might
want to consider using a union containing an element for each pointer
type you need. This means you don''t have to reply on converting a
void * to a function pointer and it keeps the code very clean (at the
expense of a messy union).
好的,这回答了我上面的第一个问题。我读了''type''值并使用
正确的联合条目。其他的将被忽略。
Ok, this answer my first question above. I read the ''type'' value and use
the correct union entry. The others will be ignored.
我会使用堆栈,可能是作为链表实现的。
I''d use a stack, probably implemented as a linked list.
我明白了。
非常感谢您花费的时间!
Marco / iw2nzm
I got it.
Thank you a lot for the time you spent!
Marco / iw2nzm
Ben Bacarisse ha scritto:
Ben Bacarisse ha scritto:
我不明白为什么你会有两个。我会像这样合并它们:
typedef struct menu {
char * name [2];
int type ;
int num_param;
union {
void * vp;
struct menu * items;
} ptr;
} menu_t;
I can''t see why you''d have two. I''d merge them like this:
typedef struct menu {
char *name[2];
int type;
int num_param;
union {
void *vp;
struct menu *items;
} ptr;
} menu_t;
请考虑这个简短的代码:
char mypar [3];
void myfunc();
typedef struct menu {
char * name [2];
int type;
int num_param;
union {
void * vp;
void(* fp)();
struct menu * items;
} ptr;
} menu_t;
好的,现在我想初始化这个结构:
const menu_t mn_mnu = {
{" Main Menu"," Menu"},
MIT_FUNCTION,
0,
{mypar}
};
成功编译。请注意,Dynamic C中的函数指针
声明与standard-C略有不同。
语法是相同的:returntype(* name)();也用于带有
参数的函数。
现在我想初始化第二个(最终是第三个)元素
联盟。我该怎么办?
{,myfunc}
{0,myfunc}
{NULL,myfunc} >
这些都不起作用。编译器大声尖叫很多错误。
我用Google搜索了这个,但我没有找到一个例子。
再见>
Marco / iw2nzm
Please, consider this short code:
char mypar[3];
void myfunc();
typedef struct menu {
char *name[2];
int type;
int num_param;
union {
void *vp;
void (*fp)();
struct menu *items;
} ptr;
} menu_t;
Ok, now I want to initialize this struct:
const menu_t mn_mnu = {
{"Main Menu", "Menu"},
MIT_FUNCTION,
0,
{mypar}
};
It successfully compiles. Please note that the function pointer
declaration in Dynamic C is slightly different from standard-C. The
syntax is the same: returntype (*name)(); also for functions with
parameters.
Now I want to initialize the second (and eventually the third) element
of the union. How should I do?
{, myfunc}
{0, myfunc}
{NULL, myfunc}
None of these works. The compiler screams loud a lot of errors.
I''m googled on this but I didn''t find an example.
Bye
Marco / iw2nzm
这篇关于指针的一些问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!