Weird Behavior

Coding Critique is the place to post source code for peer review by other members of DevNetwork. Any kind of code can be posted. Code posted does not have to be limited to PHP. All members are invited to contribute constructive criticism with the goal of improving the code. Posted code should include some background information about it and what areas you specifically would like help with.

Popular code excerpts may be moved to "Code Snippets" by the moderators.

Moderator: General Moderators

Post Reply
User avatar
legend986
Forum Contributor
Posts: 258
Joined: Sun Jul 15, 2007 2:45 pm

Weird Behavior

Post 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?
User avatar
andyhoneycutt
Forum Contributor
Posts: 468
Joined: Wed Aug 27, 2008 10:02 am
Location: Idaho Falls

Re: Weird Behavior

Post by andyhoneycutt »

Could we see a little of the preceding code and maybe a bit after your code block?
User avatar
legend986
Forum Contributor
Posts: 258
Joined: Sun Jul 15, 2007 2:45 pm

Re: Weird Behavior

Post 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...
User avatar
andyhoneycutt
Forum Contributor
Posts: 468
Joined: Wed Aug 27, 2008 10:02 am
Location: Idaho Falls

Re: Weird Behavior

Post 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
Post Reply