Const string and array

General discussion on mikroC.
Post Reply
Author
Message
olivierh
Posts: 8
Joined: 21 Mar 2005 21:57
Location: France

Const string and array

#1 Post by olivierh » 07 Jun 2005 21:23

I come back with my array ...

I use MikroC 2.0.0.3.

The following code compile :

Code: Select all

void menu () {
      short i;
      char m1[] = "Calibration";
      char m2[] = "Sauve config.";
      char m3[] = "Quitter";
      short code;
      char scode[5];

      // char * m[] = {m1, m2, m3};
      char * m[4];
      m[0] = m1;
      m[1] = m2;
      m[2] = m3;
      m[3] = '\0';

      for (i = 0 ; i < 3; i++) {
          LCD_Out (1, 1, m[i]);
          Delay_ms (200);
       }
but if I try

Code: Select all

      const char m1[] = "Calibration";
      const char m2[] = "Sauve config.";
      const char m3[] = "Quitter";
      short code;
      char scode[5];

      // char * m[] = {m1, m2, m3};
      char * m[4];
      m[0] = m1;     // <-- Illegal cast
      m[1] = m2;     // <-- Illegal cast
      ....
does not compile.

When I don't use const, th following code does not compile :

Code: Select all

for (i = 0 ; m[i] != '\0'; i++) {
I get an
Operator [] not applicable to this operands []

gambrose
Posts: 369
Joined: 28 Sep 2004 17:34
Location: uk

#2 Post by gambrose » 08 Jun 2005 09:57

you need to declare m as a array of pointers to constants not variables.
Graham Ambrose

pizon
mikroElektronika team
Posts: 823
Joined: 11 Oct 2004 08:53

Re: Const string and array

#3 Post by pizon » 08 Jun 2005 09:58

You have to use it this way:

Code: Select all

const char m1[] = "Calibration";
const char m2[] = "Sauve config.";
const char m3[] = "Quitter";

short code;
char scode[5];
char msgl;

void main() {
      //const char * m[] = {m1, m2, m3};
      const char * m[4];    // <-- This is how it should be done
      m[0] = m1;            // Now OK
      m[1] = m2;            // Now OK

      msgl = *(m[0] + 3);   // This moves the letter 'i' to the msgl variable
}//~!
The char and const char are different types and typecasting between them is not allowed.

Considering the
for (i = 0 ; m != '\0'; i++)
m is an array of pointers to char. By assigning the '\0' value to it, you created a null pointer, however this time the escape sequence didn't produce the expected result (0x000000), but something different (0x00002C). This could be a bug (and we'll take a look further to it), but the general advice is to use the escape sequences as little as possible, to avoid the ambiguities that might arise. Speaking of which, the sequence '\000' compiles properly to 0x000000. So, in order to compile it properly, you should write

Code: Select all

m[4] = 0 ;  // Explicitly declaring null pointer
...
for (i = 0 ; m[i] != 0; i++)  {...
, or, in your style

Code: Select all

m[4] = '\000';   // This will work also, but is not recommended
...
for (i = 0 ; m[i] != '\000'; i++) {...
For further explanations, please read the "Pointers" and "Escape Sequences" chapters from mikroC's Help.
pizon

olivierh
Posts: 8
Joined: 21 Mar 2005 21:57
Location: France

Re: Const string and array

#4 Post by olivierh » 08 Jun 2005 20:01

[quote="pizon"]You have to use it this way:

Code: Select all

const char m1[] = "Calibration";
const char m2[] = "Sauve config.";
const char m3[] = "Quitter";

short code;
char scode[5];
char msgl;

void main() {
      //const char * m[] = {m1, m2, m3};
      const char * m[4];    // <-- This is how it should be done
      m[0] = m1;            // Now OK
      m[1] = m2;            // Now OK

      msgl = *(m[0] + 3);   // This moves the letter 'i' to the msgl variable
}//~!
The char and const char are different types and typecasting between them is not allowed.


Ok, gambrose is right, I have to declare m as a array of pointers to constants. So if I use

Code: Select all

const char * m[4];
the following code

Code: Select all

LCD_Out (1, 1, m[i]);
give an Illegal cast error.

How can I display a constant string to the LCD ?

gambrose
Posts: 369
Joined: 28 Sep 2004 17:34
Location: uk

#5 Post by gambrose » 08 Jun 2005 20:41

well you can either load the const string into a variable string then call LCD_Out or my personal choice is to write your own function that takes a pointer to a const char and loops through the string calling LCD_Chr_CP.
Graham Ambrose

olivierh
Posts: 8
Joined: 21 Mar 2005 21:57
Location: France

#6 Post by olivierh » 08 Jun 2005 21:44

gambrose wrote:well you can either load the const string into a variable string then call LCD_Out
So I have to loops through the string to copy it in a variable, and call LCD_Out which perhaps call LCD_Chr_CP
gambrose wrote:or my personal choice is to write your own function that takes a pointer to a const char and loops through the string calling LCD_Chr_CP.
I think it's a good choice.
I'll try tomorrow.

Post Reply

Return to “mikroC General”