Typecast Warning

Go To Last Post
10 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Freaks,

Yes, it's the old "incompatible typecast warning". Normally I can fix these, but this has both me and a friend stumped.

I'm trying to write out the contents of my EEPROM on powerup through the UART. I've written the following code:

	// EEPROM data dump - spit all EEPROM data out serial port for debugging:
	for(unsigned int EEaddr=0; EEaddr<512; EEaddr++)
	{
		UART_TransmitByte(eeprom_read_byte((unsigned int *)EEaddr));
	}

	UART_TransmitByte('\n');
	// End EEPROM data dump

This works fine, but gives me the following warning:

Quote:
Master.c:70: warning: passing arg 1 of `eeprom_read_byte' from incompatible pointer type

Obviously I want this warning to go away; working or not, warnings are never a good thing. I've RTFM'd the Lib C docs, and found that the eeprom_read_byte function wants a unsigned int pointer, hence the typecast. Any ideas?

- Dean :twisted:[/code]

Make Atmel Studio better with my free extensions. Open source and feedback welcome!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

abcminiuser wrote:
....
I've RTFM'd the Lib C docs, and found that the eeprom_read_byte function wants a unsigned int pointer, hence the typecast. Any ideas?

My :twisted: documentation states something different.
The mentioned argument is of type:

const uint8_t *addr

Regards,

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

And I believe that warnings are always a good thing.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Crap. I just saw that I was looking at the eeprom_read_word function. Thanks arturl.

In any case, how do you get a pointer to a int address if the pointer's only a char width?

EDIT: Oh, warnings themselves are a good thing, but IGNORING a warning is a bad thing. I'd much prefer no warnings at all, wouldn't you? ;)

- Dean :twisted:

Make Atmel Studio better with my free extensions. Open source and feedback welcome!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Well, I still dream for the time I will write 1 page of code without errors or warnings...

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

abcminiuser wrote:

In any case, how do you get a pointer to a int address if the pointer's only a char width?

Hi !

Pointer "addr" is 2-bytes wide all the time. But it points to
one byte (char) variable in "read_byte"case , and to 2-byte variable
(int) in the case of "read_word".

HTH Jurek S.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I think that the 'pointer' variable is more to suggest an address than being used as a pointer. Anyway, the cast does nothing but a handshake with the compiler even if the value that is casted is 8 bit or 16 bit.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

"The more you know".

Thanks guys. No matter how long I learn there's ALWAYS somthing I don't know. Same for life, really.
- Dean :twisted:

Make Atmel Studio better with my free extensions. Open source and feedback welcome!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:
Anyway, the cast does nothing but a handshake with the compiler even if the value that is casted is 8 bit or 16 bit.

Although I entirely agree that in this case the cast achieves very little (except clean compilation, of course!) it is perhaps worth pointing out that the size of a "thing" being pointed to does have a bearing on the effects of arithmetic on a pointer.

For example, take the following code sequence:

char  * Bert ;
short * Fred ;

Fred = (char *) 10 ;
Bert = (short *) 10 ;

++Fred ;
++Bert ;

At the end of this fragment, "Fred" has a value of 11, whereas "Bert" has a value of 12.

Sorry if teaching grandmothers to suck eggs.

/A

If we are not supposed to eat animals, why are they made out of meat?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Yes, it was a comment on a particular case.