a question about pointers and string

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • hedonplay
    New Member
    • Apr 2007
    • 5

    a question about pointers and string

    #include<iostre am>
    using namespace std;

    int main()
    {

    char *p;

    p="i love c++";
    cout<<p<<"\n";

    return 0;
    }

    Display:i love c++
    i am confusing about this kind of assignment. how is the string located? p is a pointer, the value of it should be an address.but why it displays as i love c++ .
    if(p) ... why can p judged as true?
  • oridimant
    New Member
    • Apr 2007
    • 2

    #2
    About your first question, when you write char* p = "i am a pointer", what happens is that the string "i am a pointer" is allocated on the stack and the p pointer actually points to the memory address of the stack string, p itself is not allocated.

    About your second question, if(p) means if the address p is pointing at is a valid one, meaning not NULL.

    Comment

    • Savage
      Recognized Expert Top Contributor
      • Feb 2007
      • 1759

      #3
      Take a look on this:

      char a; // allocates space for a single char

      char * b; // creates space for a pointer to a char ( or an array of char)

      char c [SZ]; // similar to b, must have a constant integer substitued for SZ
      // however, you also allocate space for the char, c is an address like b.

      char * d [SZ]; // this is interesting... like c above, its a pointer... but with a twist

      char ** e; // is the eqivalent of d above... and its called a pointer to a pointer.

      So if you just cout b it will be the same as cout c if they have same string

      Savage

      Comment

      • hedonplay
        New Member
        • Apr 2007
        • 5

        #4
        #include<iostre am>
        using namespace std;

        int main()
        {

        char *p;

        p="";
        if(p) cout<<"it is the address that p points is valid" <<"\n";
        else cout<<"the address is null"<<"\n";
        return 0;
        }
        Display:it is the address that p points is valid


        can i understand like this?

        Comment

        • JosAH
          Recognized Expert MVP
          • Mar 2007
          • 11453

          #5
          Originally posted by oridimant
          About your first question, when you write char* p = "i am a pointer", what happens is that the string "i am a pointer" is allocated on the stack and the p pointer actually points to the memory address of the stack string
          That is most certainly not true, i.e. the literal string is not allocated on the stack.
          The compiler has to be at its smartest to be able to deduce that string literals
          can be allocated on stacks, especially when the address of that string might
          be propagated to outer scopes.

          The only thing that is guaranteed is that the string literal is allocated in memory
          that is most likely not writable. Of course pointer p points to that string.

          Most compilers put literal strings in the 'rodata' (Read Only data) segment,
          some simply stick it in the data segments (where the string will be writable).
          It is not defined by any language standard though so it's safest to think of
          literal strings to be read only data.

          kind regards,

          Jos

          Comment

          • JosAH
            Recognized Expert MVP
            • Mar 2007
            • 11453

            #6
            Originally posted by hedonplay
            #include<iostre am>
            using namespace std;

            int main()
            {

            char *p;

            p="";
            if(p) cout<<"it is the address that p points is valid" <<"\n";
            else cout<<"the address is null"<<"\n";
            return 0;
            }
            Display:it is the address that p points is valid


            can i understand like this?
            I don't understand it; change the line p= "" to this:
            Code:
            p= (char*)rand();
            and see what is printed. Being non-null doesn't imply it's a valid address.

            kind regards,

            Jos

            Comment

            • JosAH
              Recognized Expert MVP
              • Mar 2007
              • 11453

              #7
              Allow me to clarify on my previous reply: when a pointer has a non-null value
              it is no guarantee that it's value would be valid. If a pointer does have a null
              value you can be sure that it isn't pointing to anything. So basically you should
              always initialize/assign your pointers to either null, indicating that it isn't
              pointing to anything, or to a valid region in memory. Non initialized/assigned
              pointers are a nono; always.

              kind regards,

              Jos

              Comment

              • Banfa
                Recognized Expert Expert
                • Feb 2006
                • 9067

                #8
                Originally posted by JosAH
                Non initialized/assigned pointers are a nono; always.
                Or alternatively the source of many many hours of debugging :D

                Comment

                Working...