|
|
View previous topic :: View next topic |
Author |
Message |
pleditor
Joined: 27 Feb 2013 Posts: 3
|
Char Identifier in a Function |
Posted: Wed Feb 27, 2013 1:26 am |
|
|
Cant get this code to work :-/ "attempt to create a pointer to a constant" error
Code: |
void led(char[4] color)
{
if (color[0] == 'g') ledA(1,1);
if (color[0] == 'b') ledA(1,2);
if (color[0] == 'r') ledA(1,3); //reading only the first digit (r)
}
main()
{
led("r00");
}
|
This code works perfect
Code: |
void led()
{
char color[4] = "r00";
if (color[0] == 'g') ledA(1,1);
if (color[0] == 'b') ledA(1,2);
if (color[0] == 'r') ledA(1,3); //reading only the first digit (r)
}
main()
{
led();
}
|
Last edited by pleditor on Wed Feb 27, 2013 10:22 am; edited 1 time in total |
|
|
Mike Walne
Joined: 19 Feb 2004 Posts: 1785 Location: Boston Spa UK
|
|
Posted: Wed Feb 27, 2013 2:44 am |
|
|
Post SHORT complete compilable code which illustrates your problem.
Then we can try it.
Mike |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19553
|
|
Posted: Wed Feb 27, 2013 3:42 am |
|
|
This is down to the difference in constant strings.
A search in the forum will find a lot about this, and full explanations
Key is that the PIC, does not have a classic memory architecture, with a single 'flat' memory space, containing both ROM and RAM. The ROM is actually in a different memory map to the RAM. Now this has the advantage that the chip can be accessing both memories 'at once', pre-fetching the next instruction from ROM, while working with the RAM at the same time, without having to have cache memory. When you call:
led("r00");
"r00", is _not_ a char string in RAM. You are declaring a ROM string. This is in the second memory space, and on older PIC's,this memory space is completely inaccessible. Hence the compiler has to generate a _program_ which can be called with a value saying 'which character is wanted, not a string that can actually be accessed as an array.....
On later chips this can be programmed round. The compiler command:
#device PASS_STRINGS=IN_RAM
will tell the compiler to automatically copy ROM strings into RAM so they can be normally accessed as arrays.
The forum search will explain what happens otherwise.
Best Wishes |
|
|
pleditor
Joined: 27 Feb 2013 Posts: 3
|
|
Posted: Wed Feb 27, 2013 10:24 am |
|
|
Ttelmah wrote: | This is down to the difference in constant strings.
A search in the forum will find a lot about this, and full explanations
Key is that the PIC, does not have a classic memory architecture, with a single 'flat' memory space, containing both ROM and RAM. The ROM is actually in a different memory map to the RAM. Now this has the advantage that the chip can be accessing both memories 'at once', pre-fetching the next instruction from ROM, while working with the RAM at the same time, without having to have cache memory. When you call:
led("r00");
"r00", is _not_ a char string in RAM. You are declaring a ROM string. This is in the second memory space, and on older PIC's,this memory space is completely inaccessible. Hence the compiler has to generate a _program_ which can be called with a value saying 'which character is wanted, not a string that can actually be accessed as an array.....
On later chips this can be programmed round. The compiler command:
#device PASS_STRINGS=IN_RAM
will tell the compiler to automatically copy ROM strings into RAM so they can be normally accessed as arrays.
The forum search will explain what happens otherwise.
Best Wishes |
wow, thanks for the explanation. I am working with the pic16F887, as soon i get the chance i will try to get this solved. |
|
|
pleditor
Joined: 27 Feb 2013 Posts: 3
|
|
Posted: Thu Feb 28, 2013 4:08 pm |
|
|
i did add the line "#device PASS_STRINGS=IN_RAM" and everything works as expected. Thanks again :-) |
|
|
|
|
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
|