about pointer initialization

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

    #1

    about pointer initialization

    ex1.char q[]="Nick";
    q[0]='D';

    ex2.char *q ="Nick";
    q[0]='D';

    ex1 is correct, but ex2 occurs run time error.

    why?

    thanks!
  • Mike Wahler

    #2
    Re: about pointer initialization


    "nick" <i141802596@yah oo.com> wrote in message
    news:dl6o61$1j4 k$1@justice.its c.cuhk.edu.hk.. .[color=blue]
    > ex1.char q[]="Nick";
    > q[0]='D';
    >
    > ex2.char *q ="Nick";
    > q[0]='D';
    >
    > ex1 is correct, but ex2 occurs run time error.
    >
    > why?[/color]

    Because attempting to modify a string literal
    produces undefined behavior, and 'run time error'
    is one of an infinite number of possible manifestations
    of undefined behavior. Moral: Don't Do That.

    -Mike


    Comment

    • Andre Kostur

      #3
      Re: about pointer initialization

      I'm surprised this isn't in the FAQ... so here goes:

      nick <i141802596@yah oo.com> wrote in news:dl6o61$1j4 k$1
      @justice.itsc.c uhk.edu.hk:
      [color=blue]
      > ex1.char q[]="Nick";[/color]

      q is an array of 5 bytes, into which is copied 'N' 'i' 'c' 'k' '\0'
      [color=blue]
      > q[0]='D';[/color]

      Reassigns the first byte.
      [color=blue]
      >
      > ex2.char *q ="Nick";[/color]

      q is a pointer to the string literal "Nick", stored somewhere in memory,
      potentially in a read-only memory page. A more "correct" way to write this
      line is:

      const char * q = "Nick";

      C++ accepts the non-const version in order to be more compatable with C.
      [color=blue]
      > q[0]='D';[/color]

      Attempts to change the first byte, which is const. As a result, Undefined
      Behaviour (in your case, a run-time error).

      Comment

      • Greg Comeau

        #4
        Re: about pointer initialization

        In article <dl6o61$1j4k$1@ justice.itsc.cu hk.edu.hk>,
        nick <i141802596@yah oo.com> wrote:[color=blue]
        >ex1.char q[]="Nick";
        >q[0]='D';
        >
        >ex2.char *q ="Nick";
        >q[0]='D';
        >
        >ex1 is correct, but ex2 occurs run time error.
        >
        >why?[/color]

        See http://www.comeaucomputing.com/techtalk/#stringliteral

        --
        Greg Comeau / Celebrating 20 years of Comeauity!
        Comeau C/C++ ONLINE ==> http://www.comeaucomputing.com/tryitout
        World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
        Comeau C/C++ with Dinkumware's Libraries... Have you tried it?

        Comment

        • Peter_Julian

          #5
          Re: about pointer initialization


          "nick" <i141802596@yah oo.com> wrote in message
          news:dl6o61$1j4 k$1@justice.its c.cuhk.edu.hk.. .
          | ex1.char q[]="Nick";
          | q[0]='D';
          |
          | ex2.char *q ="Nick";
          | q[0]='D';
          |
          | ex1 is correct, but ex2 occurs run time error.
          |
          | why?
          |
          | thanks!

          ex1.char is not a type.
          ex2.char is not a type.
          Who knows, maybe ex1 and ex2 are actually types?
          So which would q be, the pointer or the array?

          Lets suppose...

          char q[] = "Nick";
          char *p = &q[0]; // its a single char
          *p = 'S';
          std::cout << q; // prints "Sick" (note*)

          Note: lets not get confused here, a char array is not handled like
          arrays of other primitive types or arrays of user-defined types when it
          comes to output. You can thank compatibility for that headache. A
          std::string is a much better candidate here.

          std::string s("I'm a string");
          std::string *p_s = &s; // pointer to an entire string, not just a char
          <ah, relief>

          Are ex1 and ex2 structs or classes? That would be nice to know. If
          so...char is reserved.

          struct ex1 {
          char c;
          };

          ex1 q[] = {'a', 'b', 'c'}; // treat them as what they are
          ex1 *p = &q[0]; // p points to the first element

          ex1 *p2 = {'u'}; // error
          at which point p2 is undefined, null, zipfa, dangerous, bad karma, UB
          (pick one)

          An unitialized pointer is nothing, in my humble opinion: its not even a
          pointer. Call it a null pointer if you like. To me its a black hole. A
          few will argue, and disscuss, and scream, and beg but its still a black
          hole here. Adamantly so.

          The closer you get to a black hole, and the more often you get anywhere
          near it, it's eventually bound to suck you in.




          Comment

          Working...