编译时已知可变长度数组的结构 [英] Structures with variable length array known at compile time

查看:86
本文介绍了编译时已知可变长度数组的结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要为我的项目开发一个菜单系统。菜单

将显示在由ARM7驱动的字符LCD显示器上。
微控制器。为此,我希望用C

构建一个结构,其中包含以下内容 -

结构菜单

{

int n(菜单中元素的数量);

char menu_items [20] [q]; (这将包含液晶显示屏上显示的字符串
,20个字符和n行

funcptr fptr;(指向相应菜单功能的指针)

}

数组men​​u_items将始终有20个字符串,但

其中的''q''将与每个菜单屏幕不同。字符串的数量

将在int n中定义。我将使用此结构来实现

const结构,我将使用所有菜单定义屏幕

信息。

从谷歌搜索我发现可变长度数组不能用结构实现
。我还发现结构大小

应该在编译时知道(我不想使用malloc)。我的菜单

项目将在编译时知道。我该如何实现?

谢谢。

Hi, I need to develop a menu system for a project of mine. The menu
will be displayed on a character LCD display driven by ARM7
Microcontroller. For this purpose i wish to construct a structure in C
which will contain a the following -
struct menu
{
int n (no. of elements in menu);
char menu_items[20][q]; (This will contains the strings to be
displayed on the LCD, 20 characters and n lines
funcptr fptr; (Pointer to the corresponding menu function)
}
the array "menu_items" will always have 20 character strings but the
no. of them ''q'' will differ from each menu screen. the no. of strings
will be defined in "int n". I will be using this struct to implement
const structs which i will be defining with all the menu screen
information.
From googling around i found variable length arrays cannot be
implemented with in structures. I also found that the structure size
should be known at compile time(i dont want to use malloc). My menu
items will be known at compile time. how do i implement this?
Thanks.

推荐答案

aravind< ar ******* @ gmail.comwrites:
aravind <ar*******@gmail.comwrites:

我需要为我的项目开发一个菜单系统。
Hi, I need to develop a menu system for a project of mine.



< snip>

<snip>


struct menu

{

int n(菜单中的元素数量);

char menu_items [20] [q]; (这将包含液晶显示屏上显示的字符串
,20个字符和n行

funcptr fptr;(指向相应菜单功能的指针)

}

数组men​​u_items将始终有20个字符串,但

其中的''q''将与每个菜单屏幕不同。字符串的数量

将在int n中定义。
struct menu
{
int n (no. of elements in menu);
char menu_items[20][q]; (This will contains the strings to be
displayed on the LCD, 20 characters and n lines
funcptr fptr; (Pointer to the corresponding menu function)
}
the array "menu_items" will always have 20 character strings but the
no. of them ''q'' will differ from each menu screen. the no. of strings
will be defined in "int n".



这看起来像家庭作业(通常称为课程作业)英国)所以

我不愿意发布代码。我会指出一些事情...


你的阵列错了什么回合 - - 你写的是一个数组
每个20个q字符数组的



如果字符串是20个字符,最好使用21

字符数组并且最后总是有一个空格。总是保持

a正确的字符串。如果内存非常紧,你可以避免这个,但是

编码得多更加繁琐。

This looks like homework (usually called coursework here in the UK) so
I am reluctant to post code. I will point out a couple of things...

You have the array the wrong what round -- what you wrote is an array
of 20 arrays of q characters each.

If the strings are to be 20 characters, it is better to use 21
character arrays and always have a null at the end. I.e. always keep
a proper string. If memory if very tight you can avoid this, but the
coding gets much more fiddly.


我将使用这个结构来实现

const结构,我将用所有菜单屏幕定义

信息。

从谷歌搜索我发现可变长度数组不能用结构实现
。我还发现结构大小

应该在编译时知道(我不想使用malloc)。我的菜单

项目将在编译时知道。我该如何实现?
I will be using this struct to implement
const structs which i will be defining with all the menu screen
information.
From googling around i found variable length arrays cannot be
implemented with in structures. I also found that the structure size
should be known at compile time(i dont want to use malloc). My menu
items will be known at compile time. how do i implement this?



一种方法是使用指向数组的指针。这些有点奇怪,但是你有bt b函数指针,所以数组指针只有一小步......
!单独定义字符串数组,然后指向

时初始化菜单结构。


-

Ben。

One way is to have a pointer to an array. These are a little odd, but
you have function pointers so array pointer are only a small step
away! Define the string array separately and then point to in when
you initialise the menu struct.

--
Ben.


aravind写道:
aravind wrote:

>

我需要开发一个我的项目菜单系统。

菜单将显示在由ARM7驱动的字符LCD显示屏上。
微控制器。为此,我希望在C中构建一个结构

,其中包含以下内容 -
>
Hi, I need to develop a menu system for a project of mine. The
menu will be displayed on a character LCD display driven by ARM7
Microcontroller. For this purpose i wish to construct a structure
in C which will contain a the following -



除此之外,这似乎是做作业,这是关于话题的
。 c.l.c处理C语言,如

各种C标准中所定义。 comp.arch.embedded可能是一个更好的地方,

如果它不是作业。


-

[邮件]:Chuck F(cinefalconer at maineline dot net)

[page]:< http://cbfalconer.home.att.net>

试试下载部分。

Besides the fact that this appears to be homework, it is
off-topic. c.l.c deals with the C language, as defined in the
various C standards. comp.arch.embedded might be a better place,
if it wasn''t homework.

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.att.net>
Try the download section.


CBFalconer< cb ******** @ yahoo.comwrites:
CBFalconer <cb********@yahoo.comwrites:

aravind写道:
aravind wrote:

>>
我需要为我的项目开发一个菜单系统。
菜单将显示在由ARM7
微控制器驱动的字符LCD显示屏上。为此,我希望在C中构建一个结构,它将包含以下内容 -
>>
Hi, I need to develop a menu system for a project of mine. The
menu will be displayed on a character LCD display driven by ARM7
Microcontroller. For this purpose i wish to construct a structure
in C which will contain a the following -



除了这似乎是家庭作业这一事实,它是

偏离主题。 c.l.c处理C语言,如

各种C标准中所定义。 comp.arch.embedded可能是一个更好的地方,

如果它不是功课。


Besides the fact that this appears to be homework, it is
off-topic. c.l.c deals with the C language, as defined in the
various C standards. comp.arch.embedded might be a better place,
if it wasn''t homework.



他正在询问如何最好地设计结构。

信息的其余部分仅仅是另外的信息。并且可能会带头负责

高级程序员更全面地指导他。


如果你想要破坏性和无益,请至少等待

确实发布主题的人。


他的问题很有礼貌,主题是C编程语言。


请努力阅读你很快诋毁的帖子。

He is asking advice on how best to design a structure. The rest of the
information is merely "in addition" and might well lead responsible
senior programmers to guide him more thoroughly.

If you wish to be disruptive and unhelpful please at least wait for
people who do indeed post off topic.

His question was polite and on topic for the C programming language.

Please take the effort to read the posts you are so quick to denigrate.


这篇关于编译时已知可变长度数组的结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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