case label does not reduce to an integer constant

A

adelfino

Hi! I'm pretty new to C and I'am getting an error from GCC 4.0 with the
following function:

unsigned int search_for_clo_tag (const unsigned char *array, const
unsigned int array_element) {

unsigned char clo_tag;
unsigned int i;
unsigned char ope_tags[1];

switch (*(array + array_element)) {

case '(':

clo_tag = ')';

*ope_tags = '[';

*(ope_tags + 1) = '{';

break;

case '[':

clo_tag = ']';

*ope_tags = '(';

*(ope_tags + 1) = '{';

break;

case '{':

clo_tag = '}';

*ope_tags = '(';

*(ope_tags + 1) = '[';

break;

}

for (i = 1; *(array + array_element + i) != '\0'; i++)

switch (*(array + array_element + i)) {

case *(array + array_element): case *ope_tags: case *(ope_tags + 1):

if (!must_be_ignored (array, array_element + i))

i = search_for_clo_tag (array, array_element + i) - array_element;

break;

case clo_tag:

if (!must_be_ignored (array, array_element + i))

return array_element + i;

break;

}

}

The error is: "case label does not reduce to an integer constant" and
its related to the second switch. I don't understand why this happen,
since chars are integers, right?

Well, hope you can help me. Thank you and good luck!

PS: Sorry my English. I'am from Argentina, :p
 
M

Martin Ambuhl

adelfino wrote:

[...]
switch (*(array + array_element + i)) {

case *(array + array_element): case *ope_tags: case *(ope_tags + 1):

none of these cases are integer constants
[...]
case clo_tag:

nor is this one

The error is: "case label does not reduce to an integer constant" and
its related to the second switch. I don't understand why this happen,
since chars are integers, right?

So what? variables are not constants.
 
A

adelfino

Oh, I get it.
Now I will have to figure out how to do something like that without
variables.

Thank you Martin!
 
J

Joe Wright

adelfino said:
Oh, I get it.
Now I will have to figure out how to do something like that without
variables.

Thank you Martin!

I prefer the 'else if' model to the switch cases. Something like this..

if (expr1)
do1();
else if (expr2)
do2();
else if (expr3)
do3();
else
do0(); /* when all else fails */
 
C

Christian Kandeler

adelfino said:
Hi! I'm pretty new to C and I'am getting an error from GCC 4.0 with the
following function:

Another bug unrelated to the switch problem:
unsigned char ope_tags[1];

[ ... ]
*ope_tags = '[';

*(ope_tags + 1) = '{';

You are causing undefined behavior here; for this assignment to work,
ope_tags needs to have at least two elements.


Christian
 
N

Netocrat

adelfino wrote:
Hi! I'm pretty new to C and I'am getting an error from GCC 4.0 with
the following function:

Another bug unrelated to the switch problem:
unsigned char ope_tags[1];

[ ... ]
*ope_tags = '[';

*(ope_tags + 1) = '{';

You are causing undefined behavior here; for this assignment to work,
ope_tags needs to have at least two elements.

But, doesn't ope_tags have two elements?

ope_tags[0] and ope_tags[1]?

Anyway, I split them into two separate unsigned chars, ;)

You didn't quote the message you were replying to - I've added it above.

In answer to your question, no, it doesn't. You declared it with size of
one so it only has one element. You are confusing the declaration of the
array size with its indexing. Indexing is zero-based, so you are right
that you can index ope_tags[0]. Declaring size, though, is not zero-based
so you specify the actual size you require. So if you want to properly
access a second location (ope_tags[1] or rather as you have written it,
the equivalent *(ope_tags + 1)) then you need to declare the array of size
at least 2:

unsigned char ope_tags[2];

In general if you want to index an array at [n] you need to declare the
array to be of size at least n+1.
 
A

adelfino

But, doesn't ope_tags have two elements?

ope_tags[0] and ope_tags[1]?

Anyway, I split them into two separate unsigned chars, ;)
 
C

CBFalconer

adelfino said:
But, doesn't ope_tags have two elements?

ope_tags[0] and ope_tags[1]?

Anyway, I split them into two separate unsigned chars, ;)

How can anyone tell? You don't show any code, especially no
declaration for the "ope_tags" entity.
 
A

adelfino

@Netocrat

OK, now I get it, ;)

@CBFalconer

Yes, I show the code, in my first post:

unsigned char ope_tags[1];

@*
Thanks, and good luck!
 
C

Charles Richmond

adelfino said:
Sorry, I meant I understood what Netocrat said about indexing and
declaring.
But evidently you did *not* understand what Mr. Falconer said about
top posting...


--
+----------------------------------------------------------------+
| Charles and Francis Richmond It is moral cowardice to leave |
| undone what one perceives right |
| richmond at plano dot net to do. -- Confucius |
+----------------------------------------------------------------+
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top