It is not safe if the string is dynamic, because new_name may be NULL. If that is the case, new_name[0] would result in a seg fault.Quote:
Scribbler:
I fail to see what difference it makes, whether the string is dynamicly or staticly bound.
Printable View
It is not safe if the string is dynamic, because new_name may be NULL. If that is the case, new_name[0] would result in a seg fault.Quote:
Scribbler:
I fail to see what difference it makes, whether the string is dynamicly or staticly bound.
I was able to use strlen. Thanks to all for your help!
Code:int len;
len = strlen(new_name);
if (len >0)
{
// do some stuff
}
read the very first post...Quote:
Originally Posted by AH_Tze
Code:strncpy(new_name, old_name.c_str(),4);
if (new_name != "")
{
// do some stuff here
}
how's that?
If c_str() is a member function of old_name, that would imply it's an STL string. Thus you could call empty() on it.
c_str() returns the C style string buried in the implementation of the C++ string object.
Code:string s = "dodah";
char cString[10];
if(s.length() < 9)
strcpy(cString, s);
cout << cString << end;
yeah, we know that, but did you even read the code? he's checking against new_name, which is a char*Quote:
Originally Posted by AH_Tze
edit: to be more specific, I ran this code:and got this error:Code:#include<iostream>
#include<cctype>
using namespace std;
int main()
{
string old_name;
string new_name;
char*nname=new char[4]; //used for testing
strncpy(new_name, old_name.c_str(),4); //this throws an error
if(nname[0]=='\0') //I know this is wrong, but this was for testing
{
cout<<"empty";
}
cin.get();
return 0;
}
however, when I run the code I suggested:Code:no matching function for call to `strncpy(std::string&, const char*, int)'
I get no errors. in fact, I get exactly what I'm expecting - a console window with the single line:Code:#include<iostream>
#include<cctype>
using namespace std;
int main()
{
string old_name;
string new_name; //used for testing
char*nname=new char[4];
strncpy(nname, old_name.c_str(),4); //this works now
if(nname[0]=='\0') //now this is right, and all works perfectly
{
cout<<"empty";
}
cin.get();
return 0;
}
Code:empty
Code:#include<iostream>
#include<cstring>
using namespace std;
int main()
{
string old_name;
// presumably assign old_name somewhere in here.
if(!old_name.empty()) // easy to read conditional.
{
char* new_name = new char[4];
strncpy(new_name, old_name.c_str(), 4);
// do what you got to do with a non-empty c-string
// All the messy c-string work is isolated here.
cout << new_name << endl;
}
else
{
cout << "empty" << endl;
}
cin.get();
return 0;
}
ah, I see what you meant... I generally try not to change the logic of the OP, because, for example, he may have intended to copy the string into the array wether or not it was empty. for example, if this code is being used to change/remove a password, your version would require that something was put in, where the OP code may have thrown a warning and continued on it's merry way with it's empty char*.
in this case, however, it probably doesn't matter.
bah, respecting original intent is so old school. Subversion is the new paradigm.Quote:
Originally Posted by major_small
/me notices frustrating lack of evil smileys