1. Not finding help here? Sign up for a free 30min tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

C++ storing a string in a pointer.

  1. Aug 18, 2007 #1
    1. The problem statement, all variables and given/known data

    If I write:

    char* Name="Computer";
    cout<<Name[0]; //Output: C
    cout<<Name[2]; // Output: M

    what do the above lines mean? How can a pointer store a string? And why is it acting like an array? Either it is a simple concept that I have forgotten or it is a new concept that I have yet to study.


    3. The attempt at a solution

    Here, Name is a pointer of char type i.e. it can store the address of a char type variable. We cannot assign a character to a pointer. Then how is it still correct to assign a string to a pointer. What is happening here?
    Please Help!!!

    Thanks
    Mr V
     
  2. jcsd
  3. Aug 18, 2007 #2

    chroot

    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    You're using a shorthand notation.

    When the compiler sees quote marks, it actually reserves space in your executable's data segment for the stuff inside the quotes, and then initializes the pointer Name to point to the first character of it. There's no deeper magic going on.

    - Warren
     
  4. Aug 18, 2007 #3

    mgb_phys

    User Avatar
    Science Advisor
    Homework Helper

    As chroot said, behind the scenes the compiler is doing.
    char temp[] = "Computer"
    char *Name = temp[0];

    Does this make it clearer?
     
  5. Aug 18, 2007 #4
    Ok, so the compiler reserves a temporary sapce for the string and makes the pointer point at the address of the first character. But what does it mean if I write

    cout<<Name[1];

    and it gives 'O' as output, if the string entered was "COMPUTER"?

    Is this equivalent to: cout<<Temp[1];
    So C++ actually says that if you assign a character to a pointer, then it is an error, but if you assign a string, you are not actually assigning it to the pointer but creating a space to store the string.

    There is one more problem:
    If Name is storing the address of the first character of the string, how come I get "COMPUTER" as output when I write:

    cout<<Name;

    It should show me the address of where 'C' is stored, but it gives me a string. Are all the pointer rules bent for this type of thing. Can you direct me to a site which provides all the rules for such a case and explains them.

    Thanks for your answers.

    Mr V
     
  6. Aug 18, 2007 #5
    When I write:

    cout<<*Name; //Output: C

    Which means it "is" storing the address of the first character entered. But why does it display the string in the case I have shown in my previous post above?

    Mr V
     
  7. Aug 18, 2007 #6

    mgb_phys

    User Avatar
    Science Advisor
    Homework Helper

    Because the 'C' rules for outputing a string are, start at the first character and continue until you get a '\0'.
    Strings are just areas of memory with a 0 byte at the end.

    If you want to output just the first letter you would do something like,
    char c = Name[0];
    cout << c;

    or possibly
    cout << (char)Name[0]
     
  8. Aug 18, 2007 #7
    Thanks. I have now understood the answer to my question.

    Thanks again for your answers.

    Mr V
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?



Similar Discussions: C++ storing a string in a pointer.
Loading...