case label does not reduce to an integer constant

Discussion in 'C Programming' started by adelfino, Jul 3, 2005.

  1. adelfino

    adelfino Guest

    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
     
    adelfino, Jul 3, 2005
    #1
    1. Advertising

  2. 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.
     
    Martin Ambuhl, Jul 3, 2005
    #2
    1. Advertising

  3. adelfino

    adelfino Guest

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

    Thank you Martin!
     
    adelfino, Jul 3, 2005
    #3
  4. adelfino

    Joe Wright Guest

    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 ---
     
    Joe Wright, Jul 3, 2005
    #4
  5. 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
     
    Christian Kandeler, Jul 5, 2005
    #5
  6. adelfino

    Netocrat Guest

    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.
     
    Netocrat, Jul 6, 2005
    #6
  7. adelfino

    adelfino Guest

    But, doesn't ope_tags have two elements?

    ope_tags[0] and ope_tags[1]?

    Anyway, I split them into two separate unsigned chars, ;)
     
    adelfino, Jul 6, 2005
    #7
  8. adelfino

    CBFalconer Guest

    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
     
    CBFalconer, Jul 6, 2005
    #8
  9. adelfino

    adelfino Guest

    @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!
     
    adelfino, Jul 7, 2005
    #9
  10. adelfino

    CBFalconer Guest

    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
     
    CBFalconer, Jul 7, 2005
    #10
  11. adelfino

    adelfino Guest

    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
     
    adelfino, Jul 7, 2005
    #11
  12. 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 |
    +----------------------------------------------------------------+
     
    Charles Richmond, Jul 7, 2005
    #12
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Remco Poelstra
    Replies:
    5
    Views:
    925
    Peter Shaggy Haywood
    Jul 4, 2003
  2. pete
    Replies:
    2
    Views:
    1,491
  3. SysSpider
    Replies:
    29
    Views:
    12,999
    Mark McIntyre
    Dec 31, 2004
  4. Pradeep
    Replies:
    0
    Views:
    1,563
    Pradeep
    Apr 12, 2008
  5. Knut Krueger
    Replies:
    8
    Views:
    294
    Knut Krueger
    Aug 20, 2013
Loading...

Share This Page