0

I want to copy the content of a char **pointer to another pointer. My attempt was:


void test(char **arr, int len) {
    printf("1: ");
    printArr(arr, len);
    char ***res = malloc(sizeof(char **));
    res[0] = malloc(sizeof(char *) * len);
    memcpy(&res[0], arr, len);
    printArr(res[0], len); 

Here I just wanted to copy the contents of arr, which holds several strings, to r[0] whereby len denotes the number of elements in arr. However, when inspecting res[0] I realised that it only stores two times null. As one can tell I'm a very beginner and have been learning C since a few days, so onc can expect simple mistakes.

23
  • Double pointers are already trouble. What's with the triple pointer(!!!) here? You only allocate one element, you only use one element. A one-element array should be a variable. Commented Mar 15, 2021 at 22:15
  • 1
    This code really doesn't make any sense at all. The more I look at it, the more I wonder what is going on. How is this used? What purpose does it serve? Commented Mar 15, 2021 at 22:17
  • @tadman the triple pointer should point to the different string arrays. I create different string arrays and want to check whether they were not already created, so I store them in res to remember which string arrays are already created. This might seem inefficient but since it's a dynamic application with user input I didn't find a simpler solution yet Commented Mar 15, 2021 at 22:18
  • 1
    I realised that it only stores two times null. What is it supposed to store? We can't tell from this incomplete code snippet. Please provide complete code as a minimal verifiable example. Also include the exact expected result vs actual result. Commented Mar 15, 2021 at 22:20
  • 1
    An array of length one is not really an array. Commented Mar 15, 2021 at 22:21

2 Answers 2

2
char ***res = malloc(sizeof(char **));
res[0] = malloc(sizeof(char *) * len);
memcpy(&res[0], arr, len);

The first line allocates space for a single char ** and makes res point at it

The second line allocates space for an array of len pointers to char and makes res[0] point at it.

The third line copies len byes from arr over the top of the memory pointed at by res, overwriting the result of the second malloc call and then scribbling over memory after the block allocated by the first malloc call.

You probably actually want something like

mempy(res[0], arr, len * sizeof(char*));

which will copy an array of len pointers (pointed at by arr) into the memory allocated by the second malloc call.

Sign up to request clarification or add additional context in comments.

2 Comments

unfortunately, your solution gives me a SEG FAULT when trying to print the first string of the copied array with printf("%s", res[0][0]);. arr is of the form {"ab", "bc", "cd"}
That probably means arr is incorrect (or len is wrong), or you are passing it incorrectly to your function.
0

If this is an array of C strings that you need deep copied:

char** array_deep_copy(char **arr, int len) {
    // calloc() makes "allocation of N" calculations more clear
    // that this is N allocations of char* becoming char**
    char **res = calloc(len, sizeof(char*));

    for (int i = 0; i < len; ++i) {
      // Use strdup() if available
      res[i] = strdup(arr[i]);
    }

    return res;
}

Note that this needs a proper release function that will go through and recursively free() those cloned strings or this leaks memory.

If you only need a shallow copy:

char** array_shallow_copy(char **arr, int len) {
    char **res = calloc(len, sizeof(char*));

    // memcpy(dest, src, size) is usually more efficient than a loop
    memcpy(res, arr, sizeof(char*) * len);

    return res;
}

This one doesn't need a recursive free(), you can just free() the top-level pointer and you're done. This one shares data with the original, so if any of those pointers are released before this structure is then you'll have invalid pointers in it. Be careful!

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.