You are not logged in. | Register | Resend Activation
Browsing this Thread: 1 Anonymous Users


Register To Post




#1
Two issues
Posted on: 7/19 19:57:17
Nintendoid!
Joined 2008/12/28
Slovenia
204 Posts
Highscore Top ScoreHighscore Top ScoreHighscore Top ScoreCoderContributorLong Time User (1 Year)Top10 Poster
Offline
There are two things there are currently bothering me.

First, suppose I have a program like this:

..............................
const char ExampleConst = 123;

main ()
{
char ExampleVar;

ExampleVar = 80;

switch(ExampleVar)
{
case ExampleConst:
// do stuff here
}
}
..............................

When I try to compile it, gccVB gives me a warning: "case label does not reduce to integer constant" or something like that. Why on Earth does this happen?

The second issue is declaring an array as a typedef that consists of several char variables, outside a function, to make it available for all functions. It just won't compile.

Any ideas?
Top

#2
Re: Two issues
Posted on: 7/19 20:17:04
VUE(xpert)
Joined 2003/9/3
Sweden
284 Posts
PVBCC EntryHighscore Top10 2ndHighscore Top ScoreHighscore Top ScoreHighscore Top ScoreHighscore Top ScoreHighscore Top ScoreHighscore Top ScoreHighscore Top ScoreHighscore Top ScoreHighscore Top ScoreHighscore Top ScoreHighscore Top ScoreHighscore Top ScoreHighscore Top ScoreHighscore Top ScoreHighscore Top ScoreHighscore Top ScoreHighscore Top ScoreHighscore Top ScoreHighscore Top ScoreHighscore Top ScoreHighscore Top ScoreHighscore Top ScoreCoderTop10 Poster10+ Game RatingsHOTY09 2ndLong Time User (7 Years) DonatorApp Coder
Offline
Issue 1:
I don't think it likes constants declared like that to be used as case labels... The warning will probably go away if you declare it like "#define ExampleConst 123" instead.

Issue 2:
Dunno, can we see how you tried to declare it?
Top

#3
Re: Two issues
Posted on: 7/20 12:12:53
Nintendoid!
Joined 2008/12/28
Slovenia
204 Posts
Highscore Top ScoreHighscore Top ScoreHighscore Top ScoreCoderContributorLong Time User (1 Year)Top10 Poster
Offline
Quote:
The warning will probably go away if you declare it like "#define ExampleConst 123" instead.


And it did! Thanks!

Quote:
Issue 2:
Dunno, can we see how you tried to declare it?


Like this:


typedef struct
{
  
int x;
  
int y;
  
char a;
  
char b;
  
char c;
  
char d;
ExampleTypedef;

ExampleTypedef ExampleArray[16];

main ()
{
  
// ...
}
Top

#4
Re: Two issues
Posted on: 7/20 17:45:17
VUE(xpert)
Joined 2003/9/3
Sweden
284 Posts
PVBCC EntryHighscore Top10 2ndHighscore Top ScoreHighscore Top ScoreHighscore Top ScoreHighscore Top ScoreHighscore Top ScoreHighscore Top ScoreHighscore Top ScoreHighscore Top ScoreHighscore Top ScoreHighscore Top ScoreHighscore Top ScoreHighscore Top ScoreHighscore Top ScoreHighscore Top ScoreHighscore Top ScoreHighscore Top ScoreHighscore Top ScoreHighscore Top ScoreHighscore Top ScoreHighscore Top ScoreHighscore Top ScoreHighscore Top ScoreCoderTop10 Poster10+ Game RatingsHOTY09 2ndLong Time User (7 Years) DonatorApp Coder
Offline
Hmm, that code works fine for me... What error message do you get?
Top

#5
Re: Two issues
Posted on: 8/7 20:52:31
Nintendoid!
Joined 2008/12/28
Slovenia
204 Posts
Highscore Top ScoreHighscore Top ScoreHighscore Top ScoreCoderContributorLong Time User (1 Year)Top10 Poster
Offline
"warning: variable-size type declared outside of any function"
Top

#6
Re: Two issues
Posted on: 8/8 9:10:52
PVB Elite
Joined 2003/7/25
USA
998 Posts
PVBCC 1stCoderContributor#2 PosterHOTY09 EntryLong Time User (7 Years) App Coder
Offline
Did you try that exact code? I just did a search on that error and came across this: http://bytes.com/topic/c/answers/221491-constants-not-constant , where someone tried to use a const for the size of the array... which won't work. You'd need to use #define for the size.

should work:
ExampleTypedef ExampleArray[5];

should work:
#define arraysz 5
ExampleTypedef ExampleArray[arraysz];

shouldn't work:
const int arraysz=5;
ExampleTypedef ExampleArray[arraysz];

DogP
Visit Project: VB
Top

#7
Re: Two issues
Posted on: 8/8 15:01:46
Nintendoid!
Joined 2008/12/28
Slovenia
204 Posts
Highscore Top ScoreHighscore Top ScoreHighscore Top ScoreCoderContributorLong Time User (1 Year)Top10 Poster
Offline
And it works.

In that case, I have another question: what's the point of having constants in C if they can't be used as constants?
Top

#8
Re: Two issues
Posted on: 8/8 15:41:25
VUE(xpert)
Joined 2003/9/3
Sweden
284 Posts
PVBCC EntryHighscore Top10 2ndHighscore Top ScoreHighscore Top ScoreHighscore Top ScoreHighscore Top ScoreHighscore Top ScoreHighscore Top ScoreHighscore Top ScoreHighscore Top ScoreHighscore Top ScoreHighscore Top ScoreHighscore Top ScoreHighscore Top ScoreHighscore Top ScoreHighscore Top ScoreHighscore Top ScoreHighscore Top ScoreHighscore Top ScoreHighscore Top ScoreHighscore Top ScoreHighscore Top ScoreHighscore Top ScoreHighscore Top ScoreCoderTop10 Poster10+ Game RatingsHOTY09 2ndLong Time User (7 Years) DonatorApp Coder
Offline
They are constants in the sense that you can't change their value after declaration, but you can't use them for declaring other stuff because I guess the compiler can't be sure that they have an assigned value yet.
Top

#9
Re: Two issues
Posted on: 8/8 17:44:56
PVB Elite
Joined 2003/7/26
USA
642 Posts
PVBCC EntryCoderContributorSpecial Achievement#3 Poster10+ Game RatingsHOTY09 EntryLong Time User (7 Years) App Coder
Offline
It helps to think of declaration and definition as two separate things (sometimes written on the same line). Identifiers, with or without the const qualifier, can be declared only once per program (the "extern" qualifier notwithstanding). However, identifiers can be defined multiple times, unless they were declared const.

Just remember that:


const int num = (int*)0x1234;


makes a modifiable pointer (located somewhere in RAM) to a read-only integer (which is located at the address 0x1234) and:


int 
* const num = (int*)0x1234;


would make a non-modifiable pointer (hopefully in ROM, in the case of the VB) that points to a writable integer (located at the address 0x1234).

Also, the compiler should store "const" data/pointers in, and access them from, the ROM, thus saving space in the WRAM area for things that can actually be written.

At least I think that's how it works, anyway. If it doesn't, it should

What I don't get is why the following:


int 
* const num = (int*)0x1234;
int num2;

void main (void) {
   
num2 num;
   return;
}


Makes the compiler spit out: "warning: assignment discards qualifiers from pointer target type".

"const" here isn't a qualifier of the target type, but of the pointer's type.

And besides that, if it's just the pointer that's a const, then what difference does it make that I'm assigning the address contained in that const pointer to another pointer? They're both pointers to writable memory, so there's no chance of breaking something by ignoring the const.

Anyone want to enlighten me?
No other word than the Word of God -- Deuteronomy 4:2; 12:32; Proverbs 30:6; Galatians 1:8-9; Revelation 22:18-19
No other god than the God of the Bible -- Deuteronomy 8:19; 13:1-8; Joshua 24:14-15; Matthew 6:24; Acts 17:22-31
Top



Register To Post