|
|
View previous topic :: View next topic |
Author |
Message |
jorge
Joined: 29 Nov 2009 Posts: 6 Location: germany
|
basics - reference parameters |
Posted: Sat Jan 09, 2010 6:30 am |
|
|
Hello,
going through a code (PCHW) I found following line:
Code: | *((int8*)&data +1) = read_eeprom(addr); |
What is the meaning of the second "*" at int8?
Could this asterics be ommitted?
I only know this kind of reference from parameters of procedures.
Jorge |
|
|
Ttelmah Guest
|
|
Posted: Sat Jan 09, 2010 11:02 am |
|
|
It means 'pointer'.
It says to treat the value returned, as the _location_ of an int8, _not_ as an int8 itself.
It is _vital_. Since the number being returned _is_ an address, omitting the *, won't work at all.
The value being returded, _is_ an address, (hence the first *), but by default is a pointer to a value of type 'data' (probably an int16 ). The EEPROM, wants to deal with bytes, so the cast (the (int8*) is needed to change the type, and the 'rules' associated with how arithmetic affects it to suit the EEPROM, otherwise bytes will be lost.
The whole cast could be omitted on very old compilers, which incorrectly treated _all_ pointers as if they were to single bytes. Hence you may find some old code without this.
Best Wishes
Best Wishes |
|
|
jorge
Joined: 29 Nov 2009 Posts: 6 Location: germany
|
|
Posted: Sat Jan 09, 2010 12:06 pm |
|
|
Thank you Ttelmah,
now I got it. It is pretty tricky!
Through this I am reading one byte into the low-byte of the int16 data location. Through the first de-referencing operator(*) I gain access to the data located at the address (data + 1) contained in the pointer.
Best Regards
Jorge |
|
|
Ttelmah Guest
|
|
Posted: Sun Jan 10, 2010 3:47 am |
|
|
Basically, yes.
This is one of the key 'points' of C 'pointer arithmetic'.
If you have a pointer to something, and increment it, it then accesses the next 'object' of the type being pointed to. So if you have a pointer to a int16, and add one to the pointer, it accesses the next 'int16'. Similarly, a pointer to a float, add one, and it'll access the next float.
What is being done here, is to take a pointer (to any type of object), and then tell the C pointer arithmetic, to treat it as if it points to an 'int8' (and hence single byte) data, so when it is incremented, it moves by just one byte, giving access to the next byte as you say. It is a way of overriding the automatic pointer scaling.
As a possibly clearer way of doing it (at a cost of using a temporary storage location), you can use:
Code: |
int8 *fred; //declare 'fred' as a pointer to an int8 - needs to be at start
//of routine
//Now the alternative
fred = &data; //put the address of 'data' into fred
*(fred+1) = read_eeprom(addr);
//Or....
fred[1] = read_eeprom(addr);
|
A 'pointer', can also be used as an array. Since 'fred' now points to bytes, fred[0] is the first byte in 'data', fred[1] the second, etc...
This ability to swap between pointers and arrays, is both a 'strength' of C, and also a way in which it is possible to really 'muck things up'.....
The problem in both, is that since there is no 'bounds checking', you can talk to a location like 'fred[58]', and overwrite something you shouldn't.
Best Wishes |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|