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 CCS Technical Support

Char Identifier in a Function

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



Joined: 27 Feb 2013
Posts: 3

View user's profile Send private message

Char Identifier in a Function
PostPosted: Wed Feb 27, 2013 1:26 am     Reply with quote

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 Confused
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

View user's profile Send private message

PostPosted: Wed Feb 27, 2013 2:44 am     Reply with quote

Post SHORT complete compilable code which illustrates your problem.
Then we can try it.

Mike
Ttelmah



Joined: 11 Mar 2010
Posts: 19554

View user's profile Send private message

PostPosted: Wed Feb 27, 2013 3:42 am     Reply with quote

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

View user's profile Send private message

PostPosted: Wed Feb 27, 2013 10:24 am     Reply with quote

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

View user's profile Send private message

PostPosted: Thu Feb 28, 2013 4:08 pm     Reply with quote

i did add the line "#device PASS_STRINGS=IN_RAM" and everything works as expected. Thanks again :-)
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