C compilation problem

A

ashu

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
 
I

Ike Naar

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;
 
B

Ben Bacarisse

ashu said:
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.
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.
 
A

ashu

Ben said:
ashu said:
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.
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
 
B

Ben Bacarisse

ashu said:
Ben Bacarisse writes:
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.
 
A

ashu

Ben said:
ashu said:
Ben Bacarisse writes:
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
 
B

Ben Bacarisse

ashu said:
Ben said:
ashu said:
Ben Bacarisse writes:
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
?

That seems unlikely since addresses will normally be "wider" that
unsigned char.
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.
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?
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top