Re: What's wrong with the string?

From: Bill Pringlemeir (spam_account_at_sympatico.ca)
Date: 06/21/04

  • Next message: RoSsIaCrIiLoIA: "Re: Repost: String pointer and char array"
    Date: 21 Jun 2004 16:08:51 -0400
    
    

    On Sun, 20 Jun 2004, blahhblaah@hotmail.com wrote:
    > Hi Frank,
    >
    >> void example() {
    >> char *str = "this is a test";
    >
    > Not an answer to your question, but I would suggest you change the
    > latter line to:
    > char str[] = "this is a test";

    No, these are pretty much equivalent. The original is correct here.
    It might be something to do with memory maps if the code is in some
    startup code. Then it is better to use "const char *" so that the
    string is places with code. There might be no DRAM mapped... but the
    OP didn't seem to indicate anything like this.

    >> 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.

    >> i think putc is a macro and not a function

    I think this is key. putc will evaluate "*str++". If it is evaluated
    multiple times, the str could point beyond the end of your string. In
    the array version, the increment within the parameters to putc are
    avoided. I would try this version,

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

    "while(*str != '\0')" is technically more correct as you are comparing
    characters to characters. However, C's promotion rules will end up
    converting everything to ints anyway, so it will effectively be the
    same. Ie, I doubt that is the problem.

    fwiw,
    Bill Pringlemeir.

    -- 
    I work hard because millions on welfare depend on me.
    

  • Next message: RoSsIaCrIiLoIA: "Re: Repost: String pointer and char array"

    Relevant Pages

    • Re: strange function behaviour
      ... > I have written a function that checks the first four characters in an ... > bool checkdigitsinaddress(const char* string) ... Record is the string of a file save on disk. ...
      (comp.lang.cpp)
    • Re: Extracting a substring
      ... >> the string I have to work with. ... >> numeric characters between the open and close parentheses. ... const char LD; ...
      (comp.lang.cpp)
    • Re: A doubly linked-list in C
      ... compiler gets there differently. ... characters). ... it is not because attempting to modify a string literal gives ... Use const char *p. ...
      (comp.lang.c)
    • Re: [PATCH/RFC] s390: Hypervisor File System
      ... Just return the *ptr and let the caller modify the string. ... I agree that it is a good idea to specify the characters to ... const char *reject) ...
      (Linux-Kernel)
    • Re: How to convert Infix notation to postfix notation
      ... If this is for an error message, why isn't it using stderr for its output? ... array of 15 characters, and you call this function with the limit 15 on ... Making sure that the only string I allocate and append to, ... because mulFactor in all versions must needs incorporate the functions ...
      (comp.lang.c)