[SOLVED] Character array in C

Status
Not open for further replies.

hemnath

Advanced Member level 3
Joined
Jun 24, 2012
Messages
702
Helped
61
Reputation
120
Reaction score
57
Trophy points
1,308
Location
Chennai
Activity points
6,589
Hi all,

I have an array like this,

const unsigned char menu[2][6][8] = {
{"0 BAR ", "0.25 BAR", "0.5 BAR", "0.75 BAR", "1.0 BAR ", "EXIT "},
{"0 BAR ", "0.50 BAR", "1.0 BAR", "1.50 BAR", "2.0 BAR ", "EXIT "}
};

How can I call the particular array value to display.

I tried using
Code:
sprintf(temp_value, "%s", MENU[0][1]);
// in a compiler.
It doesn't works. Please help.
 

Try it with.
sprintf(temp_value, "%s", &MENU[0][1])
 

Thanks for the reply. But it doesn't works.
 

It works:
Code:
#include <stdio.h>

int main()
{
    const unsigned char menu[2][6][8] = {
{"0 BAR ", "0.25 BAR", "0.5 BAR", "0.75 BAR", "1.0 BAR ", "EXIT "},
{"0 BAR ", "0.50 BAR", "1.0 BAR", "1.50 BAR", "2.0 BAR ", "EXIT "}
};
    printf("%s\n", menu[0][2]);

    return 0;
}
Code:
$gcc -o main *.c
$main
0.5 BAR
 

Hi,

Thanks for the reply. But it doesn't works.
This is no error description and thus it does not help to encircle the problem.

--> please say
* how you test it (code)
* what you expect
* and what happens instead.

*****
Maybe it's a problem of the string delimiter. Often a 0x00 is used as string delimiter.
Try to insert it after each of your texts.

Klaus
 
Maybe it's a problem of the string delimiter. Often a 0x00 is used as string delimiter.
Try to insert it after each of your texts.
Almost hit the point. \0 delimiter is inserted automatically in char string constants, you don't need to write it explicitly. Problem of the code is however that it has no room for the delimiter, some strings are already 8 characters long, string length must increased to [9].

Depending on the unsaid processor and compiler, there may be an additional problem with ROM versus RAM strings.
 
hello,

exemple of use with a display terminal
with MikroC

it doesn't works with sprintf, maybe because parameter is a Const char ( not in RAM)

Code:
const unsigned char Menu[2][6][9] =
{
{"0.00 BAR", "0.25 BAR", "0.50 BAR", "0.75 BAR", "1.00 BAR", "  EXIT  "}
,
{"0.00 BAR", "0.50 BAR", "1.00 BAR", "1.50 BAR", "2.00 BAR", "  EXIT  "}
};

Nota: same texte width in the menu !

// --- Copie le texte depuis FLASH ROM vers RAM
void strConstRamCpy(unsigned char *dest, const code char *source)
 {
  while (*source)*dest++ = *source++ ;
  *dest = 0 ;    // terminateur
}

void main()
{
 ....init Hardware & UART1

  UART1_Write_CText("Menu 1 :\r\n");
   for (i=0;i<6;i++)
   {
     strConstRamCpy(txt,Menu[0][i]) ; // --- Copie le texte depuis FLASH ROM vers RAM
     UART1_Write_Text(txt)   ;
    CRLF1();
   }
    CRLF1();
     UART1_Write_CText("Menu 2 :\r\n");
   for (i=0;i<6;i++)
   {
     strConstRamCpy(txt,Menu[1][i]) ;
     UART1_Write_Text(txt)   ;
    CRLF1();
   }
   while(1);
   
 }


result on display
Menu 1 :
0.00 BAR
0.25 BAR
0.50 BAR
0.75 BAR
1.00 BAR
EXIT

Menu 2 :
0.00 BAR
0.50 BAR
1.00 BAR
1.50 BAR
2.00 BAR
EXIT
 

Status
Not open for further replies.

Similar threads

Cookies are required to use this site. You must accept them to continue using the site. Learn more…