Input a char pointer (*string)

This is a discussion on Input a char pointer (*string) within the C++ Programming forums, part of the General Programming Boards category; I am trying to make the first part below function correctly: Code: char *test; cin >> test; // This does ...

  1. #1
    Polar Fuzz
    Join Date
    Oct 2003
    Posts
    36

    Input a char pointer (*string)

    I am trying to make the first part below function correctly:

    Code:
    char *test;
    cin >> test;  // This does not work
    
    char test2[25];
    cin >> test2;  // This seems to work
    How do I input a char pointer string?

  2. #2
    Unregistered User Yarin's Avatar
    Join Date
    Jul 2007
    Posts
    1,597
    As you admitted, it's a pointer. You must first point it to some memory.
    Try this:
    Code:
    char *test;
    test = new char[25];
    cin >> test;  // Now this does work
    A class that doesn't overload all operators just isn't finished yet. -- SmugCeePlusPlusWeenie
    A year spent in artificial intelligence is enough to make one believe in God. -- Alan J. Perlis

  3. #3
    Polar Fuzz
    Join Date
    Oct 2003
    Posts
    36
    Okay, I thought since I could assign:

    Code:
    char *test = "Hello";
    then I could input the same string. I thought there was a way to do this directly.

    Is this the only way to do it?
    Code:
    char *test = new test[25];
    delete test;

  4. #4
    Registered User
    Join Date
    Oct 2007
    Posts
    66
    To my knowledge, which may be wrong, that is how you use a cstring, with this slight modification:

    Code:
    char *test = new char[25];    // Use new char not new test
    
    ...
    
    delete test;

  5. #5
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    Quote Originally Posted by wbeasl View Post
    Okay, I thought since I could assign:

    Code:
    char *test = "Hello";
    then I could input the same string. I thought there was a way to do this directly.
    No.

    Everything in quotes gets placed in a special location in the executable that the compiler produces. This could be in some string table in the .data segment (or some equivalent).

    test in this case is simply a pointer, and it's given the address of where "Hello" was stored in this special location.

    When you use cin to read in a string and give it a pointer, the pointer in your example is pointing nowhere meaningful, and is most likely pointing to invalid memory. Such an attempt to store a string where it points to is undefined, which means the system is not guarenteed to do anything in particular. It is allowed to do whatever it wants. On many modern systems, you can probably expect such an attempt to result in a crash.

    Quote Originally Posted by wbeasl View Post
    Is this the only way to do it?
    Code:
    char *test = new test[25];
    delete test;
    For simple buffers, you may not need dynamic memory, and you can do well enough with a simple array. The above code will work just as fine, provided you only use test after you allocate a valid block of memory for it, and before you deallocate said block of valid memory.

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    Code:
    char* test = "Hello";
    "Hello" is a const char*. It is only for backwards compatibility that you are allowed to assign it to a char*. However, you still aren't allowed to modify it even if you assign it to a char*.
    Code:
    char *test = new char[25];
    
    delete [] test;
    You have to use delete [] if you used new [].

    Of course, in C++ you should be using the C++ string class instead of C style strings (null terminated character arrays). And if you need a dynamic array, you would use vector instead of new[]/delete[].

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Checking array for string
    By Ayreon in forum C Programming
    Replies: 87
    Last Post: 03-09-2009, 03:25 PM
  2. Code review
    By Elysia in forum C++ Programming
    Replies: 71
    Last Post: 05-13-2008, 09:42 PM
  3. code help required
    By Yobbo in forum C Programming
    Replies: 9
    Last Post: 09-02-2005, 11:15 PM
  4. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21