Page 1 of 1

Weird Behavior

Posted: Fri Nov 14, 2008 12:32 am
by legend986
I was recently trying to output a string onto an LCD screen:

Code: Select all

 
while(*p != '\0') {
    putchar(p);
}
 
But when I do that, it doesn't do anything but when I do this:

Code: Select all

 
while(*p) {
    putchar(p);
}
 
it prints the whole string with a space and then displays the cursor. I was not quite understanding what was wrong so I wrote this:

Code: Select all

 
while(*p && *p!='\r' && *p != '\0') {
    putchar(p);
}
 
And it works... I am not sure why I am seeing this behavior. Could someone please help me out? A string is supposed to be null terminated but why am I required to test for a \r character?

Re: Weird Behavior

Posted: Fri Nov 14, 2008 2:35 pm
by andyhoneycutt
Could we see a little of the preceding code and maybe a bit after your code block?

Re: Weird Behavior

Posted: Fri Nov 14, 2008 4:44 pm
by legend986
Sure... Actually the code I provided is a simplified version of this:

Code: Select all

 
void lcdputstr(char *p)
 {
    int i=0;
    char *temp;
    temp=p;
 
        while(*temp) {
               if(cursor == 0x0F) {
            lcdputch(temp++);
            cursor=0x40;
            lcdgotoaddr(cursor);
        } else if(cursor == 0x1F) {
            lcdputch(temp++);
            cursor =0x50;
            lcdgotoaddr(cursor);
        } else if(cursor == 0x4F) {
                lcdputch(temp++);
                cursor=0x10;
                lcdgotoaddr(cursor);
             } else     
            lcdputch(temp++); 
                   cursor++;    
    }   
}   
I am not sure if I can convey everything from that code but I just hope it does...

Re: Weird Behavior

Posted: Wed Nov 19, 2008 10:13 am
by andyhoneycutt
Your first approach seems to be the most correct to me. You're testing for null termination, which is the standard way of tearing through a string as far as i can tell. I would investigate the function you are using to tool the pointer *p. Your lcdputstr() function seems to be correct. I'm not sure where to troubleshoot from here. If you can attach the complete code you are writing, I'll throw it into gcc and see if I can help you come up with a solution...

-Andy