What is the title of your "700 page book"?
Passing "a number of strings" is a very loose problem description. It could mean a fixed number of strings:
Code:
char *concatenate1(const char *a, const char *b, const char *c);
Or it could mean "a number of strings" passed as an array of strings, in which case you need some way to know how many strings are in the array. One way is to pass that information in:
Code:
char *concatenate2(const char **strs, size_t n);
Another way to determine how many strings there are is to use a NULL pointer as the last "string":
Code:
char *concatenate3(const char **strs); // last "string" is a NULL pointer
If you want to use a variable argument list, then you have the same two options to determine how many strings there are. Either pass the size as the first parameter:
Code:
char *concatenate4(size_t n, ...);
Or pass one of the strings as the first parameter (functions with variable argument lists need at least one named parameter), and pass a NULL as the last "string":
Code:
char *concatenate5(const char *s0, ...); // last argument must be NULL
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
char *concatenate1(const char *a, const char *b, const char *c)
{
char *result = malloc(strlen(a) + strlen(b) + strlen(c) + 1);
strcpy(result, a);
strcat(result, b);
strcat(result, c);
return result;
}
char *concatenate2(const char **strs, size_t n)
{
size_t size = 0;
for (size_t i = 0; i < n; ++i)
size += strlen(strs[i]);
char *result = malloc(size + 1);
*result = '\0';
for (size_t i = 0; i < n; ++i)
strcat(result, strs[i]);
return result;
}
char *concatenate3(const char **strs)
{
size_t size = 0;
for (size_t i = 0; strs[i]; ++i)
size += strlen(strs[i]);
char *result = malloc(size + 1);
*result = '\0';
for (size_t i = 0; strs[i]; ++i)
strcat(result, strs[i]);
return result;
}
char *concatenate4(size_t n, ...)
{
va_list va;
va_start(va, n);
size_t size = 0;
for (size_t i = 0; i < n; ++i)
size += strlen(va_arg(va, const char *));
char *result = malloc(size + 1);
*result = '\0';
va_end(va);
va_start(va, n);
for (size_t i = 0; i < n; ++i)
strcat(result, va_arg(va, const char *));
va_end(va);
return result;
}
char *concatenate5(const char *s0, ...)
{
if (!s0) return "";
va_list va;
va_start(va, s0);
size_t size = strlen(s0);
const char *s = va_arg(va, const char *);
while (s)
{
size += strlen(s);
s = va_arg(va, const char *);
}
char *result = malloc(size + 1);
strcpy(result, s0);
va_end(va);
va_start(va, s0);
s = va_arg(va, const char *);
while (s)
{
strcat(result, s);
s = va_arg(va, const char *);
}
va_end(va);
return result;
}
int main()
{
const char *strs[] = { "one", "two", "three", "four", "five", NULL };
char *s1 = concatenate1("one", "two", "three");
char *s2 = concatenate2(strs, 5);
char *s3 = concatenate3(strs);
char *s4 = concatenate4(5, "one", "two", "three", "four", "five");
char *s5 = concatenate5("one", "two", "three", "four", "five",
(const char*)NULL);
// Note that the NULL should be cast to the proper type of pointer
// since variable argument lists are untyped.
printf("%s\n", s1);
printf("%s\n", s2);
printf("%s\n", s3);
printf("%s\n", s4);
printf("%s\n", s5);
free(s1);
free(s2);
free(s3);
free(s4);
free(s5);
return 0;
}
BTW, I can't make any sense out of your proposal:
i was kind of thinking to assign each string (i.e. string_1 & string_2) to an integer so I could pass it to the function