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

Function and const pointer?

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



Joined: 09 Mar 2010
Posts: 314
Location: Denmark

View user's profile Send private message

Function and const pointer?
PostPosted: Mon Sep 20, 2010 12:16 pm     Reply with quote

Hi

I don't understand this. I have tested in real hw and in MPLAB and I can't get this to work.

The function T1 is _not_ working but T2 is working ok. The problem is only T1.

What I want is, to have some string or int, as a "const", then call a function with the &const and print it out. This it only for testing, the real program is something else.

Code:
const char s1[]={"String as Const"};

Code:
void T1(char *c, int len){
 int i;
//........ more
}

Code:
T1(&s1,sizeof(s1));



Some days ago one suggested my not to use "&"(address pointer because a array name is the address) in the cal from "main", but if I don't have the "&" in the calling function it wont compile.
The error is: "Attempt to create a pointer to a constant".

I really don't understand this?

The small test program can directly be compiled and tested in "MPLAB"

Code:
#include "18F2455.h"

#FUSES INTRC_IO         //Internal RC Osc, no CLKOUT
#FUSES MCLR             //Master Clear Pin Normal Reset Mode
#FUSES NOPUT            //No Power Up Timer
#FUSES NOBROWNOUT       //No brownout reset
#FUSES FCMEN            //Fail-safe clock monitor enabled
#FUSES NOLVP            //No low voltage prgming, B3(PIC16) or B5(PIC18) used for I/O
#Fuses PLL1             //No PLL
#FUSES CPUDIV1          //No System PostScaler

#use delay(clock=8M, internal)

#use rs232(BAUD=115200,UART1,ERRORS)

const char s1[]={"String as Const"};
rom   char s2[]={"String as ROM"};

char buffer[10];

void T1(char *c, int len){
 int i;
 
 printf("T1,Const:%s\n",c);
 
 for (i=0;i<len;i++)
  buffer[i]= c[i];
 
 printf("T1,RAM:%s\n",buffer);
}

void T2(int16 addr, int len){
 read_external_memory(addr,buffer,len);
 printf("T2,ROM:%s\n",buffer);
}

void main(void){
 printf("START!\n");
 
 while (1){
  T1(&s1,sizeof(s1));
  T2(s2,sizeof(s2));
  delay_ms(500);
 }
}



EDIT!!!
Finally I understand it, it is _not_ possible to pass a "const" pointer to a function! Only is using #device PASS_STRINGS=IN_RAM it is possible because then the compiler copy the string to ram first, and then I only pass the address pointer for the new ram location to the function.

Is there a downside to use #device PASS_STRINGS=IN_RAM or is the best way to use ROM and then read the program memory?
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon Sep 20, 2010 5:15 pm     Reply with quote

If you want to see what the compiler is doing with
PASS_STRINGS=IN_RAM, then compile the following test program and
look at the .LST file. Be sure to comment out the #nolist statement in
the .h file for your PIC, so the CCS library code will become visible in
the .LST file. This what you need to do in 18F452.h (for example):
Quote:

//////// Standard Header file for the PIC18F452 device ////////////////
#device PIC18F452
//#nolist

Look at the .LST file. You'll see two types of routines in low memory.
There will be 3 instances of the const array character fetch code. These
routines get a byte from the string arrays stored in Flash memory, and
return it (one byte per call).

The 2nd type of routine is the Print String routine. There's only one of
this type. It reads chars from RAM array and gives them to the UART's
transmitter so they can be sent.

Then in main(), there are 3 blocks of code, one for each line in main(),
and these blocks call the character fetch routines in a loop, and they
copy the const array strings to a RAM array. Each display_string() line
has it's own separate block of code to copy its const string to the temp
RAM string.

If you look in the .SYM file, you will see 12 bytes of RAM allocated for
this temporary RAM array. There is only one temp RAM array. It's large
enough to hold any of the const arrays (one at a time). Here is the line
for it in the .SYM file:
Quote:
005-010 MAIN.@STRPARAM

The const strings are copied to the RAM array and then it's passed to the
display_string() function.

Code:

#include <18F452.h>
#device PASS_STRINGS=IN_RAM
#fuses XT, NOWDT, NOPROTECT, BROWNOUT, PUT, NOLVP
#use delay(clock=4000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)

unsigned char const test1[] = {"Hello World"};
unsigned char const test2[] = {"Hi There"};
unsigned char const test3[] = {"0123456789"};


void display_string(unsigned char *ptr)
{
printf(ptr);
}

//====================================
void main()
{
display_string(test1);
display_string(test2);
display_string(test3);


while(1);
}
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