struct数组的动态声明 [英] dynamic declaration of struct array

查看:65
本文介绍了struct数组的动态声明的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我这样做,为什么不会声明结构数组的工作:


1 typedef struct

2 {

3个钢笔;

4个铅笔;

5}文具;

6


.........


7 int main(无效)

8 {

9 int num;


........... num在代码中定义........


10

11 Stationers stats [num];

12}


错误消息显示需要常量表达式并突出显示

第11行。我想在运行时动态声明它,可以

有人解释如何做到这一点。

Why won''t the declaration of a struct array work if I do it like this:

1 typedef struct
2 {
3 int pens;
4 int pencils;
5 } Stationers;
6

.........

7 int main(void)
8 {
9 int num;

...........num defined in code........

10
11 Stationers stats[num];
12 }

The error message says "constant expression required" and highlights
line 11. I want to declare it dynamically during runtime, could
someone explain how to do this please.

推荐答案

Tim写道:

Stationers stats [num];
Tim wrote:
Stationers stats[num];
12}

错误信息显示为 ;需要持续表达并突出显示
第11行。我想在运行时动态声明它,可能会有人解释如何做到这一点。
12 }

The error message says "constant expression required" and highlights
line 11. I want to declare it dynamically during runtime, could
someone explain how to do this please.




Stationers * stats =(文具*)malloc(num * sizeof(文具));


NR



Stationers *stats = (Stationers*)malloc(num * sizeof(Stationers));

NR


" Tim" ; <喜********** @ yahoo.co.uk>在消息中写道

news:ad ************************** @ posting.google.c om ...
"Tim" <hi**********@yahoo.co.uk> wrote in message
news:ad**************************@posting.google.c om...
为什么我不希望结构数组的声明如果我这样做:
.... 7 int main(void)
8 {
9 int num;

........... num在代码中定义........

10
11 Stationers stats [num];
12}

错误消息显示需要常量表达式并突出显示
第11行。我想在运行时动态声明它,可能会有人解释如何执行此操作。
Why won''t the declaration of a struct array work if I do it like this: .... 7 int main(void)
8 {
9 int num;

...........num defined in code........

10
11 Stationers stats[num];
12 }

The error message says "constant expression required" and highlights
line 11. I want to declare it dynamically during runtime, could
someone explain how to do this please.




此代码为如果你的编译器实现了1999

C标准,那么它实际上是有效的。在此之前,确实需要数组变量

在编译时已知大小。


向后兼容的方式来做你想要的是使用:

Stationers * stats =(Stationers *)malloc(num * sizeof(Stationers));

...使用stats就好像它是一个大小的数组num ...

免费(统计); //在退出之前不要忘记这个!

希望这会有所帮助,

伊万

-
http://ivan.vecerina.com




This code is actually valid if your compiler implements the 1999
C standard. Prior to that, array variables are indeed required
to be of a size known at compile time.

The backwards-compatible way to do what you want is to use:
Stationers* stats = (Stationers*)malloc(num*sizeof(Stationers));
... use stats as if it were an array of size num ...
free(stats); // don''t forget this prior to exit !
Hope this helps,
Ivan
--
http://ivan.vecerina.com



在文章< ad ************************** @ posting.google.com>,

Tim< hi ********** @ yahoo.co.uk>写道:
In article <ad**************************@posting.google.com >,
Tim <hi**********@yahoo.co.uk> wrote:
如果我这样做,为什么不希望结构数组的工作:


[snip struct definition]

7 int main(无效)
8 {
9 int num;

........... num在代码中定义... .................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................并突出显示
第11行。我想在运行时动态声明它,可能会有人解释如何做到这一点。
Why won''t the declaration of a struct array work if I do it like this:
[snip struct definition]
7 int main(void)
8 {
9 int num;

...........num defined in code........

10
11 Stationers stats[num];
12 }

The error message says "constant expression required" and highlights
line 11. I want to declare it dynamically during runtime, could
someone explain how to do this please.




嗯...你想用什么语言编写这段代码?


如果你使用C99,你在这里应该有什么用,因为数组是

允许长度在运行时才知道。


如果您使用C90,则不允许声明新变量(如
所以一旦填写空白,你的代码仍然会被打破,无论

如何声明数组。相反,你需要在块的开头声明一个指针

,一旦你决定了你需要多少元素

,就使用malloc来为它分配内存:

--------

#include< stdlib.h>


/ *在此定义并输入定义文具结构* /


int main(无效)

{

int num;

保存者*统计数据;


/ *在此确定num的值* /


stats = malloc(num * sizeof * stats);


/ *我假设你想在这里做一些统计数据* /


免费(统计) ;

}

--------


如果您正在使用C ++(也允许声明)在非
声明之后),转到comp.lang.c ++并询问(或者,更好地,打开一个
教科书并阅读)new []和std: :vector。

dave


-

Dave Vandervies dj ****** @ csclub.uwaterloo.ca

So" Patterns"实际上只是面向对象编程的另一个名称,

这实际上只是结构化编程的另一个名称,它实际上是

好黑客。 --Richard Bos in comp.lang.c



Uhmm... what language are you trying to write this code in?

If you''re using C99, what you have here should work, since arrays are
allowed to have lengths that aren''t known until run-time.

If you''re using C90, you''re not allowed to declare a new variable (like
the array) after non-declaration stuff (like assigning a value to num),
so once you fill in the blanks your code will still be broken no matter
how you declare the array. Instead, you''d need to declare a pointer at
the beginning of the block and, once you''ve decided how many elements
you want, use malloc to allocate memory for it:
--------
#include <stdlib.h>

/*Define and typedef the Stationers struct here*/

int main(void)
{
int num;
Stationers *stats;

/*Decide on a value for num here*/

stats=malloc(num * sizeof *stats);

/*I assume you''ll want to do something with stats here*/

free(stats);
}
--------

If you''re using C++ (which also allows declarations after non-
declarations), go to comp.lang.c++ and ask about (or, better, open a
textbook and read about) new[] and std::vector.
dave

--
Dave Vandervies dj******@csclub.uwaterloo.ca
So "Patterns" are really just another name for "Object-oriented programming",
which is really just another name for "Structured programming", which is really
just big words for "good hacking". --Richard Bos in comp.lang.c


这篇关于struct数组的动态声明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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