|
|
View previous topic :: View next topic |
Author |
Message |
kevcon
Joined: 21 Feb 2007 Posts: 142 Location: Michigan, USA
|
PIC18 variable legnth string arrays |
Posted: Thu Mar 15, 2007 8:13 am |
|
|
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
|
|
Posted: Thu Mar 15, 2007 10:14 am |
|
|
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 |
|
|
|
|
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
|