-1

how can i aggregate text from different sources for example

char * a , * b,* c, *d;
a = argv[1] ;
b = "something";
c = "another text";
d=b&a&c; //this what i want to know
printf("%s",d);

output :

somethingTESTanother text

Amata
  • 1
  • 1

1 Answers1

1

First you need to get some memory for saving the resulting string. You can allocate just enough memory using malloc(); or (easier but does not scale) define d as an array with enough space (eg char d[8000];).

Then, after you have space for the result, you can strcpy() and strcat(); or sprintf() (if using C99 prefer snprintf()); or a number of other ways.

char *a, *b, *c, *d;
a = argv[1];
b = "something";
c = "another text";
int n = snprintf(NULL, 0, "%s%s%s", a, b, c);
d = malloc(n + 1);
if (!d) /* error */ exit(EXIT_FAILURE);
snprintf(d, n + 1, "%s%s%s", a, b, c); //this what i want to know
printf("%s\n",d);
free(d);
pmg
  • 106,608
  • 13
  • 126
  • 198