View previous topic :: View next topic |
Author |
Message |
ncdoody
Joined: 30 May 2023 Posts: 12
|
pointers to constants in function parameters |
Posted: Sun Sep 24, 2023 3:33 pm |
|
|
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
|
|
Posted: Sun Sep 24, 2023 6:27 pm |
|
|
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: 19539
|
|
Posted: Mon Sep 25, 2023 4:18 am |
|
|
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
|
|
Posted: Mon Sep 25, 2023 5:18 am |
|
|
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: 19539
|
|
Posted: Mon Sep 25, 2023 6:14 am |
|
|
Global.
Once the extra code is loaded for this, costs nothing on other consts. |
|
|
ncdoody
Joined: 30 May 2023 Posts: 12
|
|
Posted: Mon Sep 25, 2023 12:39 pm |
|
|
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: 19539
|
|
Posted: Tue Sep 26, 2023 9:03 am |
|
|
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. |
|
|
|