View previous topic :: View next topic |
Author |
Message |
Zer0flag Guest
|
Problem with pointers when compiling ANSI C code |
Posted: Thu Dec 13, 2007 3:31 am |
|
|
Hi all!
I am trying to compile some ANSI C code with CCS PICC. Unfortunately there seem to be some problems.
For example code like this one
Code: |
struct strSpecUnits
{
const char *SUnit;
enum enUnits eUnits;
signed char Exp;
};
|
causes the following compiler error:
*** Error 34: Unknown type
Looks like the problem here is the const keyword. I believe the problem is caused by the pointer to a constant. If I remove the const keyword this portion of the code compiles OK.
Another problem is the following line of code:
Code: |
UCHAR SCPI_ParamType (struct strParam *psParam, enum enParamType *pePType, UCHAR *pNumSubtype);
|
It causes the following compiler error:
*** Error 33: Expecting a {
Here the problem is the pointer to an enumeration: enum enParamType *pePType
Do you have any idea how the problems can be solved?
Thank you in advance!
Regards,
Zer0flag |
|
|
Ttelmah Guest
|
|
Posted: Thu Dec 13, 2007 10:54 am |
|
|
Ignoring anything else, the const declaration, is simply incorrect!.
The ansi declaration, for a pointer to a constant char, is:
char const *SUnit;
This works.
The enum declaration, fails in Sun ANSI C as show. It is accepted in C++. I don't think this is legitimate ANSI C as written.
Best Wishes |
|
|
Ken Johnson
Joined: 23 Mar 2006 Posts: 197 Location: Lewisburg, WV
|
|
Posted: Thu Dec 13, 2007 12:43 pm |
|
|
I believe that ANSI C treats:
char const *SUnit; // SUnit is a pointer to a const char;
the same as:
const char *SUnit; // SUnit is a pointer to a char which is const;
Don't know how CCS will handle either.
Slap me if I'm wrong
Ken |
|
|
Ttelmah Guest
|
|
Posted: Thu Dec 13, 2007 3:33 pm |
|
|
The declaration in the ANSI documentation (I checked...), requires the const to be after the type for a pointer. 'const int *', would imply a constant pointer to an int, while 'int const *', declares a pointer to a constant. I suspect it is more that most compilers accept either syntax, and 'force' the one with the leading const, to behave the same as the one with it trailing.
Best Wishes |
|
|
SherpaDoug
Joined: 07 Sep 2003 Posts: 1640 Location: Cape Cod Mass USA
|
|
Posted: Thu Dec 13, 2007 4:26 pm |
|
|
CCS never pretends to be ANSI compliant. It pre-dates the ANSI standard and actually follows K&R first edition C better. _________________ The search for better is endless. Instead simply find very good and get the job done. |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Thu Dec 13, 2007 5:23 pm |
|
|
Traditionally in CCS, variables declared as 'const' are placed in ROM
(program memory) as 'RETLW' statements.
In vs. 4 of the compiler, you can make 'const' behave in the ANSI
way by using the READ_ONLY directive:
Quote: |
#device CONST=READ_ONLY
Uses the ANSI keyword CONST definition,
making CONST variables read only, rather than
located in program memory.
#device CONST=ROM
Uses the CCS compiler traditional keyword
CONST definition, making CONST variables
located in program memory.
This is the default mode. |
Example: The following program places the 'data' array in RAM.
Note the #device directive which forces this to happen.
Quote: |
#include <16F877.H>
#device CONST=READ_ONLY
#fuses HS, NOWDT, NOPROTECT, BROWNOUT, PUT, NOLVP
#use delay(clock = 20000000)
//#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)
char const data[] = {"ABCDE"};
//======================================
void main()
{
char i;
char c;
c = data[i];
while(1);
} |
|
|
|
|