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

[Solved] Passing const string arrays

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



Joined: 13 Jul 2018
Posts: 23

View user's profile Send private message

[Solved] Passing const string arrays
PostPosted: Fri Jul 13, 2018 5:32 am     Reply with quote

Compiler V5.068
PIC 33EP512MC806

I am trying to write a function which prints an array of strings from ROM, but in contrast to similar questions I have read on this issue, I want to pass the string array as a parameter.

I get something like this

Code:

const char MAIN_TEXT[][TEXT_LENGTH] = {
    "Main Menu",
    " (1) Thing 1",
    " (2) Thing 2"
};

const char THING1_TEXT[][TEXT_LENGTH] = {
    "Thing 1 Menu",
    ....
};

void print_menu(rom char *menu_text) {
    char buffer[TEXT_LENGTH];
    sprintf(buffer, "%s", menu_text);
    while( strlen(buffer) > 0) {
        fprintf(OUTPUT_STREAM, buffer);
        menu_text += TEXT_LENGTH;
        sprintf(buffer, "%s", menu_text);
    }
}

void main() {
    ....
    print_menu(MAIN_TEXT);
    ....
}


But the result of this is menu_text only contains the first string from whichever array is passed. This seems to make sense given what I have read: a copy of the string from ROM is being created in RAM as needed, and since passing MAIN_TEXT is treated as if I had passed &MAIN_TEXT[0] I only get the first string.

I have tried instead passing a pointer to MAIN_TEXT and correspondingly modifying my print function, but to no success.

How might I go about solving this?


Last edited by SamMeredith on Fri Jul 13, 2018 7:33 am; edited 1 time in total
Ttelmah



Joined: 11 Mar 2010
Posts: 19546

View user's profile Send private message

PostPosted: Fri Jul 13, 2018 7:12 am     Reply with quote

There are several separate problems...

First strlen, does not support operation with a rom pointer.
Then you need to declare the array as 'rom', rather than const, to allow the pointer to be constructed.
Then your array needs to contain a zero length final string for the exit condition to be met.

Code:

#define TEXT_LENGTH 14

rom char MAIN_TEXT[][TEXT_LENGTH] = {
    "Main Menu",
    " (1) Thing 1",
    " (2) Thing 2",
    "" //to give a zero length final string
};

#include <stdlib.h>
#include <string.h>

void print_menu(rom char *menu_text) {
    char buffer[TEXT_LENGTH];
    while (*menu_text != '\0') {   
        fprintf(OUTPUT_STREAM, "%s\n",menu_text);
        menu_text += TEXT_LENGTH; 
    }
}

void main()
{
   print_menu(MAIN_TEXT);
   while(TRUE)
   {
      delay_cycles(1);
   }
}


This shows how to handle such a table. Smile
SamMeredith



Joined: 13 Jul 2018
Posts: 23

View user's profile Send private message

PostPosted: Fri Jul 13, 2018 7:32 am     Reply with quote

Wow, just like that!

Thank you very much, that has solved my problem.
Ttelmah



Joined: 11 Mar 2010
Posts: 19546

View user's profile Send private message

PostPosted: Fri Jul 13, 2018 7:46 am     Reply with quote

Using pointers to ROM is a little bit of a 'black art', I've been one of the people who has played with it from when they were first introduced, and have met most of the issues at one time or another!... Smile
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