473,397 Members | 1,969 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

"initializer element is not constant"

2
I'm trying to define a bunch of consant global variables in C, but I'm getting a "initializer element is not constant" error I think because I'm trying to define one in terms of the other.

The code is simply:

int MINFREQ = 200;
int MAXFREQ = 700;
int JND = 5; /* just noticable difference in Hz */
int SEGMENTS = (MAXFREQ-MINFREQ)/JND;
Similarly, I can't seem to define an array using any global variables.
So when I try:
int SEGMENTS = 100;
struct sVowel {
int iF[SEGMENTS];
char sIPA[5];
};
I get:
error: variable-size type declared outside of any functio
Is there a way around this?
I tried defining the first two as constants but that didn't seem to work.

Thanks,
M.E.
Jan 24 '07 #1
3 3324
Banfa
9,065 Expert Mod 8TB
I assume you are using C and not C++ ?

Anyway in both these cases in C marking something as const does really achieve anything. These are not situations where you can use a variable, global variable initialisation or array size definition.

About your only choice is to use #define instead

Expand|Select|Wrap|Line Numbers
  1. #define MINFREQ     200
  2. #define MAXFREQ     700
  3. #define JND         5 /* just noticable difference in Hz */
  4. int SEGMENTS = (MAXFREQ-MINFREQ)/JND;
  5.  
Expand|Select|Wrap|Line Numbers
  1. #define SEGMENTS 100
  2.  
  3. struct sVowel {
  4.     int iF[SEGMENTS];
  5.     char sIPA[5];
  6. };
  7.  
Jan 25 '07 #2
mettle
2
Thank you for your help.

Just out of curiosity, how does C know whether MINFREQ is an int or a double?
If I had defined it was 200.0 would it be a double instead of an int?


I assume you are using C and not C++ ?

Anyway in both these cases in C marking something as const does really achieve anything. These are not situations where you can use a variable, global variable initialisation or array size definition.

About your only choice is to use #define instead

Expand|Select|Wrap|Line Numbers
  1. #define MINFREQ     200
  2. #define MAXFREQ     700
  3. #define JND         5 /* just noticable difference in Hz */
  4. int SEGMENTS = (MAXFREQ-MINFREQ)/JND;
  5.  
Expand|Select|Wrap|Line Numbers
  1. #define SEGMENTS 100
  2.  
  3. struct sVowel {
  4.     int iF[SEGMENTS];
  5.     char sIPA[5];
  6. };
  7.  
Jan 25 '07 #3
Banfa
9,065 Expert Mod 8TB
Just out of curiosity, how does C know whether MINFREQ is an int or a double?
If I had defined it was 200.0 would it be a double instead of an int?
Yes (to the second question).

basically if you put in a decimal point if is a double, otherwise it is an int. You can modify the type of a constant using the letters U (unsigned), L (long) and F (FLOAT) so

Expand|Select|Wrap|Line Numbers
  1. 200         // int constant
  2. 200U        // unsigned int constant
  3. 200L        // long constant
  4. 200UL       // unsigned long constant
  5. 200.0       // double constant
  6. 200.0F      // float constant
  7.  
Jan 25 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: ReignMan | last post by:
I'm running JDK 1.4.0_01, Xalan2.5.1 and Tomcat 4.1.27. XSL transformations work, yet when running in debug mode (Eclipse IDE), I get the following: 990S2html.xsl:3:80: Element type...
2
by: Martin Magnusson | last post by:
It seems that the following code is not valid, but I need to find a workaround. Both gcc and Comeau tells me that j can't be used as a template argument, even though it's declared as a const...
2
by: Todd Nathan | last post by:
Hi. have this code and compiler problem. GCC 2.95.3, BeOS, error "initializer element is not constant" #ifdef FILEIO { static struct { char *sfn; FILE *sfd; } stdfiles = {
9
by: Player | last post by:
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Hello all. I am in the process of teaching myself C# and I think I am doing OK. I have learnt how to how to call the right constructor of a...
1
by: emin.martinian | last post by:
When trying to compile python extensions written in C using "python setup.py build" on cygwin I get the following error: foo.c: initializer element is not constant foo.c: error: (near...
1
by: tankbattle | last post by:
That is, what's the difference between <complexType name="Address" final="restriction"> <sequence> <element name="name" type="string"/> <element name="street" type="string"/> <element...
4
by: jaime | last post by:
Hi again all. Given the line: const int x=5; Can I then use "x" as a constant expression? (By "constant expression", I mean "constant expression" as defined in the C99 standard) I've been...
2
by: hankypan1 | last post by:
Hi All, I need a tree data structure for my application. It is the non - cyclic simple tree where i can have any number of children node and each child can recursively become a sub tree like a...
13
by: bobg.hahc | last post by:
running access 2k; And before anything else is said - "Yes, Virginia, I know you can NOT use a variable to set a constant (that's why it's constant)". BUT - my problem is - I want a constant,...
3
by: zoeb | last post by:
Hi, I am declaring an array which carries x coordinates, however these will vary depending on the geometry the user enters. I have set my code up as follows, but get the "initializer element...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.