-
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???
-
You can't assign a number to a string variable. Try something like this:
Code:
string var = "a string";
-
use std::stringstream or itoa / ftoa to convert numeric types to ascii.
-
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.
-
thanks
null was the problem
-
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.
-
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