Thread: Access Char Array

  1. #1
    Registered User
    Join Date
    Nov 2012
    Posts
    5

    Question Access Char Array

    Hi folks,
    I am trying to set up a list of menu choices for a small device. Right now, it pulls the choices from a resource file, which makes updating the list of choices rather clunky.
    What I want to do is declare the list of choices in an array, and show them up on screen one by one as the user cycles through the menu.
    Unfortunately, this process is further complicated by the fact that the device's code page is UTF16 - yuck!
    Here's what I have so far.
    In the global variables section, I set up the constant char arryay of choices.
    Code:
    const char* menuChoices[] = {
    "Increase Brightness",
    "Decrease Brightness",
    "Check Temp",
    "Save Temp",
    "Exit"
    }; //menuChoices
    These will be individually copied into the TextToShow variable, which is picked up by the display function in another module.
    Code:
    extern char  TextToShow[];
    Then, when the user presses the Next button, I try to copy the next choice into the VarToShow variable.
    Code:
    memset (TextToShow, 0, 128);
    memcpy (TextToShow, L**menuChoices[uCurrentChoiceIndex], 44);
    Naturally, this fails on two counts, one because I can't use the L macro like that, and second because I'm not accessing the data properly.
    I have tried to use _TEXT() and TEXT() to no avail - it says that function is undeclared. I'm out of ideas.
    What does work is this:
    Code:
    memcpy (TextToShow, L"Increase Brightness", 59);
    but I want the choices to be in an array, instead of buried halfway through the code in a series of ugly if statements.
    Finally, if you have a tip on getting the correct length of the string being copied (it seems to be 39+LengthOfAsciiString), I would be most grateful!
    Thanks everyone for looking! Sorry, I've been writing Basic code for years, but C is a little new to me!

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    How about simply
    strcpy(TextToShow,menuChoices[uCurrentChoiceIndex]);
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Nov 2012
    Posts
    5

    Thumbs up We're Almost There

    Thank you so much for the suggestion of strcpy. I'm very excited - the application did compile, but I am afraid to test it yet.
    So, I have two final questions, if you don't mind.
    1. I don't think strcpy will convert the string to unicode, as L"Hello" will. How do I convert the copied string to Unicode? The only way I know is to use L, but I can't figure out how to do that with a variable.
    2. There is also a section of code where I need to pass the text to a function to redraw the menu (instead of using strcpy).
    Code:
    window_SetProperty (WindowHandle, PROPERTY_TEXT_STRING, TXT_STRING_TYPE_UTF16, L"Hello");
    Could you possibly suggest a method to use menuChoices[uCurrentChoiceIndex] instead of the text in quotes, and still have it in Unicode?
    Thank you so much, everyone for your help! I appreciate it so very much.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > I am trying to set up a list of menu choices for a small device.
    So what compiler are you using?
    Does it come with a library (like libc) to help you program it. Some URL links would be super if you can't find what you're looking for in the documentation.

    If part of the device is based around UNICODE, then there should be some function to convert between string formats.
    For the sake of argument:
    convertAsciiToUnicode(TextToShow,128,menuChoices[uCurrentChoiceIndex]);


    ASCII to UNICODE is a snap to do anyway with a simple loop.

    But if it is UTF16, I'm puzzled why the transfer array "extern char TextToShow[];" is declared as char.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Nov 2012
    Posts
    5
    Thanks so much for the reply. It is the GHS compiler. I don't know why TextToShow is a char, either, that baffled me as well. All I know is L"Hello" worked, and without the L the device froze. It looks like all I need to do is convert menuChoices[1] from a char to a wchar_t. The device expects a wide-format multibyte char. Is there a function to convert my char to a wchar_t, or do I need to declare my const menuChoices as a wChar_T? Thanks so much for all your help!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. char array doesn't print the char in the array
    By KiRa11Love in forum C Programming
    Replies: 4
    Last Post: 09-07-2012, 05:52 AM
  2. uincode char array to array<unsigned char,1>^
    By ripspinner in forum Windows Programming
    Replies: 5
    Last Post: 12-14-2009, 05:41 PM
  3. Replies: 3
    Last Post: 11-17-2008, 12:36 PM
  4. signed char array to unsign char array.
    By beon in forum C Programming
    Replies: 5
    Last Post: 12-14-2006, 07:19 PM
  5. Wrong access for char *string[]
    By intmail in forum C Programming
    Replies: 6
    Last Post: 11-15-2005, 11:05 AM