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.Scribbler:
I fail to see what difference it makes, whether the string is dynamicly or staticly bound.
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.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...Originally Posted by AH_TzeCode:strncpy(new_name, old_name.c_str(),4); if (new_name != "") { // do some stuff here }
Join is in our Unofficial Cprog IRC channel
Server: irc.phoenixradio.org
Channel: #Tech
Team Cprog Folding@Home: Team #43476
Download it Here
Detailed Stats Here
More Detailed Stats
52 Members so far, are YOU a member?
Current team score: 1223226 (ranked 374 of 45152)
The CBoard team is doing better than 99.16% of the other teams
Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)
Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT
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;
You're only born perfect.
yeah, we know that, but did you even read the code? he's checking against new_name, which is a char*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
Last edited by major_small; 02-09-2005 at 11:39 PM. Reason: details and CODE TAGS!!!
Join is in our Unofficial Cprog IRC channel
Server: irc.phoenixradio.org
Channel: #Tech
Team Cprog Folding@Home: Team #43476
Download it Here
Detailed Stats Here
More Detailed Stats
52 Members so far, are YOU a member?
Current team score: 1223226 (ranked 374 of 45152)
The CBoard team is doing better than 99.16% of the other teams
Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)
Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT
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; }
Last edited by AH_Tze; 02-10-2005 at 12:28 AM. Reason: forgot a few lines
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.
Join is in our Unofficial Cprog IRC channel
Server: irc.phoenixradio.org
Channel: #Tech
Team Cprog Folding@Home: Team #43476
Download it Here
Detailed Stats Here
More Detailed Stats
52 Members so far, are YOU a member?
Current team score: 1223226 (ranked 374 of 45152)
The CBoard team is doing better than 99.16% of the other teams
Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)
Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT
bah, respecting original intent is so old school. Subversion is the new paradigm.Originally Posted by major_small
/me notices frustrating lack of evil smileys