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

pointers to constants in function parameters

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



Joined: 30 May 2023
Posts: 12

View user's profile Send private message

pointers to constants in function parameters
PostPosted: Sun Sep 24, 2023 3:33 pm     Reply with quote

Compiler version 5.115, PIC18F46K22
Having the hardest time understanding how to pass string literal constants to functions in CCS C compiler.
Originally had a function like this:
FUNCT(const char* string);
and was trying to use it like this:
FUNCT("some text");
but CCS doesn't like the ptr to a constant, so removed the const:
FUNCT(char* string);
but still can't pass a string literal without getting an error about trying to make a ptr to a constant. Have tried making the string a variable, but feel like I'm missing something obvious here. How do I pass a string literal to a function?
ncdoody



Joined: 30 May 2023
Posts: 12

View user's profile Send private message

PostPosted: Sun Sep 24, 2023 6:27 pm     Reply with quote

Have seen some of the old posts regarding:
#device ANSI
#device PASS_STRINGS=IN_RAM
but not sure if that is the solution.
Ttelmah



Joined: 11 Mar 2010
Posts: 19226

View user's profile Send private message

PostPosted: Mon Sep 25, 2023 4:18 am     Reply with quote

First key thing to get your head round, is that 'const' on CCS C has a
different meaning to 'const' in ANSI C.

CCS had 'const' before ANSI. CCS's const, meant a value stored in the
ROM. Because of the architecture of the chip, this is a different memory
space to the primary memory. Early chip offered no actual way to read
this ROM space. So the early CCS consts, could not have pointers constructed.
Also since the memory space was separate, there was a address 0x100 in
ROM and another in RAM.....

ANSI const, means a standard RAM variable, that has potentially some
protection against being written (easy on chips with address protection,
but not on other chips). On something like 75% of ANSI compilers, const
actually does nothing!....

So to use const, like ANSI, just ignore it. Remove const from all
declarations, and pass strings as RAM strings.

To use string literals, select the PASS_STRINGS option. What this does is
generate a small temporary RAM buffer which can be addressed with a
pointer, and generates a virtual address using this through which the
literal is copied. This genuinely behaves like an ANSI const, since the
resulting pointer cannot be written to.
ncdoody



Joined: 30 May 2023
Posts: 12

View user's profile Send private message

PostPosted: Mon Sep 25, 2023 5:18 am     Reply with quote

So, would I use PASS_STRINGS as a global setting, e.g. at the beginning of main, or should it be used in a more narrow scope?
Ttelmah



Joined: 11 Mar 2010
Posts: 19226

View user's profile Send private message

PostPosted: Mon Sep 25, 2023 6:14 am     Reply with quote

Global.
Once the extra code is loaded for this, costs nothing on other consts.
ncdoody



Joined: 30 May 2023
Posts: 12

View user's profile Send private message

PostPosted: Mon Sep 25, 2023 12:39 pm     Reply with quote

How do rom pointers relate to the passing of string literals, constants, and PASS_STRINGS?

i.e. something like...
void some_function(rom char* string) {}
and the call...
some_function( (rom char*) "some text" );
Ttelmah



Joined: 11 Mar 2010
Posts: 19226

View user's profile Send private message

PostPosted: Tue Sep 26, 2023 9:03 am     Reply with quote

No real connection at all.

The point is a rom pointer is known to be an address in rom space, and
the code knows to handle this. A normal pointer does not have this
knowledge so has to handle everything in or through RAM.
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