View previous topic :: View next topic |
Author |
Message |
edbfmi1
Joined: 18 Jul 2006 Posts: 103
|
Code Question and Recommendation on Good C Book |
Posted: Tue Jul 18, 2006 3:40 pm |
|
|
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
|
|
Posted: Tue Jul 18, 2006 4:00 pm |
|
|
C language in general:
The C Programming Lanugage, 2nd Ed. by Kernighan and Ritchie.
And I'd switch your function to use etc instead of then access them inside the function as . 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: 1933 Location: Norman, OK
|
|
Posted: Tue Jul 18, 2006 4:02 pm |
|
|
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
|
|
Posted: Tue Jul 18, 2006 5:20 pm |
|
|
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 etc instead of | 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
|
|
Posted: Wed Jul 19, 2006 2:27 am |
|
|
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
|
|
Posted: Wed Jul 19, 2006 6:41 am |
|
|
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: 1907
|
|
Posted: Wed Jul 19, 2006 10:25 am |
|
|
"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 |
Posted: Tue Aug 29, 2006 6:08 am |
|
|
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 |
|
|
|