CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

basics - reference parameters

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
jorge



Joined: 29 Nov 2009
Posts: 6
Location: germany

View user's profile Send private message

basics - reference parameters
PostPosted: Sat Jan 09, 2010 6:30 am     Reply with quote

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







PostPosted: Sat Jan 09, 2010 11:02 am     Reply with quote

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

View user's profile Send private message

PostPosted: Sat Jan 09, 2010 12:06 pm     Reply with quote

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







PostPosted: Sun Jan 10, 2010 3:47 am     Reply with quote

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

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
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
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