pointer to string???

This is a discussion on pointer to string??? within the C++ Programming forums, part of the General Programming Boards category; this is normal pointer that gives variable that var contains Code: #include <iostream> #include <string> using namespace std; int main ...

  1. #1
    beginner for now
    Join Date
    Oct 2009
    Posts
    35

    Question pointer to string???

    this is normal pointer that gives variable that var contains

    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    
    int main ()
    {
        int var = 0;
        cout << var << endl;
        int * pointer1 = &var;
        cout << *pointer1 << endl;
        
        int g;
        cin >> g;
        return 0;
    }


    it's all the same I have just put string instead of int

    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    
    int main ()
    {
        string var = 0;
        cout << var << endl;
        string * pointer1 = &var;
        cout << *pointer1 << endl;
        
        int g;
        cin >> g;
        return 0;
    }

    1. I don't get compiler error
    the program starts and ends very fast why???

    what does program exactly do now??

    or how to point to string???

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    You can't assign a number to a string variable. Try something like this:
    Code:
    string var = "a string";

  3. #3
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    839
    use std::stringstream or itoa / ftoa to convert numeric types to ascii.

  4. #4
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Code:
    string var = 0;
    This uses the string constructor that accepts const char*, similarly to:

    Code:
    string var(static_cast<char*>(0));
    However, the result of passing a NULL pointer to this constructor is undefined: some implementations may throw an exception, others may just crash from dereferencing it.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  5. #5
    beginner for now
    Join Date
    Oct 2009
    Posts
    35

    Thumbs up

    thanks

    null was the problem
    Last edited by military genius; 10-14-2009 at 11:41 AM. Reason: found solution to 0 or false to not give null to pointer

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    You can use an empty string.

    If an empty string won't work with what you're doing, then you'll have to find some other solution. One option is a separate bool that holds the true or false value you want to keep. If the information is complicated enough you would combine these into a struct or class and provide methods to handle the behavior.

  7. #7
    beginner for now
    Join Date
    Oct 2009
    Posts
    35
    sorry I have edit the message

    yes I found the solution to that message you saw

    I used bool and works fine for 0 or flase

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Pointer to String and Pointer to Char
    By vb.bajpai in forum C Programming
    Replies: 3
    Last Post: 06-15-2007, 04:03 PM
  2. Pointer to array of string and Array of Pointer to String
    By vb.bajpai in forum C Programming
    Replies: 2
    Last Post: 06-15-2007, 07:04 AM
  3. String issues
    By The_professor in forum C++ Programming
    Replies: 7
    Last Post: 06-12-2007, 10:11 AM
  4. Custom String class gives problem with another prog.
    By I BLcK I in forum C++ Programming
    Replies: 1
    Last Post: 12-18-2006, 03:40 AM
  5. Program using classes - keeps crashing
    By webren in forum C++ Programming
    Replies: 4
    Last Post: 09-16-2005, 04:58 PM

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