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

Pointer to constant

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



Joined: 20 Dec 2005
Posts: 16

View user's profile Send private message Send e-mail

Pointer to constant
PostPosted: Mon Apr 04, 2011 7:42 am     Reply with quote

Compiler: 4.119

Hello,

I tried a sample for using pointer to constant from CCS web site.
The same is in CCS manual.

This error appears:
*** Error 46 "Bdt12_Main.c" Line 7(16,17): Expecting an =
*** Error 49 "Bdt12_Main.c" Line 14(8,11): Expecting LVALUE such as a variable name or * expression
2 Errors, 0 Warnings.

Code:

#include <16F1824.h>

const char version[] = "PRODUCT ID V1.01";
const char *ptr;

#zero_ram
void main()
{
// A simple example showing the assignment of a pointer to a constant with the address of a constant string:

   ptr = &version[0];
   
   while(TRUE)
      ;
}

What is the problem ?

Thank's
Ttelmah



Joined: 11 Mar 2010
Posts: 19267

View user's profile Send private message

PostPosted: Mon Apr 04, 2011 9:19 am     Reply with quote

Read the manual......

It is down to understanding that in the PIC, you have a separate memory space for program memory, and for the RAM. Different addresses. On many of the older chips, there was no hardware ability to actually read a specified location in program memory. So a 'pointer' could not be constructed. This is the CCS default.
Now on modern chips the ability does exist, but for reverse compatibility, and because doing this involves a significant amount of extra code, the _ROM_ data type is used for a constant for which a pointer can be constructed (you can change this with a setting). Alternatively, if you use 'ANSI' for the compiler setup, 'CONST' is no longer stored in ROM, but becomes a 'read only' definition for a RAM variable, for which a pointer can then be constructed, but at the cost of using a lot more RAM....

Best Wishes
Sid2286



Joined: 12 Aug 2010
Posts: 119

View user's profile Send private message

PostPosted: Mon Apr 04, 2011 9:32 am     Reply with quote

I guess You should assign the value during declaration.. Try this..
const char *ptr= &product[0]
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon Apr 04, 2011 12:00 pm     Reply with quote

I made a test program that uses the "rom" keyword that Ttelmah refers to.
It displays the following output in the MPLAB simulator for vs. 4.119.
This output is correct.
Quote:

ptr = 0fef
PRODUCT ID V1.01
PRODUCT ID V1.01


Code:
#include <16F1824.h>
#fuses INTRC_IO,NOWDT
#use delay(clock=4000000)
#use rs232(baud=9600, UART1, ERRORS)

char rom version[] = "PRODUCT ID V1.01";
char rom *ptr;

void display_string(rom *str)
{
printf("%s \r", str);
}

//=====================
void main()
{
ptr = version;

printf("ptr = %lx \r", ptr);

display_string(version);
display_string(ptr);
   
while(1);
}
Pekub



Joined: 20 Dec 2005
Posts: 16

View user's profile Send private message Send e-mail

PostPosted: Tue Apr 05, 2011 3:11 am     Reply with quote

Thank you PCM programmer and Ttelmah also. This is what I needed.
In my opinion, use keyword "rom" is not sufficiently explained in the manual.
Ttelmah



Joined: 11 Mar 2010
Posts: 19267

View user's profile Send private message

PostPosted: Tue Apr 05, 2011 3:39 am     Reply with quote

The 'best' explanation, is the _readme_ with the compiler. This covers things that are relatively recent (though 'ROM' has been used for over a year!), and not fully in the manual. This gives the current syntax for ROM, and the keywords to change between the ROM, and const behaviour.
To work with a PIC, and CCS, you need to use the following bits of paperwork:

The C programming language or a near equivalent.
The chip's data sheet
The CCS manual
The readme.txt, and fuses.txt
The Microchip reference manuals for the specific peripherals and chip versions.

You then 'have a chance'!.... Smile

Best Wishes
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