string initilization WHY?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • puzzlecracker

    #1

    string initilization WHY?

    Why would this work? - and it does! any thoughts?



    #include<stdio. h>

    void init(char **str){ *str="Awesome"; }

    main(){


    char * str;

    printf("address : %d\n", str);
    init(&str);
    printf("address : %d\n", str);
    printf("string: \'%s\' has %d characters \n", str,strlen(str) );
    }

  • Emmanuel Delahaye

    #2
    Re: string initilization WHY?

    puzzlecracker wrote on 30/01/05 :
    Your code is seriously buggy...
    [color=blue]
    > #include<stdio. h>
    >
    > void init(char **str){ *str="Awesome"; }
    >
    > main(){[/color]

    [C99] main() requires an explicit return type
    [color=blue]
    >
    > char * str;
    >
    > printf("address : %d\n", str);[/color]

    The correct formatter for an address is "%p".

    Note that 'str' not being initialized, the behaviour is undefined.
    [color=blue]
    > init(&str);
    > printf("address : %d\n", str);
    > printf("string: \'%s\' has %d characters \n", str,strlen(str) );[/color]

    strlen() requires <string.h>

    [C90] main() requires a valid return...
    [color=blue]
    > }[/color]

    This works as expected...

    #include <stdlib.h>
    #include <stdio.h>
    #include <string.h>

    void init (char const **str)
    {
    *str = "Awesome";
    }

    int main (void)
    {


    char const *str = NULL;

    printf ("address: %p\n", (void const *) str);
    init (&str);
    printf ("address: %p\n", (void const *) str);
    printf ("string:\'% s\' has %d characters \n", str, strlen (str));

    /* Dev-C++ trick */
    system ("pause");
    return 0;
    }

    address: 00000000
    address: 00401280
    string:'Awesome ' has 7 characters
    Appuyez sur une touche pour continuer . . .

    --
    Emmanuel
    The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
    The C-library: http://www.dinkumware.com/refxc.html

    "Clearly your code does not meet the original spec."
    "You are sentenced to 30 lashes with a wet noodle."
    -- Jerry Coffin in a.l.c.c++

    Comment

    • Peter Shaggy Haywood

      #3
      Re: string initilization WHY?

      Groovy hepcat Emmanuel Delahaye was jivin' on Sun, 30 Jan 2005
      17:07:40 +0100 in comp.lang.c.
      Re: string initilization WHY?'s a cool scene! Dig it!
      [color=blue]
      >puzzlecracke r wrote on 30/01/05 :
      >Your code is seriously buggy...
      >[color=green]
      >> #include<stdio. h>
      >>
      >> void init(char **str){ *str="Awesome"; }
      >>
      >> main(){[/color]
      >
      >[C99] main() requires an explicit return type
      >[color=green]
      >>
      >> char * str;
      >>
      >> printf("address : %d\n", str);[/color]
      >
      >The correct formatter for an address is "%p".[/color]

      And, of course, the coresponding argument must be a pointer to void.
      So a cast is necessary here. Although it could be argued that, since
      char * has the same size and representation as void *, it is
      unnecessary in this case. However, it is better to be safe than sorry,
      so...

      printf("address : %p\n", (void*)str);
      [color=blue]
      >Note that 'str' not being initialized, the behaviour is undefined.
      >[color=green]
      >> init(&str);
      >> printf("address : %d\n", str);
      >> printf("string: \'%s\' has %d characters \n", str,strlen(str) );[/color]
      >
      >strlen() requires <string.h>
      >
      >[C90] main() requires a valid return...
      >[color=green]
      >> }[/color]
      >
      >This works as expected...[/color]

      Not quite. You're still causing undefined behaviour.
      [color=blue]
      >#include <stdlib.h>
      >#include <stdio.h>
      >#include <string.h>
      >
      >void init (char const **str)
      >{
      > *str = "Awesome";
      >}
      >
      >int main (void)
      >{
      > char const *str = NULL;
      >
      > printf ("address: %p\n", (void const *) str);[/color]

      Why void const *? The %p conversion specifier is for void *, not
      void const *.
      [color=blue]
      > init (&str);
      > printf ("address: %p\n", (void const *) str);
      > printf ("string:\'% s\' has %d characters \n", str, strlen (str));[/color]

      You're forgetting that strlen returns a size_t, not an int. A size_t
      is an implementation defined unsigned type. Do this:

      printf ("string:\'% s\' has %lu characters\n", str,
      (unsigned long)strlen(str ));

      or (in C99) this:

      printf ("string:\'% s\' has %llu characters\n", str,
      (unsigned long long)strlen(str ));
      [color=blue]
      > /* Dev-C++ trick */
      > system ("pause");[/color]

      Non-portable, of course.
      [color=blue]
      > return 0;
      >}[/color]

      --

      Dig the even newer still, yet more improved, sig!


      "Ain't I'm a dog?" - Ronny Self, Ain't I'm a Dog, written by G. Sherry & W. Walker.
      I know it's not "technicall y correct" English; but since when was rock & roll "technicall y correct"?

      Comment

      • Mike Wahler

        #4
        Re: string initilization WHY?

        "Peter "Shaggy" Haywood" <phaywood@alpha link.com.au.NO. SPAM> wrote in
        message news:41fd9701.5 117155@news.alp halink.com.au.. .[color=blue]
        > Groovy hepcat Emmanuel Delahaye was jivin' on Sun, 30 Jan 2005[color=green]
        > > init (&str);
        > > printf ("address: %p\n", (void const *) str);
        > > printf ("string:\'% s\' has %d characters \n", str, strlen (str));[/color]
        >
        > You're forgetting that strlen returns a size_t, not an int. A size_t
        > is an implementation defined unsigned type. Do this:
        >
        > printf ("string:\'% s\' has %lu characters\n", str,
        > (unsigned long)strlen(str ));
        >
        > or (in C99) this:
        >
        > printf ("string:\'% s\' has %llu characters\n", str,
        > (unsigned long long)strlen(str ));[/color]

        C99 has a specifier for 'size_t':

        printf("%zu\n", strlen(str));

        -Mike


        Comment

        • GrandElf

          #5
          Re: string initilization WHY?

          why can the string be accessed after calling "init" function?
          Maybe the content of the string is not stored in stack?But it looks
          like not to be in heap?

          Comment

          • Lawrence Kirby

            #6
            Re: string initilization WHY?

            On Mon, 31 Jan 2005 01:18:45 -0800, GrandElf wrote:
            [color=blue]
            > why can the string be accessed after calling "init" function?
            > Maybe the content of the string is not stored in stack?But it looks
            > like not to be in heap?[/color]

            The object defined by a string literal has static storage duration, like a
            variable declared static. So it exists for the lifetime of the program.

            Lawrence

            Comment

            • CBFalconer

              #7
              Re: string initilization WHY?

              GrandElf wrote:[color=blue]
              >
              > why can the string be accessed after calling "init" function?
              > Maybe the content of the string is not stored in stack?But it
              > looks like not to be in heap?[/color]

              What string? What "init"? What stack? What heap? In addition, of
              all those things, only string is defined in standard C, so they
              can't be discussed here without code to implement them.

              See sig. below.

              --
              "If you want to post a followup via groups.google.c om, don't use
              the broken "Reply" link at the bottom of the article. Click on
              "show options" at the top of the article, then click on the
              "Reply" at the bottom of the article headers." - Keith Thompson


              Comment

              Working...