View previous topic :: View next topic |
Author |
Message |
bcfd36
Joined: 14 Apr 2015 Posts: 28 Location: Boulder Creek, CA
|
Pointers into ROM |
Posted: Fri May 15, 2015 2:59 pm |
|
|
Experimentation shows this to be true, but I want to verify it with those in-the-know.
The following code segment has the correct behavior:
Code: | char *letters[] = {"ab", "cd", "ef", "gh", "ij") ;
for(int i = 0; i < 5; i++)
{
func1(letters[i]) ;
}
|
However, if you put a const in front of the char *, the strings are not dealt with correctly. Pointers into the ROM area do not work. And no compiler errors for 5.0.18.
Have I got that right? _________________ D. Scruggs |
|
|
asmboy
Joined: 20 Nov 2007 Posts: 2128 Location: albany ny
|
|
Posted: Fri May 15, 2015 3:05 pm |
|
|
march 2015 manual for 5.042+ says:
Quote: |
(type qualifier) ROM Forces data into program memory. Pointers may be used to this data but they can not be mixed with RAM pointers. |
and while i recognize THIS declaration:
i am unfamiliar with how you expect YOUR declaration of a character array ending with a ')' to work at all.
certainly that line does not compile for you as listed does it ?
it does not for me........ |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Fri May 15, 2015 3:27 pm |
|
|
Here is one way to do it. This program displays the following output in
MPLAB simulator (MPLAB vs. 8.92):
Test program:
Code: | #include <18F4620.h>
#device PASS_STRINGS=IN_RAM
#fuses INTRC_IO, BROWNOUT, PUT, NOWDT
#use delay(clock=4M)
#use rs232(baud=9600, UART1, ERRORS)
void func1(char *ptr)
{
printf(ptr);
putc(' ');
}
//========================
void main()
{
char const letters[5][3] = {"ab", "cd", "ef", "gh", "ij"} ;
for(int i = 0; i < 5; i++)
{
func1(letters[i]) ;
}
while(1);
} |
There might also be a way to do it using 'rom' pointers. |
|
|
bcfd36
Joined: 14 Apr 2015 Posts: 28 Location: Boulder Creek, CA
|
|
Posted: Fri May 15, 2015 3:36 pm |
|
|
Obviously, the ")" was a typo and should have been a "}". I typed in the segment, not cut and paste.
And I am using 5.018 (not my choice).
So you found a coding error, but didn't answer the question.
The FAQ in the CCS manual, under "How can a constant data table be
placed in ROM?" states you cannot have something like
Code: |
byte const table[5] = {1,2,3,4,5};
ptr = &table[i] |
if table is in ROM. I believe the const char * would fall into the same category.
Do you know a way around this? _________________ D. Scruggs |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Fri May 15, 2015 3:41 pm |
|
|
Did you see my post ? |
|
|
bcfd36
Joined: 14 Apr 2015 Posts: 28 Location: Boulder Creek, CA
|
|
Posted: Fri May 15, 2015 4:15 pm |
|
|
PCM Programmer,
Yep, saw your post later in the discussion.
Unfortunately, in my real world program, the strings in the char * array are not all the same length. Making them all the same length would chew up a bunch of space that was not really being used.
Also, "rom pointers"? I haven't found a reference to that. Yet. I'll go look.
I appreciate the reply though _________________ D. Scruggs |
|
|
|