Strchr

From Nutwiki
Revision as of 07:09, 12 March 2008 by Anonymous (Talk) (New page: == Description == One day i find strchr in NutApi: char *strchr( const char * p, int ch) { for (;; ++p) { if (*p == ch) return ((char *) p); if (!*p...)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Description

One day i find  strchr in NutApi:
char *strchr( const char * p, int ch)
{
    for (;; ++p) {
        if (*p == ch)
            return ((char *) p);
        if (!*p)
            return ((char *) NULL);
    }
    /* NOTREACHED */
}

Test Environment

This sample should run on all platforms. I actually used: 
Nut/OS: Version 4.5.0 
Hardware: ATmega128. 
Compiler: icc 7
Debug tool: AVR Studio

Source Code 1

  1. include <stdio.h>
char * test="ethernut";
char *strchr_test( char * const p, int ch)
{
    for (;; ++p) {
        if (*p == ch)
            return ((char *) p);
        if (!*p)
            return ((char *) NULL);
    }
    /* NOTREACHED */
}

int main(void)

{
    char * temp;
    temp=strchr_test(test,'e');
    printf("%s\r\n", temp);
}
the result is : temp=NULL;

File:UnExpected.JPG

Source Code 2

  1. include <string.h>
  2. include <stdio.h>
char * test="ethernut";
char *strchr_test( const char  *  p, int ch)
{
    for (;; ++p) {
        if (*p == ch)
            return ((char *) p);
        if (!*p)
            return ((char *) NULL);
    }
    /* NOTREACHED */
}
int main(void)
{
char * temp;
temp=strchr(test,'e');
printf("%s\r\n", temp);
}
And this time the result is as we expected: 

File:Expected.JPG

seek help

 Anyone who knows reasons, please keep in touch with me.
 I will be very grateful to him.
 email: liyongming1982@163.com
 from: Guangzhou,China