Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

C++

Name: Anon 2009-01-28 2:07

Hey, I am a complete noob to c++, and I need to know how to add strings.

Eg, I have:
a="one"
b="two"
c="three"

and I would like "d" to equal "onetwothree".

Thank you for any help

Name: Anonymous 2009-01-28 14:33

>>5
 * sizeof(char) is defined by the standard to be 1.
 * strcpy is not the proper function to use in this situation.
 * Using strcat requires calloc instead of malloc to be used to ensure NULL-termination.

The corrected code has been attached to this message.
____________________________
Attachments:

#include <stdlib.h>

char *a="one";
char *b="two";
char *c="three";
char *d=calloc(strlen(a)+strlen(b)+strlen(c)+1, 1);

strcat(d, a);
strcat(d, b);
strcat(d, c);

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List