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

PIC18 variable legnth string arrays

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



Joined: 21 Feb 2007
Posts: 142
Location: Michigan, USA

View user's profile Send private message

PIC18 variable legnth string arrays
PostPosted: Thu Mar 15, 2007 8:13 am     Reply with quote

Has anyone had any success with variable length string arrays?

This looks like it should work, but when it runs the pointer for the rom_string points to a blank area of rom.

Code:

#include "18F65J10.h"
#include "string.h"



unsigned int8 const rom_string[ 2 ][ * ] = { "1234567890", "string 2"};

unsigned int8 ram_string[ 32 ];


void main( void )
{

   strcpy( ram_string, rom_string[ 1 ] );

}





I can get the program to work correctly by using fixed size arrays as seen below, but I would prefer to use variable size.


Code:

#include "18F65J10.h"
#include "string.h"



unsigned int8 const rom_string[ 2 ][ 32 ] = { "1234567890", "string 2"};

unsigned int8 ram_string[ 32 ];


void main( void )
{

   strcpy( ram_string, rom_string[ 1 ] );

}



I'm using CCS version 4.029

Thanks
Kevin
Ttelmah
Guest







PostPosted: Thu Mar 15, 2007 10:14 am     Reply with quote

In common with much of the 'new' stuff, not everything works as you'd expect. Your code will run OK, if you don't use variable length strings. Variable length strings, seem to only support being 'used' as entities (to printf statements etc.), and don't work for pointers. There is a degree of logic here, since it would be very hard for the pointer arithmetic to 'know' where the latter elements are in the array, with variable length strings.
You can actually 'cheat', by not using printf.
This will do what you want (a bodge...):
Code:

const unsigned int8 rom_string[2 ][11 ] = { "1234567890", "string 2"};

unsigned int8 ram_string[ 32 ];
int8 ctr;

void to_str(int8 b) {
   ram_string[ctr++]=b;
}

void main( void ) {
   ctr=0;
   to_str(rom_string[1]);
   while (true) ;
}


This passes the string to an output function, which puts it byte by byte, into the RAM string. you can just reset ctr, and feed another string as wanted.

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