ptr is still at the end of the array. What about not re-using ptr? Count the length with a for-loop local variable like: for (char **p = result; p != NULL; p++). Then you can just iterate over result to populate the array.