It is currently 16 Oct 2018 13:23

All times are UTC + 1 hour




Post new topic Reply to topic  [ 5 posts ] 
Author Message
PostPosted: 02 Aug 2013 21:57 
Offline

Joined: 23 Feb 2008 09:37
Posts: 109
Location: Sheffield, UK
Hi,

Putting a string in ROM by const char[] M0 = "This is a string"; should allow me to return 'strlen' so that I may read it character by character with -

code_len = strlen(*M0) - 1; // Get code length

for (char_pos = 0; char_pos < code_len; char_pos++) {

msg_char = M0[char_pos]; // Output character
|
| more code;
|
}

Unfortunately it returns a fixed 0x52 length no matter what string I use.

Is there a fix or what am I doing wrong? I hope my pointer syntax is correct!

Cheers - Joe


Top
 Profile  
 
PostPosted: 02 Aug 2013 23:02 
Offline

Joined: 23 Feb 2008 09:37
Posts: 109
Location: Sheffield, UK
Hi,

While I'd still like to know how to recover a string length from ROM, I've just realized the obvious answer to my problem. As the input string is a constant, all I have to do is count the characters in it and use that result as my length.

Somedays it doesn't pay to get out of bed.

Cheers- Joe


Top
 Profile  
 
PostPosted: 03 Aug 2013 05:53 
Offline

Joined: 14 Feb 2011 04:07
Posts: 1119
Location: Bar, Montenegro
strlen function prototype looks like this:
int strlen(char *s);
That means it can only be used for strings located in RAM.

You will need to write your own function if you wish to obtain the length of the string located in ROM. The prototype of that function will look like this:
int strlen_rom(const char *s);

Here's the complete example:
const char M0[] = "This is a string";

int strlen_rom(const char *s) {
   int i = 0;
   
   while (*s++)
      i++;

   return i;
}

void main() {
   int len = strlen_rom(M0);    // len = 16
}

ximac wrote:
While I'd still like to know how to recover a string length from ROM, I've just realized the obvious answer to my problem. As the input string is a constant, all I have to do is count the characters in it and use that result as my length.

Somedays it doesn't pay to get out of bed.
There are some cases where this approach won't work and you will be forced to use your own function like the one in the previous example. Although you must initialize constant string in code, that doesn't mean it will remain the same during the execution of the program. For example, what if you declare a const string in your code at a specific address in ROM using absolute directive and then you do some FLASH writing in your program at that same address?

One more thing to note about "pointer syntax", as you call it.
If M0 is declared as: const char M0[] = "This is a string"; then:
1) M0 is an array
2) M0 is also a pointer to the array's first element (same as &M0[0])
3) *M0 is a dereferenced pointer, i.e. the value stored at the location pointed by M0 (same as M0[0])

Regards


Top
 Profile  
 
PostPosted: 03 Aug 2013 11:45 
Offline

Joined: 18 Feb 2006 13:17
Posts: 4980
There's also the sizeof operator that's suitable for compile-time size estimation:
code_len = sizeof (M0) - 1; // Get code length

_________________
Replacement libraries for mP PRO and PIC18 processors, mP PRO tips & trics


Top
 Profile  
 
PostPosted: 07 Aug 2013 11:49 
Offline

Joined: 23 Feb 2008 09:37
Posts: 109
Location: Sheffield, UK
Hi,

Thank you aCkO and janni for your inputs. I have taken both on-board and will experiment with them. I am only just learning C and so get a little confused since at times it seems to be somewhat arbitrary. BTW, the routine I am trying to write is for a Morse tutor that pulls long, preordained strings (up to 200 characters) out of ROM and so their content and length will not be changing.

Thanks - Joe


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 5 posts ] 

All times are UTC + 1 hour


Who is online

Users browsing this forum: Google [Bot] and 10 guests


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 post attachments in this forum

Search for:
Jump to: