Hot!Array of strings?

Page: 12 > Showing page 1 of 2
Author
jlawton11
Junior Member
  • Total Posts : 92
  • Reward points : 0
  • Joined: 2011/05/04 15:44:43
  • Location: 0
  • Status: offline
2019/02/18 19:58:47 (permalink)
0

Array of strings?

I have a small, old-style LCD ASCII character display. I generally feed it what I'd refer to as "strings" but they're fixed-length
(all the same length) without nulls between them. What I need to do is to try and translate the assembly code (that was working but the X IDE assembler wasn't reliable or consistent) into XC8 (what I'm saying is correct, no need to refight the Civil War here), in assembly this was pretty simple because I could just do the math on a byte counter into the call of RETLW's. Trouble is the most direct translation into C is to try and mimic the "pointer math" (yes some of you are cringing, like I care what you call it) and from what I know ANSI C doesn't normally let you do that. I certainly don't want to make selection of a string into a case statement and have to give each one a name, that's ridiculous! Barring that is there some way I can simply declare an "array of strings" (and I don't intend to assign each a pointer) so I can just introduce the string index and "trick" the compiler into doing the pointer math and being happy about it? (Oh come on, it's not like SOMEBODY out there hasn't had the same issue, like you're just so ANSI you wouldn't THINK of anything so corrupt...!) I don't want to have to "crawl through" the whole mega-string before I get there either if I can help it. Or maybe (as a last-ditch) you'd have to use a little inlining to get there? Well, any ideas that you've actually successfully implemented?
#1

22 Replies Related Threads

    NorthGuy
    Super Member
    • Total Posts : 5256
    • Reward points : 0
    • Joined: 2014/02/23 14:23:23
    • Location: Northern Canada
    • Status: offline
    Re: Array of strings? 2019/02/18 20:40:23 (permalink)
    +1 (1)

    char * array_of_strings[] = {"string one", "string two", "string three"};

    #2
    Gort2015
    Klaatu Barada Nikto
    • Total Posts : 3038
    • Reward points : 0
    • Joined: 2015/04/30 10:49:57
    • Location: 0
    • Status: offline
    Re: Array of strings? 2019/02/18 20:41:54 (permalink)
    -1 (1)
    Just basic indexing:
    const char *strtable = "abcdefGHIjkl", *idxptr;
    int len = 3, idx = 2;
     
    idxptr = strtable + idx * len;
    lcd_string(idxptr, len); //GHI

    LCD string prototype:
    int lcd_string(const char *String, int Length);
     
     
    Big speech.
    post edited by Gort2015 - 2019/02/18 20:49:52

    MPLab X playing up, bug in your code? Nevermind, Star Trek:Discovery will be with us soon.
    https://www.youtube.com/watch?v=Iu1qa8N2ID0
    + ST:Continues, "What Ships are Made for", Q's back.
    #3
    Gort2015
    Klaatu Barada Nikto
    • Total Posts : 3038
    • Reward points : 0
    • Joined: 2015/04/30 10:49:57
    • Location: 0
    • Status: offline
    Re: Array of strings? 2019/02/18 20:49:09 (permalink)
    -1 (1)
    "trick" the compiler?
    What does even that mean?
     
     

    MPLab X playing up, bug in your code? Nevermind, Star Trek:Discovery will be with us soon.
    https://www.youtube.com/watch?v=Iu1qa8N2ID0
    + ST:Continues, "What Ships are Made for", Q's back.
    #4
    jlawton11
    Junior Member
    • Total Posts : 92
    • Reward points : 0
    • Joined: 2011/05/04 15:44:43
    • Location: 0
    • Status: offline
    Re: Array of strings? 2019/02/18 21:09:31 (permalink)
    0
    It means you're obviously adding an integer type to a pointer type and expecting to still have a pointer type. Now that doesn't personally bother ME all that much, and in embedded you sometimes get away with more stuff, then again in Harvard architectures you sometimes get less. Nonetheless some compilers "scream bloody murder" for a lot less especially if they're "preaching strict ANSI", unless ANSI C has softened up quite a bit since I last encountered it (this sort of thing was fine all day long with good ol' K&R C but then the academics came along and ruined everything as usual). If you know it works with XC8 that's all I need to know, but I'm new to this compiler and the fewer "wild goose chases" I have to run to figure it out the better.
    #5
    NKurzman
    A Guy on the Net
    • Total Posts : 17002
    • Reward points : 0
    • Joined: 2008/01/16 19:33:48
    • Location: 0
    • Status: online
    Re: Array of strings? 2019/02/18 21:18:59 (permalink)
    0
    If the strings are a fixed length then a two dimensional array of const chars will also work and be more space efficient. Which is more space efficient depends on the data.
    #6
    jlawton11
    Junior Member
    • Total Posts : 92
    • Reward points : 0
    • Joined: 2011/05/04 15:44:43
    • Location: 0
    • Status: offline
    Re: Array of strings? 2019/02/18 21:27:31 (permalink)
    0
    I like that concept, I wonder if a const char could be a union set up with the other side as a one-dimensional array of strings so I don't have to type as much to set it up. Sounds like it's worth trying!
    #7
    qhb
    Superb Member
    • Total Posts : 9182
    • Reward points : 0
    • Joined: 2016/06/05 14:55:32
    • Location: One step ahead...
    • Status: online
    Re: Array of strings? 2019/02/18 21:28:02 (permalink)
    +1 (1)
    jlawton11
    ...
    Trouble is the most direct translation into C is to try and mimic the "pointer math" (yes some of you are cringing, like I care what you call it) and from what I know ANSI C doesn't normally let you do that.

    It's perfectly legal in "ANSI C" to add an integer offset to a pointer.
     

    is there some way I can simply declare an "array of strings" (and I don't intend to assign each a pointer) so I can just introduce the string index and "trick" the compiler into doing the pointer math and being happy about it?

    The solution proposed by NorthGuy does exactly that, giving you an array of pointers to variable length, NULL terminated strings.
    Not what you were doing in assembly, but the standard way you'd do it in C.
    You could do exactly what your assembly did as well, using fixed length and no terminator, but then you have to do all your own string handling as well rather than just using the normal C string handling.
     

    (Oh come on, it's not like SOMEBODY out there hasn't had the same issue, like you're just so ANSI you wouldn't THINK of anything so corrupt...!)

    Why persist with this tiresome cynical attitude? This is first year C programming, nothing fancy.
     

    I don't want to have to "crawl through" the whole mega-string before I get there either if I can help it. Or maybe (as a last-ditch) you'd have to use a little inlining to get there? Well, any ideas that you've actually successfully implemented?

    Now this is just silly pontificating.
     
     
    #8
    Mysil
    Super Member
    • Total Posts : 3204
    • Reward points : 0
    • Joined: 2012/07/01 04:19:50
    • Location: Norway
    • Status: offline
    Re: Array of strings? 2019/02/18 23:12:03 (permalink)
    +2 (2)
    Hi,
    Mr. Lawton, you are writing bullshit.
     
    As far as I understand,  
    ISO/IEC 9899:1999 aka. C99, ISO/IEC 9899:2011 aka C11, and ISO/IEC 9899:2018 aka C18
    are a little more strict than: ANSI C aka. C89, same as ISO/IEC 9899:1990 aka. C90,
    In that the Standard require Compiler to issue a warning when you are about to do something stupid.
     
    Pointer arithmetic have always been a feature of the C programming language,
    and still is available with the current standards.
    With C99 and later, you have to do it correctly,
    which may require use of some casting operators to indicate that you know what you are doing.
     
    In CX8 version 1.xx,  there is a pitfall when compiling for PIC16 devices.
    In PIC16 devices, a default pointer is 8 bits, since each page in RAM memory is only 128 bytes,
    so if you do pointer arithmetic on that, there is a possibility that calculation may overflow, and give unexpected result. I have had code that work on PIC18, but failed for PIC16.
     
    C99 is also more permissive in some areas, in that  // line comments are formally allowed,
    and declarations anywhere within a block is allowed.
     
    It is now soon 20 years since C99 was published, and it is quite recently that Microchip released XC8 v2... with C99 support. This is nothing special, there are also other compilers that are slow to uppgrade to currrent standard.
    It also work the other way, in that many features specified in standards have been available as extensions for a long time.
     
    What you are complaining about have nothing to do with Harward architecture.
     
        Mysil
    #9
    NKurzman
    A Guy on the Net
    • Total Posts : 17002
    • Reward points : 0
    • Joined: 2008/01/16 19:33:48
    • Location: 0
    • Status: online
    Re: Array of strings? 2019/02/18 23:30:56 (permalink)
    0
    I suppose it could, but why. You appear to be trying to duplicate a custom ASM solution.
    Standard C gives you several simple clean ways to deal with strings.
    #10
    malaugh
    Super Member
    • Total Posts : 375
    • Reward points : 0
    • Joined: 2011/03/31 14:04:42
    • Location: San Diego
    • Status: offline
    Re: Array of strings? 2019/02/19 08:14:17 (permalink)
    0
    You can easily declare an array of fixed length strings
     

    #define FIXED_STRING_LENGTH 12
    typedef const char LCD_STRING[FIXED_STRING_LENGTH];
     
    LCD_STRING StringArray[] = {
    "Goodbye",
    "Cruel",
    "World"
    };
     
    void main(void)
    {
    char *StrPtr = (char *)StringArray[2];
    printf("%s", StrPtr);
    }

     
    In the code
    The "define" tells the compiler to substitute 12 for every instance of FIXED_STRING_LENGTH 
    The typedef defines an object called LCD_STRING, which is a string of 12 characters
    The next line declares and array of LCD_STRING objects and initializes them with the strings
    The code in main just gives and example of how to manipulate the strings
     
    #11
    NorthGuy
    Super Member
    • Total Posts : 5256
    • Reward points : 0
    • Joined: 2014/02/23 14:23:23
    • Location: Northern Canada
    • Status: offline
    Re: Array of strings? 2019/02/19 09:16:13 (permalink)
    0
    If you want them all the same length:
     
    char fixed_length_strings[] =
      "String One     "
      "String Two     "
      "String Three   "
    ;

    #12
    1and0
    Access is Denied
    • Total Posts : 8740
    • Reward points : 0
    • Joined: 2007/05/06 12:03:20
    • Location: Harry's Gray Matter
    • Status: offline
    Re: Array of strings? 2019/02/19 09:38:00 (permalink)
    0
    NorthGuy's code in Post #12 will give you that table of RETLW's from your (PIC16 ?) assembly code, if that is what you wanted. If you don't want the NUL character '\0' at the end, limit the size of the array short of the NUL character. ;)
    #13
    Chris A
    Super Member
    • Total Posts : 818
    • Reward points : 0
    • Joined: 2010/07/20 04:37:07
    • Location: 0
    • Status: offline
    Re: Array of strings? 2019/02/19 10:13:44 (permalink)
    0
    NorthGuyIf you want them all the same length:
    char fixed_length_strings[] =
     
      "String One     "
      "String Two     "
      "String Three   "
    ;

    Don't be confused by there being no commas after each string.  The compiler joins all three into one string.  You can then index along like your asm code does.  But you would be better letting the compiler do the work using an array like answer #11 or #2.  They also would catch typos better.
    #14
    1and0
    Access is Denied
    • Total Posts : 8740
    • Reward points : 0
    • Joined: 2007/05/06 12:03:20
    • Location: Harry's Gray Matter
    • Status: offline
    Re: Array of strings? 2019/02/19 10:38:00 (permalink)
    +1 (1)
    jlawton11
    I have a small, old-style LCD ASCII character display. I generally feed it what I'd refer to as "strings" but they're fixed-length
    (all the same length) without nulls between them. What I need to do is to try and translate the assembly code (that was working but the X IDE assembler wasn't reliable or consistent) into XC8 (what I'm saying is correct, no need to refight the Civil War here), in assembly this was pretty simple because I could just do the math on a byte counter into the call of RETLW's.

    Assuming length of 16 characters, it is simple in C too:
    #define FIXED_LEN 16U
    char fixed_length_strings[3*FIXED_LEN] =
        "String One      " \
        "String Two      " \
        "String Three    "
    ;

     

    Trouble is the most direct translation into C is to try and mimic the "pointer math" (yes some of you are cringing, like I care what you call it) and from what I know ANSI C doesn't normally let you do that. I certainly don't want to make selection of a string into a case statement and have to give each one a name, that's ridiculous! Barring that is there some way I can simply declare an "array of strings" (and I don't intend to assign each a pointer) so I can just introduce the string index and "trick" the compiler into doing the pointer math and being happy about it?

    ... and there is no need for "pointer math" or "trick"
    uint8_t len;
    len = FIXED_LEN;
    do {
        foo = fixed_length_strings[1*FIXED_LEN - len];    // access 1st string
    } while (--len);

    len = FIXED_LEN;
    do {
        foo = fixed_length_strings[2*FIXED_LEN - len];    // access 2nd string
    } while (--len);

    but you can use pointer (for the reader to implement) if you like. ;) As others have said, it is legal to add an offset to a pointer, and there are other methods in C too.
     
    #15
    mlp
    boots too small
    • Total Posts : 687
    • Reward points : 0
    • Joined: 2012/09/10 15:12:07
    • Location: previously Microchip XC8 team
    • Status: offline
    Re: Array of strings? 2019/02/19 11:11:46 (permalink)
    0
    jlawton11
    adding an integer type to a pointer type and expecting to still have a pointer type.

    So, the defined behaviour of standard C for 30 years now.
     

    Mark (this opinion available for hire)
    #16
    1and0
    Access is Denied
    • Total Posts : 8740
    • Reward points : 0
    • Joined: 2007/05/06 12:03:20
    • Location: Harry's Gray Matter
    • Status: offline
    Re: Array of strings? 2019/02/19 11:22:26 (permalink)
    +1 (1)
    mark.pappin
    jlawton11
    adding an integer type to a pointer type and expecting to still have a pointer type.

    So, the defined behaviour of standard C for 30 years now.

    I wonder what the OP thinks these are doing ;)
    char *ptr;
    ptr++;
    ptr += 16;

    #17
    dan1138
    Super Member
    • Total Posts : 3021
    • Reward points : 0
    • Joined: 2007/02/21 23:04:16
    • Location: 0
    • Status: offline
    Re: Array of strings? 2019/02/19 11:40:27 (permalink)
    +5 (5)
    jlawton11
    ... What I need to do is to try and translate the assembly code (that was working but the X IDE assembler wasn't reliable or consistent) into XC8 (what I'm saying is correct, no need to refight the Civil War here) ...

    Mr. Lawton,
    The tread you started, MPLAB X IDE editor/assembler symbols unstable, claims there are fundamental faults with the Microchip assembler. Reproducing the results in your claims has eluded me.

    This current thread seems a continuation of that behavior. From reading other posts from you it seems you show little experience in solving problems with micro-controller firmware or hardware.

    Your attitude when questioned on your methods or conclusions is openly hostile.

    Those that have replied to this thread have decades of experience with Microchip products and development tools. That you have chosen to ignore their observations is your loss.

    There is no doubt that the Microchip development tools have many bugs that make them inconvenient to use, especially for those that have limited experience.

    As a caution, hostile ranting on the forum is unlikely to yield the kind of results you seek. Once you achieve more depth of experience with Microchip tools you may find more success. To get help with gaining that experience you may need to find a greater tolerance to frustration.
    post edited by dan1138 - 2019/02/19 19:12:11
    #18
    crosland
    Super Member
    • Total Posts : 1521
    • Reward points : 0
    • Joined: 2005/05/10 10:55:05
    • Location: Bucks, UK
    • Status: offline
    Re: Array of strings? 2019/02/20 02:12:27 (permalink)
    +1 (1)
    Let's re-phrase the question
     
    jlawton11
    I have a small, old-style LCD ASCII character display. I generally feed it "strings" (all the same length) but without nulls between them.
     
    Is there some way I can simply declare an "array of strings" so I can just introduce the string index?



    See answers above.
     
    There now, that wasn't too difficult was it?
    #19
    Gort2015
    Klaatu Barada Nikto
    • Total Posts : 3038
    • Reward points : 0
    • Joined: 2015/04/30 10:49:57
    • Location: 0
    • Status: offline
    Re: Array of strings? 2019/02/20 05:44:50 (permalink)
    -1 (1)
    The OP has problems with C and ASM.

    MPLab X playing up, bug in your code? Nevermind, Star Trek:Discovery will be with us soon.
    https://www.youtube.com/watch?v=Iu1qa8N2ID0
    + ST:Continues, "What Ships are Made for", Q's back.
    #20
    Page: 12 > Showing page 1 of 2
    Jump to:
    © 2019 APG vNext Commercial Version 4.5