0

I'm writing a program in C and I can't find out how to do the following. I have two NULL terminated arrays of pointers to strings, namely

char *tokens1[size1]
char *tokens2[size2]

I want to merge them into a third array of pointers to strings, namely

char **tokens;

I've tried the following but it doesn't work:

char *tokens1[size1]
char *tokens2[size2]
char **tokens;

/* code to fill the *tokens1[] and *tokens2[] arrays with string values */

tokens = (char*) malloc(size1+size2+1);
strcpy(tokens, tokens1);
strcat(tokens, tokens2);

Could you please help me?

7
  • 1
    To be technically correct, you have arrays of pointer to char. C does not have a string type. Commented Oct 29, 2012 at 14:44
  • @chris, I believe we are to understand that this has happened in the commented region. Commented Oct 29, 2012 at 14:46
  • @cmh, Ah, I read that part as a prelude to the following section. Commented Oct 29, 2012 at 14:48
  • 1
    strcat/strcpy are for copying 'strings' not arrays of pointers Commented Oct 29, 2012 at 14:49
  • 1
    It's unclear whether you want to merely copy the pointers to the strings or create copies of those strings and store the new pointers Commented Oct 29, 2012 at 15:03

3 Answers 3

4

You are copying pointer values, not strings, so you need to use memcpy instead of strcpy/strcat:

int i, j;
/* Find the current size of tokens1 and tokens 2 */
for (i=0; tokens1[i] != NULL; i++) 
   ;
for (j=0; tokens2[j] != NULL; j++) 
   ;
/* Allocate enough memory to hold the result */
tokens = calloc(i + j + 1, sizeof(char*));
/* Copy the arrays */
memcpy(tokens, tokens1, i * sizeof(char*));
memcpy(tokens + i, tokens2, j * sizeof(char*));
/* Since we used calloc, the new array is initialized with NULL:s. Otherwise we would have to NULL-terminate it like so: */
tokens[i+j] = NULL;
Sign up to request clarification or add additional context in comments.

3 Comments

the pointer arrays are two NULL terminated arrays. so you have to copy tokens1 till you find the NULL address and not copy the whole array
Or how about tokens = (char**) malloc((size1+size2+1)*sizeof(char*));
@Klas Lindbäck. indeed you have to use tokens = (char**) malloc((size1+size2+1)*sizeof(char*)); instead of tokens = (char*) malloc(size1+size2+1);
1

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;

3 Comments

oops I missed this tokens = (char*) malloc(size1+size2+1); make me confused
@MohamedKALLEL: Sorry! I made a mistake. It isn't a static allocation of the tokens array. I want to allocate memory for this array with malloc(). I tried tokens = (char*) malloc(size1+size2+1); but I got the following warning from gcc: assignment from incompatible pointer type [enabled by default]. Any ideas?
@Stavros so you have to remove char *tokens[size1+size2] and replaced by char **tokens : double pointers ;-). I will update my answer with this
0

I just assumed that memory is well allocated for each pointers

    int i;
    for (i=0 ; i< size1 ;i++)
    {
     strcpy (tokens[i],tokens1[i]);
    }
    int j=i;
    for(i=0;i<size2;i++,j++)
    {
     strcpy(tokens[j],tokens2[i]);
    }
    tokens[j]='\0';

2 Comments

This is workable but it does unnecessary copying. Klas' answer is more efficient, because it only copies the pointers, instead of copying entire strings that have already been read into memory.
@TomDignan: Yeah you are right .. but I thought of implementing strcpy while solving rather thinking of memcpy

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.