Array of strings

Discussion in 'Programmer's Corner' started by coldpenguin, Jun 6, 2010.

  1. coldpenguin

    Thread Starter Active Member

    Apr 18, 2010
    165
    9
    What am I doing wrong please?
    I am getting corrupted strings,
    Hitech C, again (still not sure that this could be a compiler issue).

    char **days[7];
    void main(void){

    days[0]="Monday";
    days[1]="Tuesday";
    days[2]="Wednesday";
    days[3]="Thursday";
    days[4]="Friday";
    days[5]="Saturday";
    days[6]="Sunday";

    ..... /there are no other references to 'days' anywhere in code. time[8] is being set to 0.

    lcd_goto(0x00);
    //this line
    sprintf(out,"%s",days[time[8]]);
    //
    lcd_puts(out);
    lcd_goto(0x40);
    sprintf(out,"%c%c:%c%c:%c%c",time[2],time[3],time[4],time[5],time[6],time[7]);

    }
    So, with the line commented out, the LCD shows:
    Line1:
    Line2: 21:23:11

    With the line uncommented, I get:
    Line1: uonday
    Line2: 21:23:11

    If I print out anything else (i.e., not a variable stored in an array), it comes out fine. (i.e., if I did, char * u="Monday";sprintf(out,"%s",u); it works)
     
  2. someonesdad

    Senior Member

    Jul 7, 2009
    1,585
    141
    Look carefully at your declaration of days; what is its type? (We used to make beginning C programmers read the type out loud to help them understand problems like this.) If you don't know how to answer this, then you'd better take a look at K&R and learn about arrays and types.

    Then ask yourself what the type is of e.g. days[2].

    When you're writing code and getting errors, one of the first things you should do is get a compiler that will help you with error messages -- then turn all of its warnings on (or find a lint tool). For example, use gcc and turn on e.g. pedantic mode. This would be easy to do for the code you showed if you stub out the various function calls.

    Another tip is when you're working with arrays of pointers is to print out the pointers with a printf("%p") so you can see the pointer's value (or use a debugger). Make sure the values are what you expect -- when they're not, you have to figure out why.
     
  3. nsaspook

    AAC Fanatic!

    Aug 27, 2009
    2,907
    2,168
    Just a guess... Not a Hitech C user.
    The problem could be in the type of pointers the compiler is generating. Try add in "const" to your day[0] code lines, that should place it in ROM.

    const days[0]="Monday";

    http://www.ccsinfo.com/forum/viewtopic.php?p=38498
     
  4. AsmCoder8088

    New Member

    Apr 17, 2010
    15
    1
    Change the following:

    Code ( (Unknown Language)):
    1.  
    2. char **days[7];
    3.  
    to this:

    Code ( (Unknown Language)):
    1.  
    2. char *days[7];
    3.  
    With the brackets, you've indicated you want an array of seven elements. In this case, seven pointers to character strings.

    You don't need an additional asterisk, because that would mean you are requesting an array of seven two-dimensional arrays. A character string is one-dimensional.
     
  5. John P

    AAC Fanatic!

    Oct 14, 2008
    1,634
    224
    Can he just do this:

    days[0]="Monday";

    and expect the string to be created properly? As declared, the array is an array of pointers, but a pointer isn't a string. It can point to a string, but that's different.

    Also, is the above line correct in any sense? I'd have used:

    strcpy(days[0], "Monday");

    But compilers for microcontrollers sometimes have their quirks, so I'm not sure what's legal and what isn't.
     
  6. coldpenguin

    Thread Starter Active Member

    Apr 18, 2010
    165
    9

    Bingo, gcc, intel, pgcc (f77 if pushed).

    That is what I tried first off. Will try again though.

    In order to use strcpy, I would have to already have declared the space where days[0] existed, in order to copy it in.

    Whereas (in my warped mind), the
    days[0] ="Monday";
    should have left a pointer to ROM where Monday exists.
    What is worrying me, is the fact that it is uonday that is returned. I would have expected, if this were a pointer/memory issue, that the first letter would be the safe one, and it would be the others that got corrupted.
     
  7. CVMichael

    Senior Member

    Aug 3, 2007
    416
    17
    Doesn't this work ?
    Code ( (Unknown Language)):
    1.  
    2. char days[][] = {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"};
    3.  
    4. void main(void){
    5.  
     
  8. coldpenguin

    Thread Starter Active Member

    Apr 18, 2010
    165
    9
    I couldn't get that notation to compile when I tried last night, but I was tired at the time, and this isn't something that I have done in hitech C before, so assumed that the error was that it wasn't implemented.
    I will try that again.
     
  9. CVMichael

    Senior Member

    Aug 3, 2007
    416
    17
    By the way, I found this on Google: Click here

    They say you should create the array like this:
    Code ( (Unknown Language)):
    1.  
    2. const char * const array[] = { "string 1", "string 2" };
    3.  
     
  10. coldpenguin

    Thread Starter Active Member

    Apr 18, 2010
    165
    9
    Gave the output:
    Line1: (null)
    Line2:

    Really not what I expected!

    Had high hopes for, but:
    Code ( (Unknown Language)):
    1. [SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]
    2. Error [283] Y:\microchip\WaterCan\Watercan.c; 255.13 dimension required
    3. [/COLOR][/SIZE][/COLOR][/SIZE]
    Even as char days[7][] it fails :(


    Gave: s3 [squigly alien thing][black blob]
    well, it didn't work lets put it that way!
    I think I should give up, and just do a switch statement instead. I am loosing faith in this compiler, the program isn't that complicated really (state machine which reads i2c DS1307, and a 1wire DS1820, once per second initiated by the SQWE output from the DS1307).
     
  11. BMorse

    Senior Member

    Sep 26, 2009
    2,675
    234
    I tried playing around with this a bit in Hi-Tech (I am currently using it to write an app for an F887) and this seems to work, but needs fine tuning....

    Code ( (Unknown Language)):
    1. char days[7][9] = {"Monday   ", "Tuesday   ", "Wednesday", "Thursday ", "Friday   ", "Saturday ", "Sunday   "};
    2.  
    [7] is the array dimension, and [9] is the length of data....

    My .02
    B. Morse
     
  12. t06afre

    AAC Fanatic!

    May 11, 2009
    5,939
    1,222
    You should be able to code as this
    Code ( (Unknown Language)):
    1.  
    2. const char * const MESSAGE[] = {"monday", "friday", "etc"};
    3.  
    This will put the constant in program memory
    Once you have defined the character output function putch() you can use puts to output a simple string with no formatting e.g. puts( MESSAGE[1] ); would output "friday".
    I can test not this at the current moment. Can you do it B. Morse?
     
    Last edited: Jun 8, 2010
  13. BMorse

    Senior Member

    Sep 26, 2009
    2,675
    234

    I tried it like that and it prints all the day names when you referrence the first variable in the array...

    It might be better to define the weekdays in a structure then reference each day from there.... I will see what else can be done to simplify it....



    B. Morse
     
  14. coldpenguin

    Thread Starter Active Member

    Apr 18, 2010
    165
    9
    I have reduced it down to:
    Code ( (Unknown Language)):
    1. const char * const days[] = {"Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"};
    2. void main(void){
    3. ....
    4.  lcd_goto(0x00);
    5.  time[8]=0;
    6.  sprintf(out,"%s %c%c/%c%c/%c%c",days[time[8]],time[9],time[10],time[11],time[12],time[13],time[14]);
    7.  lcd_puts(out);
    8.  
    and continued to move more code around in the main program. This appears to work... for the moment.
    I think that the problem is possibly memory management within the compiler. I have tried this on two different chips (I have 1 setup on breadboard, and I was moving to PCB strip-board at the moment for a more permanent solution), with the same problems.
    Moving functions from one .c file to another .c file, and still being linked in, appears to have the capability of changing what should be 'constants' in memory.
    I think you can see why I am using the sprintf here, rather than writing a putc/putr
    This is kinda working at the moment (the DS1307 is not returning what I think it should be. Earlier, it counted from 0 to 67 seconds! Here, the month and days are not being reported correctly either, which I think I must correct before continuing)
     
  15. BMorse

    Senior Member

    Sep 26, 2009
    2,675
    234
    He could try this;

    Code ( (Unknown Language)):
    1. const char (*array[]) = {
    2. "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"
    3. };
    compiles fine without errors, did not try the sprintf though.....


    B. Morse
     
  16. t06afre

    AAC Fanatic!

    May 11, 2009
    5,939
    1,222
    I did test and this seams to be correct. But I have only tested it MPLAB simulator. The string is copied correct to the variable buffer, "monday" in this case

    Code ( (Unknown Language)):
    1.  #include <htc.h>
    2. #include <string.h>
    3. #include <stdio.h>
    4. const char * const MESSAGE[] = {"monday", "friday", "etc"};
    5. void main (void)
    6. {
    7. char buffer[11];
    8. char s3=0;
    9. strcpy(buffer,MESSAGE[s3]);
    10. }
    11.  
     
  17. Elcric

    New Member

    Jun 23, 2010
    1
    0
    Hello everyone!

    Give this a try and see if it helps:

    Code ( (Unknown Language)):
    1.  
    2. #include<conio.h>     //  contains function prototypes
    3. #include<stdio.h>     //  standard I/O header file
    4. #include<string.h>    //  string handling
    5. #include<windows.h>   //  keybd_event()
    6.  
    7. int main( int argc, char* argv[] )
    8. {
    9.     char* GRACE[] = { "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"};
    10.     printf("\n\n  THE DAYS OF THE WEEK ARE:\n\n" );
    11.     char** p = GRACE;
    12.     for( unsigned n = 0; n < 7; ++n )
    13.     {
    14.         printf( "  %s", *p );
    15.         ++p;
    16.     }
    17.     printf("\n\n\n\n  Please press any key to continue." );
    18.     printf("\n\n  " );
    19.     char c;
    20.     c = getchar();
    21.     return 0;
    22. }
    23.  
    A string in C is an array of characters, with the last character being a '\0'. When double quotes are used, the nul character ('\0') escape sequence is automatically appended to the end of each string. For example “Monday” becomes Monday\0 because by definition a string in C must be nul terminated.
     
  18. GetDeviceInfo

    Senior Member

    Jun 7, 2009
    1,571
    230
    I've run into similar problems and it came down to buffer overruns. Try making your declaration earlier in your allocation and see if that helps.
     
Loading...