Why do i get error "error: expression must have a constant value"

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • PB

    Why do i get error "error: expression must have a constant value"

    Hi !

    I have the following code, which I am using in an Embedded systems,
    c-compiler.. However I see the same problem with GCC too..

    I need the last 10 bits of an address pointer, which is completly know
    at link time, but will be symbols at the compile time. I used the
    following code for this initalization..
    uint32 data_ptr_mask = ((uint32) data) & 0x3ff;

    for illustration purpose, I have also included
    uint32 data_ptr_off = ((uint32 ) data) +0x3ff;

    When I compile this code (see the example routine below) I get the
    following error with gcc
    "error: expression must have a constant value"

    Can somebody tell me why the bit operators, "*" and "/" don't work
    while "+" and "-" do.

    TIA

    -P.B. Srinivas
    /****** Start of CODE ***/
    #include <stdio.h>

    typedef int int32;
    typedef unsigned uint32;

    /* Global variable declarations **/
    int32 data[64];

    /** this works **/
    uint32 data_ptr = (uint32 ) data;

    /** this also works **/
    uint32 data_ptr_off = ((uint32 ) data) +0x3ff;

    /** this doesn't work , only +/- operators **/
    /** seem to work, why is this ? ****/
    uint32 data_ptr_mask = ((const uint32) data) & 0x3ff;

    int main()
    {
    printf("Pointer Addition %x %x\n",data_ptr, data_ptr_off);
    printf("Pointer Masking %x %x\n",data_ptr, data_ptr_mask);

    }
    /*********** End of code *************/

  • Albert

    #2
    Re: Why do i get error &quot;error: expression must have a constant value&quot;

    In your typedef second typedef statement you have not specified the
    built in-type's identifier name. You have only specified the modifier
    for that data type.

    In other words, an 'unsigned' what?

    Comment

    • Jordan Abel

      #3
      Re: Why do i get error &quot;error: expression must have a constant value&quot;

      On 2006-03-15, Albert <albert.xtheunk nown0@gmail.com > wrote:[color=blue]
      > In your typedef second typedef statement you have not specified the
      > built in-type's identifier name. You have only specified the modifier
      > for that data type.
      >
      > In other words, an 'unsigned' what?[/color]

      int, what else?

      [hey, you don't say "a 'long' what", do you?]

      Comment

      • Vladimir S. Oka

        #4
        Re: Why do i get error &quot;error: expression must have a constant value&quot;

        On Wednesday 15 March 2006 06:13, Jordan Abel opined (in
        <slrne1fcbh.oi1 .random832@rand om.yi.org>):
        [color=blue]
        > On 2006-03-15, Albert <albert.xtheunk nown0@gmail.com > wrote:[color=green]
        >> In your typedef second typedef statement you have not specified the
        >> built in-type's identifier name. You have only specified the modifier
        >> for that data type.
        >>
        >> In other words, an 'unsigned' what?[/color]
        >
        > int, what else?
        >
        > [hey, you don't say "a 'long' what", do you?][/color]

        He must have forgot what he was referring to as he did not include any
        context. ;-)

        --
        BR, Vladimir

        Murder is always a mistake -- one should never do anything one cannot
        talk about after dinner.
        -- Oscar Wilde, "The Picture of Dorian Gray"

        Comment

        • Keith Thompson

          #5
          Re: Why do i get error &quot;error: expression must have a constantvalue&q uot;

          "Albert" <albert.xtheunk nown0@gmail.com > writes:[color=blue]
          > In your typedef second typedef statement you have not specified the
          > built in-type's identifier name. You have only specified the modifier
          > for that data type.
          >
          > In other words, an 'unsigned' what?[/color]

          Please provide context. Read <http://cfaj.freeshell. org/google/> to
          understand how and why.

          The type "unsigned int" can also be referred to as "unsigned".

          The declaration you're complaining about is:

          typedef unsigned uint32;

          which is perfectly legal. (It does implictly assume that unsigned int
          is 32 bits, but that's a different issue.)

          --
          Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
          San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
          We must do something. This is something. Therefore, we must do this.

          Comment

          • PB

            #6
            Re: Why do i get error &quot;error: expression must have a constant value&quot;

            sorry it is supposed to be unsigned int.. the compiler automatically
            converts unsigned to unsigned int..

            I see the same error even if I put "int" after unsigned

            Comment

            • Vladimir S. Oka

              #7
              Re: Why do i get error &quot;error: expression must have a constant value&quot;

              On Wednesday 15 March 2006 07:31, PB opined (in
              <1142407903.569 124.95520@j33g2 000cwa.googlegr oups.com>):
              [color=blue]
              > sorry it is supposed to be unsigned int.. the compiler automatically
              > converts unsigned to unsigned int..
              >
              > I see the same error even if I put "int" after unsigned[/color]

              Please provide context. See <http://cfaj.freeshell. org/google/>. This
              one <http://clc-wiki.net/wiki/Introduction_to _comp.lang.c> is also
              useful.

              As others have pointed out, `unsigned` is exactly the same as `unsigned
              int`. There's no error on your part there. Your problem is using
              non-constant initialisers for your variables. I'd recommend re-writing
              your code to avoid this. Consider moving such stuff into a function.

              --
              BR, Vladimir

              In 1750 Issac Newton became discouraged when he fell up a flight of
              stairs.

              Comment

              • PB

                #8
                Re: Why do i get error &quot;error: expression must have a constant value&quot;


                Vladimir S. Oka wrote:[color=blue]
                > On Wednesday 15 March 2006 07:31, PB opined (in
                > <1142407903.569 124.95520@j33g2 000cwa.googlegr oups.com>):
                >[color=green]
                > > sorry it is supposed to be unsigned int.. the compiler automatically
                > > converts unsigned to unsigned int..
                > >
                > > I see the same error even if I put "int" after unsigned[/color]
                >
                > Please provide context. See <http://cfaj.freeshell. org/google/>. This
                > one <http://clc-wiki.net/wiki/Introduction_to _comp.lang.c> is also
                > useful.
                >
                > As others have pointed out, `unsigned` is exactly the same as `unsigned
                > int`. There's no error on your part there. Your problem is using
                > non-constant initialisers for your variables. I'd recommend re-writing
                > your code to avoid this. Consider moving such stuff into a function.
                >[/color]

                thanks for all the replies.. since I am working on a embedded system, i
                really don't like putting in a function for initialization, as it is
                take up code memory.. I have only posted an example of the problem, i
                have dozen or more of these initilization "macros' in the code..

                I find it difficult to understand why the compiler/linker cant do this
                initialization, as these are really not "non-constant" but fixed
                initializers that are resolved at compile time.. also if the "+"
                operator works why not "&".
                [color=blue]
                > --
                > BR, Vladimir
                >
                > In 1750 Issac Newton became discouraged when he fell up a flight of
                > stairs.[/color]

                Comment

                • David Holland

                  #9
                  Re: Why do i get error &quot;error: expression must have a constant value&quot;

                  On 2006-03-15, PB <pbsriniv@gmail .com> wrote:[color=blue]
                  > thanks for all the replies.. since I am working on a embedded system, i
                  > really don't like putting in a function for initialization, as it is
                  > take up code memory.. I have only posted an example of the problem, i
                  > have dozen or more of these initilization "macros' in the code..
                  >
                  > I find it difficult to understand why the compiler/linker cant do this
                  > initialization, as these are really not "non-constant" but fixed
                  > initializers that are resolved at compile time.. also if the "+"
                  > operator works why not "&".[/color]

                  It might be able to, but the C standard says it doesn't have to and/or
                  shouldn't.

                  Don't forget that you can write a program (to run on your development
                  machine) to generate a source file that has all the precomputed
                  initialization you want. Then you can compile this source file and
                  link it into the program that runs on your embedded target.

                  Just be careful to make sure this program does these computations the
                  same way your target does, or you can end up with subtle and bizarre
                  problems.

                  --
                  - David A. Holland
                  (the above address works if unscrambled but isn't checked often)

                  Comment

                  • Robin Haigh

                    #10
                    Re: Why do i get error &quot;error: expression must have a constant value&quot;


                    "PB" <pbsriniv@gmail .com> wrote in message
                    news:1142409381 .867570.270310@ p10g2000cwp.goo glegroups.com.. .[color=blue]
                    >
                    > Vladimir S. Oka wrote:[color=green]
                    > > On Wednesday 15 March 2006 07:31, PB opined (in
                    > > <1142407903.569 124.95520@j33g2 000cwa.googlegr oups.com>):
                    > >[color=darkred]
                    > > > sorry it is supposed to be unsigned int.. the compiler automatically
                    > > > converts unsigned to unsigned int..
                    > > >
                    > > > I see the same error even if I put "int" after unsigned[/color]
                    > >
                    > > Please provide context. See <http://cfaj.freeshell. org/google/>. This
                    > > one <http://clc-wiki.net/wiki/Introduction_to _comp.lang.c> is also
                    > > useful.
                    > >
                    > > As others have pointed out, `unsigned` is exactly the same as `unsigned
                    > > int`. There's no error on your part there. Your problem is using
                    > > non-constant initialisers for your variables. I'd recommend re-writing
                    > > your code to avoid this. Consider moving such stuff into a function.
                    > >[/color]
                    >
                    > thanks for all the replies.. since I am working on a embedded system, i
                    > really don't like putting in a function for initialization, as it is
                    > take up code memory.. I have only posted an example of the problem, i
                    > have dozen or more of these initilization "macros' in the code..
                    >
                    > I find it difficult to understand why the compiler/linker cant do this
                    > initialization, as these are really not "non-constant" but fixed
                    > initializers that are resolved at compile time.. also if the "+"
                    > operator works why not "&".[/color]

                    Because as you said originally, the array address is a symbol at compile
                    time, so address-based initializers have to be resolved in the linker.

                    The linker is language-independent, it doesn't know C, it can't evaluate C
                    expressions according to C rules. It can add offsets to addresses. So, in
                    C, there are different kinds of constant expression. An arithmetic constant
                    expression is computed in the compiler, it can do most kinds of operations,
                    but the operands have to be available to the compiler as numbers. An
                    address constant can be computed in the linker, can have an address as an
                    operand, but all you can do with it is add a constant offset.

                    Casting a pointer to an integer type isn't supported by the standard in any
                    type of static initializer. Your compilers let you get away with it only
                    when the expression is similar to a legal address constant and they can
                    compile it into something linkable.

                    --
                    RSH



                    Comment

                    • Dave Thompson

                      #11
                      Re: Why do i get error &quot;error: expression must have a constant value&quot;

                      On 14 Mar 2006 19:13:45 -0800, "PB" <pbsriniv@gmail .com> wrote:
                      [color=blue]
                      > Hi !
                      >
                      > I have the following code, which I am using in an Embedded systems,
                      > c-compiler.. However I see the same problem with GCC too..
                      >
                      > I need the last 10 bits of an address pointer, which is completly know
                      > at link time, but will be symbols at the compile time. I used the[/color]

                      Right. In general this is and only can be determined at link time.
                      [color=blue]
                      > following code for this initalization..
                      > uint32 data_ptr_mask = ((uint32) data) & 0x3ff;
                      >
                      > for illustration purpose, I have also included
                      > uint32 data_ptr_off = ((uint32 ) data) +0x3ff;
                      >
                      > When I compile this code (see the example routine below) I get the
                      > following error with gcc
                      > "error: expression must have a constant value"
                      >
                      > Can somebody tell me why the bit operators, "*" and "/" don't work
                      > while "+" and "-" do.
                      >[/color]
                      Because 6.6p7 allows "an address constant for an object type plus or
                      minus an integer constant" <OT> in turn because linkers traditionally
                      support that but not portably other operations like your bitwise. </>
                      (Actually what you wrote isn't strictly an address constant but rather
                      the conversion of an address constant; however, on most systems,
                      including the ones gcc supports, this is effectively an identity.)

                      <OT> Depending on your linker, you may be able to cause (all or
                      specific) data to aligned at a 10-bit boundary, or even placed at a
                      fixed location; in either case the offset is known in advance and you
                      can compile it as a constant. </> Otherwise you'll have to do this
                      computation at run time, probably early in your initialization code.

                      - David.Thompson1 at worldnet.att.ne t

                      Comment

                      Working...