C compilation problem

Discussion in 'C Programming' started by ashu, Feb 27, 2011.

  1. ashu

    ashu Guest

    dear all,

    I am facing a compilation problem for my C code on a 8 bit micro
    controller (Motorola HC11).

    I use cosmic software's compiler which allows me to directly refrence
    addresses, using the @ variable.

    *****************************************************************************************************************
    So the undermentioned C statement,

    unsigned char portA @ 0xD300;

    will enable me to use portA to access the memory location D300.

    I can now read and write to the memory location D300 using simple C
    statements like

    portA= 10; (will write 10 at location D300 refrenced by portA)

    similarly to read i can say....

    c= portA; (this will copy the value inside memory location D300
    referenced by variable portA to c).
    *********************************************************************************************************************

    Now the problem starts...
    I made a declaration like this,

    ********************************************************************************************
    unsigned char register_data @ 0x00FE; /* declare a register whose
    address is 00FE*/
    unsigned char *ptr_db;
    unsigned char ram_packet_header @ 0xD300; /* declare a memory address,
    0xD300*/
    ptr_db = &ram_packet_header; /* Now ptr_db contains the address
    D300*/

    untill here therr is no problem and the code compiles fine.

    Now I want to store the content of ptr_db( which is 0xD300, an
    address) in the variable register_data. .

    so I say,

    register_data = (unsigned char) ptr_db;

    which doesnt compile and gives me incompatible pointer type
    assingment.

    Is there a way to get the value stored inside a pointer type variable
    (which is an address) and store it in another unsigned char variable ?

    Thanks
    ashu
     
    ashu, Feb 27, 2011
    #1
    1. Advertising

  2. ashu

    Ike Naar Guest

    On 2011-02-27, ashu <> wrote:
    > unsigned char register_data @ 0x00FE; /* declare a register whose
    > address is 00FE*/
    > unsigned char *ptr_db;
    > unsigned char ram_packet_header @ 0xD300; /* declare a memory address,
    > 0xD300*/
    > ptr_db = &ram_packet_header; /* Now ptr_db contains the address
    > D300*/
    >
    > untill here therr is no problem and the code compiles fine.
    >
    > Now I want to store the content of ptr_db( which is 0xD300, an
    > address) in the variable register_data. .
    >
    > so I say,
    >
    > register_data = (unsigned char) ptr_db;
    >
    > which doesnt compile and gives me incompatible pointer type
    > assingment.
    >
    > Is there a way to get the value stored inside a pointer type variable
    > (which is an address) and store it in another unsigned char variable ?


    register_data = *ptr_db;
     
    Ike Naar, Feb 28, 2011
    #2
    1. Advertising

  3. ashu <> writes:

    > I am facing a compilation problem for my C code on a 8 bit micro
    > controller (Motorola HC11).
    >
    > I use cosmic software's compiler which allows me to directly refrence
    > addresses, using the @ variable.
    >

    <snip>
    > unsigned char register_data @ 0x00FE; /* declare a register whose
    > address is 00FE*/
    > unsigned char *ptr_db;
    > unsigned char ram_packet_header @ 0xD300; /* declare a memory address,
    > 0xD300*/
    > ptr_db = &ram_packet_header; /* Now ptr_db contains the address
    > D300*/
    >
    > untill here therr is no problem and the code compiles fine.
    >
    > Now I want to store the content of ptr_db( which is 0xD300, an
    > address) in the variable register_data. .
    >
    > so I say,
    >
    > register_data = (unsigned char) ptr_db;
    >
    > which doesnt compile and gives me incompatible pointer type
    > assingment.


    What is this supposed to do? The conversion from a pointer to an
    unsigned char is up to the implementation -- the most obvious choice
    would be to treat the address as in integer and reduce modulo
    UCHAR_MAX+1 but that's very different from "[storing] the content of
    ptr_db in ... register_data".

    > Is there a way to get the value stored inside a pointer type variable
    > (which is an address) and store it in another unsigned char variable ?


    That seems unlikely since addresses will normally be "wider" that
    unsigned char.

    --
    Ben.
     
    Ben Bacarisse, Feb 28, 2011
    #3
  4. ashu

    ashu Guest

    Ben Bacarisse writes:

    > ashu <> writes:
    >
    >> I am facing a compilation problem for my C code on a 8 bit micro
    >> controller (Motorola HC11).
    >>
    >> I use cosmic software's compiler which allows me to directly refrence
    >> addresses, using the @ variable.
    >>

    > <snip>
    >> unsigned char register_data @ 0x00FE; /* declare a register whose
    >> address is 00FE*/
    >> unsigned char *ptr_db;
    >> unsigned char ram_packet_header @ 0xD300; /* declare a memory address,
    >> 0xD300*/
    >> ptr_db = &ram_packet_header; /* Now ptr_db contains the address D300*/
    >>
    >> untill here therr is no problem and the code compiles fine.
    >>
    >> Now I want to store the content of ptr_db( which is 0xD300, an address)
    >> in the variable register_data. .
    >>
    >> so I say,
    >>
    >> register_data = (unsigned char) ptr_db;
    >>
    >> which doesnt compile and gives me incompatible pointer type assingment.

    >
    > What is this supposed to do? The conversion from a pointer to an
    > unsigned char is up to the implementation -- the most obvious choice
    > would be to treat the address as in integer and reduce modulo
    > UCHAR_MAX+1 but that's very different from "[storing] the content of
    > ptr_db in ... register_data".
    >
    >> Is there a way to get the value stored inside a pointer type variable
    >> (which is an address) and store it in another unsigned char variable ?

    >
    > That seems unlikely since addresses will normally be "wider" that
    > unsigned char.


    Hi Ben,

    you noticed it :)

    Actually since my pointer can hold 16 bits, i want to extract lower 8
    bits of the value contained in pointer and store it in a 8 bit
    register.

    I also need the higher byte, which i will store in some other 8 bit
    register(which i havent mentioned in the code).

    thanks
    ashu
     
    ashu, Feb 28, 2011
    #4
  5. ashu <> writes:

    > Ben Bacarisse writes:

    <snip>
    >> ashu <> writes:
    >>> so I say,
    >>>
    >>> register_data = (unsigned char) ptr_db;
    >>>
    >>> which doesnt compile and gives me incompatible pointer type assingment.

    <snip>
    >>> Is there a way to get the value stored inside a pointer type variable
    >>> (which is an address) and store it in another unsigned char variable ?

    >>
    >> That seems unlikely since addresses will normally be "wider" that
    >> unsigned char.

    <snip>
    > you noticed it :)
    >
    > Actually since my pointer can hold 16 bits, i want to extract lower 8
    > bits of the value contained in pointer and store it in a 8 bit
    > register.
    >
    > I also need the higher byte, which i will store in some other 8 bit
    > register(which i havent mentioned in the code).


    The error message you get is unusual so it might help is you the actual
    code that generates it. There might be some detail that matters because
    what you reported is reasonable thing to try. I'd want to be absolutely
    sure it was not working.

    There are other options. If you have <stdint.h> you should be able to
    cast to uintptr_t. The two bytes you want can then be obtained in one
    go (you need shift when you assign the high-order byte). Indeed, a cast
    to an unsigned integer type with at least 16 bits should preserve the
    whole pointer.

    You could use a union:

    union ptr {
    void *ptr;
    unsigned char bytes[2]; /* You seem to know it's 2 bytes */
    } up = { ptr_db };

    You'll need to find out if up.bytes[0] or up.bytes[1] is the low-order
    byte.

    However, it's really important to find out why your compiler objects to
    casting a pointer to a char with a message about incompatible pointer
    types. It looks like you have not posted the actual code.

    --
    Ben.
     
    Ben Bacarisse, Feb 28, 2011
    #5
  6. ashu

    ashu Guest

    Ben Bacarisse writes:

    > ashu <> writes:
    >
    >> Ben Bacarisse writes:

    > <snip>
    >>> ashu <> writes:
    >>>> so I say,
    >>>>
    >>>> register_data = (unsigned char) ptr_db;
    >>>>
    >>>> which doesnt compile and gives me incompatible pointer type
    >>>> assingment.

    > <snip>
    >>>> Is there a way to get the value stored inside a pointer type variable
    >>>> (which is an address) and store it in another unsigned char variable
    >>>> ?
    >>>
    >>> That seems unlikely since addresses will normally be "wider" that
    >>> unsigned char.

    > <snip>
    >> you noticed it :)
    >>
    >> Actually since my pointer can hold 16 bits, i want to extract lower 8
    >> bits of the value contained in pointer and store it in a 8 bit
    >> register.
    >>
    >> I also need the higher byte, which i will store in some other 8 bit
    >> register(which i havent mentioned in the code).

    >
    > The error message you get is unusual so it might help is you the actual
    > code that generates it. There might be some detail that matters because
    > what you reported is reasonable thing to try. I'd want to be absolutely
    > sure it was not working.
    >
    > There are other options. If you have <stdint.h> you should be able to
    > cast to uintptr_t. The two bytes you want can then be obtained in one
    > go (you need shift when you assign the high-order byte). Indeed, a cast
    > to an unsigned integer type with at least 16 bits should preserve the
    > whole pointer.
    >
    > You could use a union:
    >
    > union ptr {
    > void *ptr;
    > unsigned char bytes[2]; /* You seem to know it's 2 bytes */
    > } up = { ptr_db };
    >
    > You'll need to find out if up.bytes[0] or up.bytes[1] is the low-order
    > byte.
    >
    > However, it's really important to find out why your compiler objects to
    > casting a pointer to a char with a message about incompatible pointer
    > types. It looks like you have not posted the actual code.


    Hi Ben,

    No, I am not missing an astreik, an astriek, would get me the value
    "pointed to" by the pointer, and not the address contained in the
    pointer type variable.

    I want to store the address stored in the pointer type variable in
    some variable, How to do that ?

    regs
    ashu
     
    ashu, Mar 1, 2011
    #6
  7. ashu <> writes:

    > Ben Bacarisse writes:
    >
    >> ashu <> writes:
    >>
    >>> Ben Bacarisse writes:

    >> <snip>
    >>>> ashu <> writes:
    >>>>> so I say,
    >>>>>
    >>>>> register_data = (unsigned char) ptr_db;
    >>>>>
    >>>>> which doesnt compile and gives me incompatible pointer type
    >>>>> assingment.

    >> <snip>
    >>>>> Is there a way to get the value stored inside a pointer type variable
    >>>>> (which is an address) and store it in another unsigned char variable
    >>>>> ?
    >>>>
    >>>> That seems unlikely since addresses will normally be "wider" that
    >>>> unsigned char.

    >> <snip>
    >>> you noticed it :)
    >>>
    >>> Actually since my pointer can hold 16 bits, i want to extract lower 8
    >>> bits of the value contained in pointer and store it in a 8 bit
    >>> register.
    >>>
    >>> I also need the higher byte, which i will store in some other 8 bit
    >>> register(which i havent mentioned in the code).

    >>
    >> The error message you get is unusual so it might help is you the actual
    >> code that generates it. There might be some detail that matters because
    >> what you reported is reasonable thing to try. I'd want to be absolutely
    >> sure it was not working.
    >>
    >> There are other options. If you have <stdint.h> you should be able to
    >> cast to uintptr_t. The two bytes you want can then be obtained in one
    >> go (you need shift when you assign the high-order byte). Indeed, a cast
    >> to an unsigned integer type with at least 16 bits should preserve the
    >> whole pointer.
    >>
    >> You could use a union:
    >>
    >> union ptr {
    >> void *ptr;
    >> unsigned char bytes[2]; /* You seem to know it's 2 bytes */
    >> } up = { ptr_db };
    >>
    >> You'll need to find out if up.bytes[0] or up.bytes[1] is the low-order
    >> byte.
    >>
    >> However, it's really important to find out why your compiler objects to
    >> casting a pointer to a char with a message about incompatible pointer
    >> types. It looks like you have not posted the actual code.

    <snip>
    > No, I am not missing an astreik, an astriek, would get me the value
    > "pointed to" by the pointer, and not the address contained in the
    > pointer type variable.


    Yes, that's not my point. The error message that you talk about (and
    you should really cut and paste it here so that there is no doubt about
    what it says) does not relate to the code you posted. Compilers can go
    wrong, of course, but I am not going to say that your compiler is
    emitting daft errors unless I've seen actual compilable code and the
    actual error message.

    A key factor is that what you want to do:

    > I want to store the address stored in the pointer type variable in
    > some variable, How to do that ?


    is often done (in part) with the code that you said gives this error.
    Understanding that error would be my first step.

    Did none of the other options I posted work for you?

    --
    Ben.
     
    Ben Bacarisse, Mar 2, 2011
    #7
    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. ELJ
    Replies:
    2
    Views:
    582
    Paulo Valentim
    Feb 25, 2005
  2. Dan
    Replies:
    3
    Views:
    632
    =?Utf-8?B?RGFu?=
    Jun 14, 2004
  3. JSR
    Replies:
    2
    Views:
    373
  4. bcassand
    Replies:
    4
    Views:
    1,541
    Martin Moessner
    Jan 12, 2004
  5. C__chp
    Replies:
    4
    Views:
    532
    Puppet_Sock
    Feb 15, 2008
Loading...

Share This Page