Splitting strings in C I have created some code to split strings. The code will split the given string by the given delimiter into an array which is inside a struct it returns. 

I am using strtok to do the actual tokenizing, but in my code the original string will not be changed.

split.h

#ifndef SPLIT_H
#define SPLIT_H

#ifdef __cplusplus
extern "C" {
#endif

    struct splitString {
        char **tokens;
        unsigned total;
        unsigned size;
    };

    struct splitString *split(const char *, const char *);
    void splitFree(struct splitString **);

#ifdef __cplusplus
}
#endif

#endif /* SPLIT_H */


split.c

#include &lt;stdio.h&gt;
#include &lt;stdlib.h&gt;
#include &lt;string.h&gt;
#include "split.h"

#define DEFAULT_TOKEN_SIZE 10

static struct splitString* initSplitString() {
    struct splitString *s = malloc(sizeof (struct splitString));

    if (s) {
        s-&gt;total = 0;
        s-&gt;size = DEFAULT_TOKEN_SIZE;
        s-&gt;tokens = malloc(sizeof (char *) * DEFAULT_TOKEN_SIZE);

        for (int i = 0; i &lt; DEFAULT_TOKEN_SIZE; i++) {
            s-&gt;tokens[i] = NULL;
        }


        return s;
    }

    return NULL;
}

/**
 * Adjust the size of the tokens variable if necessary
 * @param s
 */
static void adjustSize(struct splitString *s) {
    if (s-&gt;total == s-&gt;size) {
        s-&gt;size += DEFAULT_TOKEN_SIZE;

        char **temp_tokens = realloc(s-&gt;tokens, sizeof (char *) * s-&gt;size);

        if (temp_tokens) {
            s-&gt;tokens = temp_tokens;
        }
    }
}

static void clearAndCopy(char *destination, const char *source, unsigned size) {
    memset(destination, 0, size);
    memcpy(destination, source, size);
}

struct splitString* split(const char *string, const char *delimiter) {
    unsigned size = strlen(string) + 1;
    char tmp[size];
    char *token = NULL;
    struct splitString *s = initSplitString();

    if (s == NULL) {
        goto error;
    }

    clearAndCopy(tmp, string, size);

    token = strtok(tmp, delimiter);

    while (token != NULL) {
        int bsize = strlen(token) + 1;

        s-&gt;tokens[s-&gt;total] = malloc(bsize);

        if (s-&gt;tokens[s-&gt;total]) {
            clearAndCopy(s-&gt;tokens[s-&gt;total], token, bsize);
        }
        else {
            splitFree(&amp;s);
            goto error;
        }

        s-&gt;total++;

        adjustSize(s);
        token = strtok(NULL, delimiter);
    }

    return s;

error:

    return NULL;
}

/**
 * Clear all token and the splitString
 * @param s
 */
void splitFree(struct splitString **s) {
    for (int i = 0; i &lt; (*s)-&gt;total; i++) {
        free((*s)-&gt;tokens[i]);
        (*s)-&gt;tokens[i] = NULL;
    }

    free(*s);
    *s = NULL;
}


Here is just some code I am using to test:

#include &lt;stdio.h&gt;
#include &lt;stdlib.h&gt;
#include "split.h"

int main(int argc, char** argv) {

    char *test_string = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.";
    struct splitString *test = split(test_string, ",");

    printf("Total: %d\n", test-&gt;total);

    for (int i = 0; i &lt; test-&gt;total; i++) {
        printf("String: %s\n", test-&gt;tokens[i]);
    }

    splitFree(&amp;test);

    return EXIT_SUCCESS;
}
Unnecessary code

This function is puzzling to me:


static void clearAndCopy(char *destination, const char *source, unsigned size) {
    memset(destination, 0, size);
    memcpy(destination, source, size);
}



Why do you need to clear the destination buffer when you are going to copy over it immediately afterwards?  All calls to this function could simply be replaced by calls to memcpy instead.

Memory leak

In split_free(), you forgot to free (*s)-&gt;tokens.

Allocation strategy

There are two things I would do differently with respect to how you allocate your tokens:


Instead of using realloc to dynamically resize your token array, I would prescan the input and determine how many tokens there will be, and just allocate once.  This avoids a possible \$O(n^2)\$ reallocation time.
Instead of allocating a buffer for each token and copying the token to it, I would make a single copy of the input string, and then have s-&gt;tokens[i] be pointers to the tokens within the single copy.