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

Passing str

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








Passing str
PostPosted: Fri Aug 04, 2006 3:08 am     Reply with quote

Hi

I try to pass a string constant into a function only to receive jarbled memory as a result. Manually malloc'ing the memory and then passing the pointer in works, however. Can someone tell me why?

Function:
Code:

void debug(unsigned char *msg)
{
   int i = 0;

   for(i = 0; msg[i] != '\0'; i++)
      putc(msg[i]);
}


Call that works:
Code:

        unsigned char *buffer;
        buffer = (unsigned char *) malloc(30);
        strcpy(buffer, "This is a test.");
        debug(buffer);
        free(buffer);


Call that doesn't work:
Code:

        debug("This is a test");


I will add that I am using a PIC18F2525.
Ttelmah
Guest







PostPosted: Fri Aug 04, 2006 4:21 am     Reply with quote

Read the manual....
It is in the 'frequently asked questions', under 'How can a constant data table be placed in ROM'.
You cannot have a pointer to a constant string, and though you use the 'array' construct to try to access the data in the function, the actual construct is using a pointer to hand the address from one location to another.
It is possible to do, using 'typemod', but this is a bulky bodge. It is a limitation of the current CCS C, bought about by the fact that the processor architecture, has seperate RAM, and ROM data spaces.
If your function is only ever going to be used with constant strings, then this will work:
Code:

void debug(int char) {
    putc(char);
}

debug("This is a test");


Best Wishes
Guest








PostPosted: Fri Aug 04, 2006 5:11 am     Reply with quote

Ttelmah wrote:
Read the manual....
It is in the 'frequently asked questions', under 'How can a constant data table be placed in ROM'.
You cannot have a pointer to a constant string, and though you use the 'array' construct to try to access the data in the function, the actual construct is using a pointer to hand the address from one location to another.
It is possible to do, using 'typemod', but this is a bulky bodge. It is a limitation of the current CCS C, bought about by the fact that the processor architecture, has seperate RAM, and ROM data spaces.
If your function is only ever going to be used with constant strings, then this will work:
Code:

void debug(int char) {
    putc(char);
}

debug("This is a test");


Best Wishes

Hi, thanks, I now realise that it isn't possible, but I don't understand what your example code is trying to do..?
Ttelmah
Guest







PostPosted: Fri Aug 04, 2006 6:31 am     Reply with quote

It'll do exactly what your code was trying to do!.
It is a 'non standard extra', in CCS C, that if you call a function defined to accept an int, with a constant string as the 'value', it'll repeatedly call the function, with each character in turn of the constant string. So 'debug', will be called with 'T', then with 'h', then with 'i' etc., for the whole constant string.

Best Wishes
Guest








PostPosted: Fri Aug 04, 2006 7:24 am     Reply with quote

wow, thanks for that tip! it will definately come in handy!

although I don't need it for now as I just discovered from the manual that printf() basically does what I want, even sending through the rs232 pins!

thanks!
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