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

Problem with pointers when compiling ANSI C code

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







Problem with pointers when compiling ANSI C code
PostPosted: Thu Dec 13, 2007 3:31 am     Reply with quote

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







PostPosted: Thu Dec 13, 2007 10:54 am     Reply with quote

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

View user's profile Send private message

PostPosted: Thu Dec 13, 2007 12:43 pm     Reply with quote

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 Smile

Ken
Ttelmah
Guest







PostPosted: Thu Dec 13, 2007 3:33 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Thu Dec 13, 2007 4:26 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Thu Dec 13, 2007 5:23 pm     Reply with quote

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);
}
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