English 中文(简体)
我们能够将“标准”固定在“结构宣言”中作为阵容大小?
原标题:can we use #define constant in struct declaration as array size?

I am doing the following in C

#define MAX_DATA_SIZE 500;

struct reliable_state {

  char dataBuffer[MAX_DATA_SIZE]; 

}

i.e I want to use the #define constant as array size in structure declaration. But above code gives weird error

.c:36: error: expected ‘]’ before ‘;’ token

So is there any other way to do this?

最佳回答

Yes you can, just remove ; in your define line:

#define MAX_DATA_SIZE 500

如果界定你有汇编者,那么你的结构定义实际上将被视为你的结构定义。

char dataBuffer[500;];

which is clearly erroneous.

问题回答

当你使用<代码>#define时,右边的宏观定义是“......”。 比如,你刚才必须纠正这种情况。

#define MAX_DATA_SIZE 500  /* no semicolon */

The syntax for a non-empty object-like macro definitions is

#define MACRO_IDENTIFIER    REPLACEMENT

请注意,与C类声明和声明不同,这一辛迪加没有消灭半殖民地。 你们的半殖民地成为REPLACE的一部分,在你们使用宏观识别标志、产生结果的地方被插入。

char dataBuffer[500;];

这是编辑诊断出的一起错。





相关问题
Fastest method for running a binary search on a file in C?

For example, let s say I want to find a particular word or number in a file. The contents are in sorted order (obviously). Since I want to run a binary search on the file, it seems like a real waste ...

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

Tips for debugging a made-for-linux application on windows?

I m trying to find the source of a bug I have found in an open-source application. I have managed to get a build up and running on my Windows machine, but I m having trouble finding the spot in the ...

Trying to split by two delimiters and it doesn t work - C

I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...

Good, free, easy-to-use C graphics libraries? [closed]

I was wondering if there were any good free graphics libraries for C that are easy to use? It s for plotting 2d and 3d graphs and then saving to a file. It s on a Linux system and there s no gnuplot ...

Encoding, decoding an integer to a char array

Please note that this is not homework and i did search before starting this new thread. I got Store an int in a char array? I was looking for an answer but didn t get any satisfactory answer in the ...

热门标签