user738804
user738804

Reputation: 351

better way to concatenate multiple strings in c?

Is there a better way to concatenate multiple strings together in c other than having multiple calls to strcat() all in a row, like below?

char prefix[100] = "";
strcat(prefix, argv[0]);
strcat(prefix, ": ");
strcat(prefix, cmd_argv[0]);
strcat(prefix, ": ");
strcat(prefix, cmd_argv[1]);
perror(prefix);

Upvotes: 35

Views: 85741

Answers (8)

Lily AB
Lily AB

Reputation: 400

Couldn't you use a Macro.

#include <stdio.h>
#include <string.h>

#define a argv[0]
#define b argv[1]
#define c argv[2]
#define strcat1(a,b,c) strcat(prefix, a);\
                       strcat(prefix, ": ");\
                       strcat(prefix, b);\
                       strcat(prefix, ": ");\
                       strcat(prefix, c);\
                       perror(prefix);\



int main(int argc, char *argv[]){
    char prefix[100] = "";
    strcat1(a,b,c);
    return 0;
}

Upvotes: 1

technosaurus
technosaurus

Reputation: 7812

Assuming you have a char[fixed_size] as posted, rather than a char*, you can use a single, creative macro to do it all at once with a cout-like ordering rather than the disjointed printf style format. If you are working with embedded systems, this method will allow you to leave out the large printf family of functions like snprintf() (This keeps dietlibc from complaining too) and doesn't even require malloc() or any functions from <string.h>.

#include <unistd.h> //for write
//note: you should check if offset==sizeof(buf) after use
#define strcpyALL(buf, offset, ...) do{ \
    char *bp=(char*)(buf+offset); /*so we can add to the end of a string*/ \
    const char *s, \
    *a[] = { __VA_ARGS__,NULL}, \
    **ss=a; \
    while((s=*ss++)) \
         while((*s)&&(++offset<(int)sizeof(buf))) \
            *bp++=*s++; \
    if (offset!=sizeof(buf))*bp=0; \
}while(0)

char buf[100];
int len=0;
strcpyALL(buf,len, argv[0],": ", cmd_argv[0],": ",cmd_argv[1]);
if (len==sizeof(buf))write(2,"error\n",6);
else write(1,buf,len); 
  • Note 1, you can use any function that outputs a char*, including nonstandard functions like itoa() for converting integers to string types.
  • Note 2, if you are using a shared library or are already using a printf style function anywhere in your statically built program, the only reason not to use snprintf() (since the compiled code would be larger with this method) would be that since it is inlined and doesn't call any external functions, so it should be relatively fast.

Upvotes: 3

Fred Larson
Fred Larson

Reputation: 62093

I might take a rep hit for this, but what the heck. The worst thing that can happen is I'll learn something.

I don't really use C these days, and I don't typically use C-style strings in C++. But one idea I have is to write a modified strcpy() that returns the end of the string:

char* my_strcpy(char*dest, const char* src)
{
    while ((*dest = *src++))
        ++dest;
    return dest;
}

Now Shlemiel can bring his bucket along with him:

char prefix[100] = "";
char* bucket = my_strcpy(prefix, argv[0]);
bucket = my_strcpy(bucket, ": ");
bucket = my_strcpy(bucket, cmd_argv[0]);
bucket = my_strcpy(bucket, ": ");
bucket = my_strcpy(bucket, cmd_argv[1]);
perror(prefix);

I haven't tested this. Comments?

EDIT: Removed the unnecessary my_strcat() function. Also it turns out to be the same as stpcpy(), which is apparently part of POSIX as of 2008. See http://www.manpagez.com/man/3/stpcpy/.

Upvotes: 6

Alok Singhal
Alok Singhal

Reputation: 96181

I would use sprintf() like others have suggested, but this is for completeness:

If you have stpcpy(), then you can do:

char prefix[100] = "";
stpcpy(stpcpy(stpcpy(sptcpy(stpcpy(prefix, argv[0]), ": "),
        cmd_argv[0]), ": "), cmd_argv[1]);
perror(prefix);

The convenience with stpcpy() is that it can be "chained", as above. Also, since stpcpy() returns a pointer to the end of the resultant string, subsequent stpcpy() calls don't need to go through the old data again and again. So, it is more efficient than multiple strcat()s and probably more efficient than sprintf(). stpcpy() is POSIX:2008.

Upvotes: 7

Lucky Murari
Lucky Murari

Reputation: 12782

you can use snprintf function

char prefix[100];
snprintf(prefix, 100, "%s: %s: %s", argv[0], cmd_argv[0], cmd_argv[1]);

Upvotes: 2

forsvarir
forsvarir

Reputation: 10839

If you're trying to build a string from other strings (which your example suggests), then you can use snprintf.

char prefix[100] = "";
snprintf( prefix, sizeof(prefix), "%s: %s: %s", argv[0], cmd_argv[0], cmd_argv[1]);

If you're trying to do concatenation of an existing string, where you can't use the format approach, then you're probably stuck with multiple calls to strcat, although I'd strongly suggest that you might want to consider using strncat instead and checking to ensure you don't have buffer overruns.

Upvotes: 4

Jacob
Jacob

Reputation: 34621

sprintf(prefix,"%s: %s: %s",argv[0],cmd_argv[0],cmd_argv[1]);

Or snprintf to prevent buffer overruns.

Upvotes: 34

Wes Hardaker
Wes Hardaker

Reputation: 22262

snprintf would be the best and easiest to use option, though it may not be "fast". You didn't state what your criteria was. Simplicity is definitely this, though:

snprintf(prefix, sizeof(prefix), "%s: %s: %s", argv[0], cmd_argv[0], cmd_argv[1]);

Upvotes: 19

Related Questions