Q:

C Function Returning a Pointer to a Pointer

I'm a bit new to C here, and just wanted to understand a few things about pointers, pointers to pointers, and strings. Here's what I have written down so far:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

/* Return a pointer to an array of two strings. The first is the characters
   of string s that are at even indices and the second is the characters from
   s that are at odd indices */

char **parity_strings(const char *s) {
    int len = strlen(s);
    char **lst = malloc(sizeof(char*)*2);
    for(int i=0; i<2; i++){
        lst[i] = malloc(sizeof(char)*(len/2));
    }
    for(int i=0; i<len; i++){
        if(i%2==0){
            (*lst)[0]=s[i];
        }else{
            (*lst)[1]=s[i];
        }
    }
    return lst;

}

int main(int argc, char **argv) {
    char **r = parity_strings(argv[1]);
    printf("%s %s %s", r[0], r[1], argv[1]);
    return 0;
}

So I want to dynamically allocate the memory needed for both the string array, and the strings themselves. I want to know if I have the right idea here by wanting to return a pointer of type char** which points to two pointers of type char*, and then from there access each char of the string. 
My output is not as expected. I'm relatively new to C, so there are things I'm still learning about.
Any help would be appreciated, thanks.

A:

First, make sure to allocate space for the null byte in the output strings:
    for(int i=0; i<2; i++){
        /* Add an extra space for the \0 */
        lst[i] = malloc(sizeof(char)*(len/2 + 1));
    }

Your main problem was the weird (*lst)[0]=s[i]; part. This has to do with how arrays work in C.
The name of an array is best thought of as a pointer to the zeroth element. So, (*lst) is exactly equivalent to writing lst[0]. so (*lst)[0] was just repeatedly overwriting the first character in the first array with the latest even letter, while (*lst)[1] was just repeatedly overwriting the second letter of the first array. The second array was never modified since it was allocated, and just contained random data.
    for(int i=0; i<len; i++){
        if(i%2==0){
            /* i/2 makes sure every space gets filled,
            remember / means integer division in C */
            lst[0][i/2]=s[i];
        }else{
            lst[1][i/2]=s[i];
        }
    }
    /* Null terminate both strings, to be safe */

    /* terminate one early if the string was odd */
    lst[0][len/2 -len%2] = '\0';

    lst[1][len/2 ] = '\0';

    return lst;

}

This solution is 'quick and dirty' - one of the arrays always gets double terminated, but that's ok since we allocated room for both of them.

