468,315 Members | 1,400 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,315 developers. It's quick & easy.

case label does not reduce to an integer constant

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

Nov 15 '05 #1
11 16741
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.
Nov 15 '05 #2
Oh, I get it.
Now I will have to figure out how to do something like that without
variables.

Thank you Martin!

Nov 15 '05 #3
adelfino wrote:
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 */

--
Joe Wright
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Nov 15 '05 #4
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.
Christian
Nov 15 '05 #5
But, doesn't ope_tags have two elements?

ope_tags[0] and ope_tags[1]?

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

Nov 15 '05 #6
On Wed, 06 Jul 2005 07:50:40 -0700, adelfino wrote:
On Tue, 05 Jul 2005 10:07:44 +0200, Christian Kandeler wrote:
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.

Nov 15 '05 #7
adelfino wrote:

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.

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
Nov 15 '05 #8
@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!

Nov 15 '05 #9
adelfino wrote:

@Netocrat

OK, now I get it, ;)


No you don't. You still are failing to quote adequate context.
See my sig below.

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
Nov 15 '05 #10
Sorry, I meant I understood what Netocrat said about indexing and
declaring.

CBFalconer wrote:
adelfino wrote:

@Netocrat

OK, now I get it, ;)


No you don't. You still are failing to quote adequate context.
See my sig below.

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson


Nov 15 '05 #11
adelfino wrote:

CBFalconer wrote:
adelfino wrote:

@Netocrat

OK, now I get it, ;)


No you don't. You still are failing to quote adequate context.
See my sig below.

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson


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 |
+----------------------------------------------------------------+
Nov 15 '05 #12

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

8 posts views Thread by JKop | last post: by
3 posts views Thread by Martin Magnusson | last post: by
3 posts views Thread by qianz99 | last post: by
11 posts views Thread by quakewang | last post: by
5 posts views Thread by Rex Mottram | last post: by
reply views Thread by NPC403 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.