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 support@ccsinfo.com

Code Question and Recommendation on Good C Book

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



Joined: 18 Jul 2006
Posts: 98

View user's profile Send private message

Code Question and Recommendation on Good C Book
PostPosted: Tue Jul 18, 2006 3:40 pm     Reply with quote

I have a question as to what the & in the int8 declarations in the function below does.

Also I am new to C programming (not a student as someone thought) just an old analog engineer trying to keep my grey matter from hardening.

Can anyone recommend a good book on C programming as it relates to the CCS compiler and microchip? I bought the "PICmicro MCU C" by Nigel Gardner but I need something with a little more meat to it.

Thanks for everyones support.

Code:
void GetDigits(int16 Val, int8 &thous, int8 &huns, int8 &tens, int8 &units)
{
  tens = 0;
  huns = 0;
  thous = 0;
 
  while (Val >= 1000)
  {
    Val -= 1000;
    thous++;
  }
 
  while (Val >= 100)
  {
    Val -= 100;
    huns++;
  }
 
  while (Val >= 10)
  {
    Val -= 10;
    tens++;
  }

  units = Val;
}

//===============================
void main ()
{
  int16 adcvalue;
  int8 thous,huns,tens,units;

  adcvalue = 1015;

  GetDigits(adcValue, thous, huns, tens, units);
 
  while (1);
}
rwyoung



Joined: 12 Nov 2003
Posts: 563
Location: Lawrence, KS USA

View user's profile Send private message Send e-mail

PostPosted: Tue Jul 18, 2006 4:00 pm     Reply with quote

C language in general:

The C Programming Lanugage, 2nd Ed. by Kernighan and Ritchie.

And I'd switch your function to use
Code:
int8 *thou
etc instead of
Code:
int8 &thou
then access them inside the function as .
Code:
*thou = 0;
and when calling
Code:
GetDigits(adcValue, &thous, &huns, &tens, &units);


& and * are sort of equivalent but not exactly. & generally reads as "address of" and * as "pointer to". Becareful too with operator precidence when using pointers.
_________________
Rob Young
The Screw-Up Fairy may just visit you but he has crashed on my couch for the last month!
dyeatman



Joined: 06 Sep 2003
Posts: 1923
Location: Norman, OK

View user's profile Send private message

PostPosted: Tue Jul 18, 2006 4:02 pm     Reply with quote

a quick Google gives this:

In both C and C++, the ampersand is also used at the start of a variable name to reference to the memory address of that variable in order to change the variable's value. This means they are passing the address of the variable.

GOOGLE is your friend!

K&R C is a great book that most of us have. You can get it from Amazon for almost nothing.

P.S. Sorry Rob, you just beat me to it.....
ckielstra



Joined: 18 Mar 2004
Posts: 3680
Location: The Netherlands

View user's profile Send private message

PostPosted: Tue Jul 18, 2006 5:20 pm     Reply with quote

The method of passing the parameters with the '&' sign is named 'call by reference'. It is method for passing parameters of which you want to change and return the contents to the caller. As already indicated by the other posters it is similar to passing pointer parameters but the resulting code is easier to read because you don't have to ' dereference' the pointers.

rwyoung wrote:
And I'd switch your function to use
Code:
int8 *thou
etc instead of
Code:
int8 &thou
Rob, could you explain why you think the function call should be changed? Why do you think the pointer parameters are better in this situation? To my knowledge both methods are equally efficient and is it just a matter of taste which technique to use.

Early versions of C didn't support the 'call by reference' parameters while modern languages like Java don't allow the unrestricted pointer parameters anymore because of safety reasons.
Ttelmah
Guest







PostPosted: Wed Jul 19, 2006 2:27 am     Reply with quote

You have to be a bit careful with 'call by reference'. It is an extension to basic C, and in CCS, only implemented in a _very_ limited way. It's advantage, is that as shown, with a single function used (or with multiple functions, if these are coded as 'inline'), it will result in the subroutine directly accessing the variables 'passed', without using address calculations at all, and will result in smaller/faster code. Basically the compiler 'hard codes' the referenced address, into the subroutine. However if multiple calls to the routine are made, and these are seperate, then using the 'traditional' pointer form is safer in CCS.

Best Wishes
rwyoung



Joined: 12 Nov 2003
Posts: 563
Location: Lawrence, KS USA

View user's profile Send private message Send e-mail

PostPosted: Wed Jul 19, 2006 6:41 am     Reply with quote

RJ gave a better (and shorter) answer than I could.

I have had much less trouble with "odd" code when using int *num_monkeys than int &num_monkeys with the CCS compiler(s).

This does bring up two good points for beginners (all of us for that matter), and they are:

Always look at your LST file to see how the compiler did something.

And, be able to at least READ the assembly listings. Better if you can write in assembly too but at least be able to read a section of code and understand what is happening.
_________________
Rob Young
The Screw-Up Fairy may just visit you but he has crashed on my couch for the last month!
newguy



Joined: 24 Jun 2004
Posts: 1903

View user's profile Send private message

PostPosted: Wed Jul 19, 2006 10:25 am     Reply with quote

"Embedded C Programming and the Microchip PIC" by Barnett, Cox and O'Cull. Pretty good book, and it does deal with the CCS compiler. Even comes with a demo version of the compiler on CD.
arnfielb
Guest







recommended 'C' Book
PostPosted: Tue Aug 29, 2006 6:08 am     Reply with quote

Good book is "The Indispensible Guide to 'C'" - written for students and contains many real world embedded C examples and including practical examples of interfacing to hardware. This book is not PIC specific.
Good Hunting
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