using
char *tokens[size1+size2]
and
tokens = (char*) malloc(size1+size2+1);
is not correct. . If you use the first one so you have already defined array of pointer with static allocation of (size1+size2) of string pointers. So you can not reallocate dynamically with malloc.
If you want to allocate dynamically an array of string pointer with malloc than you have to define tokens like that:
char **tokens
double pointer. which means pointer to an array containing pointers to string
and for the allocation you have do it like this:
tokens = (char**) malloc((size1+size2+1)*sizeof(char *));
for:
strcpy(tokens, tokens1);
you want to copy array of pointers to another pointer array. but you have used function to copy array of char to array of char. and the char type and the pointer type are not the same. the size of char is 1 byte and the size of pointer is 4bytes/8bytes (it depends on the system you use)
the same for strcat
The memcpy could not help you because you want to copy the tokens1 array till you find the NULL address and not copy the whole array
If you want to copy only pointers (address) of strings: here after how you can do it
//to copy tokens1 (terminated with NULL address)
for (i=0;tokens1[i]!=NULL;i++)
{
tokens[i]=tokens1[i];
}
//to concat tokens2 (terminated with NULL address)
for (j=0;tokens2[j]!=NULL;j++)
{
tokens[i+j]=tokens2[j];
}
tokens[i+j]=NULL;
If you want to copy strings of tokens1 and tokens2 to tokens you can use strdup() function: here after how you can do it
for (i=0;tokens1[i]!=NULL;i++)
{
tokens[i]=strdup(tokens1[i]);
}
for (j=0;tokens2[j]!=NULL;j++)
{
tokens[i+j]=strdup(tokens2[j]);
}
tokens[i+j]=NULL;