What's wrong with the string?

From: Frank (young726_at_yahoo.com)
Date: 06/11/04

  • Next message: Frank: "Repost: String pointer and char array"
    Date: 10 Jun 2004 16:10:11 -0700
    
    

    Hi,

    I am developing an application on PPC405 (Walnut). But somehow in the
    string in the C function doesn't work. The code is as below:

    void example() {
      char *str = "this is a test";
      while (*str != 0) {
        putc(*str++);
      }
      return;
    }

    putc() is a function to put a char on the serial console.

    This function ends up with a strange string on the serial console
    "><...HH..>>..."

    But if I modify the function to the following one:

    void example_modified() {
      char str[10] = \
        {'t','h','i','s',' ','i','s',' ','a',' ','t','e','s','t','\0'};
      int i = 0;
      for (i = 0; str[i] != 0; i++) {
        putc(str[i]);
      }
      return;
    }

    Then it works. So I am wondering if there is something wrong with the
    stack setting. Can anybody tell me what makes the above two functions
    different? Why using char array str[10] works while using pointer str*
    doesn't work?

    Thanks!

    Frank


  • Next message: Frank: "Repost: String pointer and char array"

    Relevant Pages

    • Re: String pointer and char array
      ... But somehow the string pointer in a C function doesn't ... > putcis a function to put a char on the serial console. ... > void example_modified{ ... stack setting, or, why you got different results. ...
      (comp.lang.c)
    • Repost: String pointer and char array
      ... This function ends up with a strange string on the serial console ... void example_modified{ ... Why assigning string str individually works while assigning ...
      (comp.lang.c)
    • Repost: String pointer and char array
      ... This function ends up with a strange string on the serial console ... void example_modified{ ... Why assigning string str individually works while assigning ...
      (comp.os.linux.embedded)
    • Re: Whats wrong with the string?
      ... > string in the C function doesn't work. ... > putc() is a function to put a char on the serial console. ... > void example_modified{ ...
      (comp.os.linux.embedded)
    • Re: Random String
      ... void RH_Random(unsigned char *str, size_t len) ... As long as it is a char array yes. ... Are you ever going to initialize something else to a string? ...
      (comp.lang.c)