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

Discussion in 'C Programming' started by PB, Mar 15, 2006.

  1. PB

    PB Guest

    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 *************/
    PB, Mar 15, 2006
    #1
    1. Advertising

  2. PB

    Albert Guest

    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?
    Albert, Mar 15, 2006
    #2
    1. Advertising

  3. PB

    Jordan Abel Guest

    On 2006-03-15, Albert <> wrote:
    > 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?


    int, what else?

    [hey, you don't say "a 'long' what", do you?]
    Jordan Abel, Mar 15, 2006
    #3
  4. On Wednesday 15 March 2006 06:13, Jordan Abel opined (in
    <>):

    > On 2006-03-15, Albert <> wrote:
    >> 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?

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


    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"
    Vladimir S. Oka, Mar 15, 2006
    #4
  5. Re: Why do i get error "error: expression must have a constantvalue"

    "Albert" <> writes:
    > 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?


    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_Keith) <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.
    Keith Thompson, Mar 15, 2006
    #5
  6. PB

    PB Guest

    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
    PB, Mar 15, 2006
    #6
  7. On Wednesday 15 March 2006 07:31, PB opined (in
    <>):

    > 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


    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.
    Vladimir S. Oka, Mar 15, 2006
    #7
  8. PB

    PB Guest

    Vladimir S. Oka wrote:
    > On Wednesday 15 March 2006 07:31, PB opined (in
    > <>):
    >
    > > 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

    >
    > 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.
    >


    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 "&".

    > --
    > BR, Vladimir
    >
    > In 1750 Issac Newton became discouraged when he fell up a flight of
    > stairs.
    PB, Mar 15, 2006
    #8
  9. On 2006-03-15, PB <> wrote:
    > 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 "&".


    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)
    David Holland, Mar 16, 2006
    #9
  10. PB

    Robin Haigh Guest

    "PB" <> wrote in message
    news:...
    >
    > Vladimir S. Oka wrote:
    > > On Wednesday 15 March 2006 07:31, PB opined (in
    > > <>):
    > >
    > > > 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

    > >
    > > 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.
    > >

    >
    > 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 "&".


    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
    Robin Haigh, Mar 17, 2006
    #10
  11. On 14 Mar 2006 19:13:45 -0800, "PB" <> wrote:

    > 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


    Right. In general this is and only can be determined at link time.

    > 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.
    >

    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.net
    Dave Thompson, Mar 27, 2006
    #11
    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. =?Utf-8?B?VGltOjouLg==?=

    Why, why, why???

    =?Utf-8?B?VGltOjouLg==?=, Jan 27, 2005, in forum: ASP .Net
    Replies:
    6
    Views:
    565
    Juan T. Llibre
    Jan 27, 2005
  2. Horace Nunley

    why why why does function not work

    Horace Nunley, Sep 27, 2006, in forum: ASP .Net
    Replies:
    1
    Views:
    457
    =?Utf-8?B?UGV0ZXIgQnJvbWJlcmcgW0MjIE1WUF0=?=
    Sep 27, 2006
  3. Mr. SweatyFinger

    VWD why why why

    Mr. SweatyFinger, Nov 28, 2006, in forum: ASP .Net
    Replies:
    1
    Views:
    383
    =?Utf-8?B?Q2lhcmFuIE8nJycnRG9ubmVsbA==?=
    Dec 21, 2006
  4. Mr. SweatyFinger

    why why why why why

    Mr. SweatyFinger, Nov 28, 2006, in forum: ASP .Net
    Replies:
    4
    Views:
    878
    Mark Rae
    Dec 21, 2006
  5. Mr. SweatyFinger
    Replies:
    2
    Views:
    1,840
    Smokey Grindel
    Dec 2, 2006
Loading...

Share This Page