|
|
View previous topic :: View next topic |
Author |
Message |
Johannes Guest
|
Pointers in CCS |
Posted: Thu May 22, 2003 2:10 am |
|
|
I have wrote a few C code programs, but have never ever used pointers. I need to get the necessary knowledge of pointers before I start. Does anyone know about a document or something I can read that will clearly explain the usage of pointers for CCS.
Currently I am using the PIC18F458 with onboard CAN and the PCW + PCH v3.150 compiler.
I need to know why pointers are used?
Is it saving ram usage, more efficient code, etc... ?
Any info will be appreciated.
Thanks,
Johannes Geldenhuys
___________________________
This message was ported from CCS's old forum
Original Post ID: 144514663 |
|
|
R.J.Hamlett Guest
|
Re: Pointers in CCS |
Posted: Thu May 22, 2003 7:07 am |
|
|
:=I have wrote a few C code programs, but have never ever used pointers. I need to get the necessary knowledge of pointers before I start. Does anyone know about a document or something I can read that will clearly explain the usage of pointers for CCS.
:=
:=Currently I am using the PIC18F458 with onboard CAN and the PCW + PCH v3.150 compiler.
:=
:=I need to know why pointers are used?
:=Is it saving ram usage, more efficient code, etc... ?
:=
:=Any info will be appreciated.
At their heart, pointers are the same in CCS, as any other C, _with the exception that they cannot be used for constants, for 'bit' variables, or for functions_. The 'reasons' for pointers are many. The most fundamental one, is that C, passes all variables 'by value'. In most other languages, it is possible to pass variables 'by reference' as well, and in this case, a function can modify the original variable. In C, since this is not done, pointers can be used instead, with the function receiving the _address_ of a variable, and then accessing the contents of this address. This 'lack' in C, results generally in smaller compiled code.
The other use of normal pointers, is to allow similar abilities to those of unions, where data in a particular variable, at an address, can be manipulated differently in a function, from in the main program. So (for instance), the main program can pass the address of a floating point variable, and the subroutine can access this as individual bytes.
Pointers to functions, are normally a very powerful way to allow rapid access to different routines, and this is probably the biggest 'lack' in the CCS implementation. The core reason behind the lacks, is the Harvard processor architecture, which distinguishes data memory, from program memory (constants and functions are stored in the program memory), which especially on the older processors, makes implementing functions to deal with these addresses relatively hard. With the newer larger chips (like the 18F family), a number of abilities were added to the chip, which potentially make it possible to implement these abilities (some of the competing 'C' compilers do this).
Any C primer (or even the original Kerningham & Ritchie 'C programming language), will cover how pointers are used.
Best Wishes
___________________________
This message was ported from CCS's old forum
Original Post ID: 144514671 |
|
|
Steve H. Guest
|
Re: Pointers in CCS |
Posted: Thu May 22, 2003 8:34 am |
|
|
This is JMHO - It works for me, perhaps not for everyone. Pointers are described very well in the K&R book (as described by R.J. in an earlier post). You can do really cool things with them like walking up and down strings or reversing strings, etc.
You can also do really, really cool things with pointers like use pointers to pointers to pointers to functions.
Now this is the part I will get flamed on :-)
I avoid pointers where possible. Why?
1) I need code that is easy to figure out and that non C gurus can maintain (including myself).
2) I need to optimize readability over code size or speed. Keeping Pointers to pointers to whatever straight ... just makes my head hurt.
I do use pointers where it makes the code so compact that it replaces many lines with just a few as the string functions in the K&R book show or where there is no other simple other way. In my case this is mostly string work or parsing.
So I would suggest the K&R book (As R.J. did) then decide if you need way cool code that only a select few will ever be able to figure out or if you need to optimize the readability of the code for the masses.
Good luck - Steve H.
___________________________
This message was ported from CCS's old forum
Original Post ID: 144514674 |
|
|
Richard Payne Guest
|
Re: Pointers in CCS |
Posted: Tue May 27, 2003 5:24 am |
|
|
I am bit confused:-
Will CCS (PCWH) support the pointers implementation for 18F series but not 16F, is this what you trying to express.
I wonder if IAR complier support pointers as well?. It much more expensive package.
Best Wishes.
Riscy.
___________________________
This message was ported from CCS's old forum
Original Post ID: 144514775 |
|
|
R.J.Hamlett Guest
|
Re: Pointers in CCS |
Posted: Tue May 27, 2003 7:58 am |
|
|
:=I am bit confused:-
:=
:=Will CCS (PCWH) support the pointers implementation for 18F series but not 16F, is this what you trying to express.
:=
No.
The CCS compiler does not support pointers to constants with either chip type. On the 16x family, this is 'understandable', since there is no support in the architecture for this. However potentially, with the 18F chips (actually with any of the chips that allow 'read_program_eeprom' accesses), it would be possible to write code to allow pointer accesses to constant data. However CCS does not do this at present.
:=I wonder if IAR complier support pointers as well?. It much more expensive package.
:=
It does in a way on the 18F chips. There is a '__constptr' function, which declares a pointer to a constant in the program memory for these chips.
:=Best Wishes.
:=Riscy.
Best Wishes
___________________________
This message was ported from CCS's old forum
Original Post ID: 144514780 |
|
|
|
|
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
|