Original Post

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;

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?

8 Replies

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? πŸ˜›

The warning will probably go away if you declare it like “#define ExampleConst 123” instead.

And it did! Thanks!

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 ()
  // ...

Hmm, that code works fine for me… What error message do you get?

“warning: variable-size type declared outside of any function”

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];


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?

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.

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;

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? πŸ˜€


Write a reply

You must be logged in to reply to this topic.