cStr cheatsheet – duplicate a c-str on stack (no strdup)

http://gcc.gnu.org/onlinedocs/gcc/Variable-Length.html sample code shows that C99 supports variable-length-array, or VLA.

Now it’s easy to duplicate a c-string on stack as a local variable.

void dup(char* orig) {
char dup[strlen(orig) + 1];
strcpy(dup, orig);
cout << “– duplicate a string on stack\n” << dup << endl;
}
int main() {
char a[] = “Four score and seven years ago”;
dup(a);

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s