Add formatted string to string in C (not C ++) without pointer arithmetic?

Wrote a very simple C function to illustrate what I would like to simplify:

void main(int argc, char *argv[]){
    char *me="Foo";
    char *you="Bar";
    char us[100];
    memset(us,100,0x00);

    sprintf(us,"You: %s\n",you);
    sprintf(us+strlen(us),"Me: %s\n",me);
    sprintf(us+strlen(us),"We are %s and %s!\n",me,you);
    printf(us);
}

It seems like there should be a standard library function to handle what I'm doing with sprintf and moving the pointer, no? It has been many years since I made any number of C ...

Thanks, -L

+3
source share
5 answers

sprintf returns the number of characters other than NUL.

int len = 0;
len += sprintf(us+len, ...);
len += sprintf(us+len, ...);
...
+8
source

Hmm ... could you just use something like:

sprintf(us, "You: %s\nMe: %s\nWe are %s and %s!\n, you, me, me, you);
+2
source
+1
char *me="Foo";
char *you="Bar";
char us[100];

char* out = us;
out += sprintf(out,"You: %s\n",you);
out += sprintf(out,"Me: %s\n",me);
out += sprintf(out,"We are %s and %s!\n",me,you);
printf("%s", us);
+1

printf ( fprintf). . me "Muahaha %n%d%n%d%n%d%n%d%n" SEGFAULT. printf , , , printf , , t - .

" " , fputs(str,stdout). printf .

+1

Source: https://habr.com/ru/post/1727690/


All Articles