Thread: char* question

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    153

    char* question

    Hey everyone,
    I typed out some simple code and it confused me:
    Code:
    #include <iostream>
    
    using namespace std;
    
    main()
    {
    	char* name = new char[80];
    	cout << "Enter a name: ";
    	cin  >> name;
    	cout << "name is " << name << endl;
    }
    How am I able to cin into name? Am I wrong in thinking that name points to an array? This was just a quick question in my head... what exactly is taking place in the line:
    Code:
    char* name = new char[80];
    Thanks!

    Just to add in a bit extra....if name is a pointer, shouldn't I have to use *name to cin to? And also *name to cout?

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >How am I able to cin into name?
    The effect is the same as if you were using an array of char.

    >Am I wrong in thinking that name points to an array?
    Not entirely. To be strictly correct, you are "simulating" an array with dynamic memory, so even though under the hood things are completely different, the behavior is the same in most cases.

    >what exactly is taking place in the line
    A block of memory, 80 characters long, is allocated and the pointer name refers to the beginning of that block.
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Character arrays are treated specially in C, (and so in C++ as well). There are many functions that treat an array of characters as a string with the null character ('\0') indicating the end of the string. These null terminated character arrays are often called C style strings.

    Two of those functions are the operator>> and operator<< that work with cin and cout. Instead of printing out the pointer value, which would normally happen with a pointer like name, they instead print out all the consecutive characters they find starting with the one pointed to by the variable and ending with the first null character.

    If you use *name, then you are dereferencing the pointer and you will get only the single character at the start of the string. To get the special benefits of C style strings, you must use the pointer itself (otherwise the code will think you just want to input or output a single character).

    It's good to know and understand this stuff, but in C++ the standard string class should still be preferred over C style strings.

  4. #4
    Registered User
    Join Date
    Sep 2004
    Posts
    153
    ah ha! Yes...now I remember! I went over c style strings a decent time ago and just havent practiced them since then...clearly it's showing in my inabilty to understand this concept. I have down now...thanks a lot prelude and daved, great help as usual.

  5. #5
    Registered User
    Join Date
    Sep 2004
    Posts
    153
    Actually I just have one last question...
    Code:
    char *str = "buzz";
    How exactly does this work? How am I able to assign a string literal to something that should be expecting a memory address?

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    The string literal is stored in memory (it is constant, and cannot be changed by you). The pointer points to the first character of the string literal, so that when you use the variable str in your program, the functions that act on it look at each character until a null is found just like a regular C style string.

    The proper form of that statement is actually:
    Code:
    const char *str = "buzz";
    Without the const is allowed for backwards compatibility, but it is deprecated in C++. Putting the const in there reminds you through compiler warnings or errors if you try to modify the string literal through that pointer. It isn't allowed and would cause problems with your program if you did it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Alice....
    By Lurker in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 06-20-2005, 02:51 PM
  2. Debugging question
    By o_0 in forum C Programming
    Replies: 9
    Last Post: 10-10-2004, 05:51 PM
  3. Question about pointers #2
    By maxhavoc in forum C++ Programming
    Replies: 28
    Last Post: 06-21-2004, 12:52 PM
  4. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  5. Question, question!
    By oskilian in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 12-24-2001, 01:47 AM