Concatenate double pointer char array in C

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



Concatenate double pointer char array in C



There are many questions about how to concatenate char array in C, but what I want to know is different from that. I would like to know how to concatenate "double pointer" char array in C.



For example,


const char *array1 = "aa", "bb";
const char *array2 = "cc", "dd", "ee";



I want to get the new double pointer array of char like this:


char **new_array = "aa", "bb", "cc", "dd", "ee";
// this code actually doesn't work, though



I want to know the simplest and fastest way to realize this.



EDIT: In fact, I am planning to create a program below.


int main(int argc, char **argv)
pid_t pid;
char **args = "sh", "-c" + argv; // this code doesn't work
posix_spawn(&pid, "/bin/sh", NULL, NULL, (char* const*)args, NULL);



This program just passes arguments after "sh -c".





We need some context i think. Are these runtime strings? If not, you have the quickest way. Just write it.
– Fantastic Mr Fox
Aug 10 at 11:05





There is no standard function like strcat for combining generic arrays. You need to do it manually.
– Gerhardh
Aug 10 at 11:05



strcat





I've added the detail context. I want to add "sh", "-c" argument before the original arguments (argv**).
– subdiox
Aug 10 at 11:14





Simply make an array of pointers. Have the two first pointers point at string literals and the rest to each item in argv.
– Lundin
Aug 10 at 11:55




2 Answers
2



Sure, just allocate a buffer for that


#define _GNU_SOURCE 1
#include <stdio.h>
#include <strings.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
int main()

#define ARRAY_SIZE(x) (sizeof(x) / sizeof(x[0]))
const char * const array1 = "aa", "bb";
const size_t array1_size = ARRAY_SIZE(array1);
const char * const array2 = "cc", "dd", "ee";
const size_t array2_size = ARRAY_SIZE(array2);
const size_t concat_len = array1_size + array2_size;
const char ** const concat = malloc(concat_len * sizeof(*concat));

assert(concat != NULL);
for (size_t i = 0; i < concat_len; ++i)
concat[i] = strdup(i < array1_size ? array1[i] : array2[i - array1_size]);
assert(concat[i] != NULL);


for (size_t i = 0; i < concat_len; ++i)
printf("concat[%zu] = (%zu) '%s'n", i, strlen(concat[i]), concat[i]);


for (size_t i = 0; i < concat_len; ++i)
free(concat[i]);

free(concat);

return 0;



But we should be able to do better.





It worked! If no better answer comes up, this is temporarily the answer. Thank you.
– subdiox
Aug 10 at 11:44


#include<stdio.h>
#include <string.h>
#include <stdlib.h>

void main()

const char *array1 = "aa", "bb";
const char *array2 = "cc", "dd", "ee";


int arraySize1 = (sizeof(array1)/sizeof(char*));
int arraySize2 = (sizeof(array2)/sizeof(char*));
char **resultArr = (char **)malloc(sizeof(char *)*(arraySize1+arraySize2));
int i =0;

/* copy array1*/
for(i=0;i<arraySize1;i++)

resultArr[i] = malloc(strlen(array1[i]) +1);
strcpy(resultArr[i], array1[i]);


/*copy array2*/
for(i=0;i<arraySize2;i++)

resultArr[arraySize1+i] = malloc(strlen(array2[i])+1);
strcpy(resultArr[arraySize1+i], array2[i]);


/*print resulting array*/
for(i=0;i<arraySize1+arraySize2;i++)

printf("%s ", *(resultArr+i));






malloc(sizeof(array1[i])) ?? If you initialise with some decent length strings the program crashes.
– Weather Vane
Aug 10 at 11:40



malloc(sizeof(array1[i]))





. . . because sizeof(array1[i]) is the size of a pointer, not the length of a string.
– Weather Vane
Aug 10 at 11:46


sizeof(array1[i])





Yes I got it, that was typo.
– kiran Biradar
Aug 10 at 11:48






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

Creating a leaderboard in HTML/JS