I'm trying to split an array which is that of unsigned char where NULL marks the split point
I have written this code, but it just looks clunky, and I would like to ask advice on any other
more efficient methods of doing such a thing there might be.

#include "stdafx.h"
#include <stdlib.h>
#include <iostream>
#include <sstream>
#include <stdio.h>

#define mymaxarray 256

typedef unsigned char uchar;

using namespace std;

void testFunc00(uchar *, int, uchar **);



int _tmain(int argc, _TCHAR* argv[])
{
    char c;
    uchar a1[mymaxarray] = {0};
    uchar a2[mymaxarray] = {0};
    uchar a3[mymaxarray] = {0};
    uchar a4[mymaxarray] = {0};

    uchar * arrayofarray[4] = {a1,a2,a3,a4};

    uchar testarray[30] = {'1','1','1','\0','1','1','1','1','1','1','1','\0','1','1','1','1','1','1','1','1','1','1','1','1','1','\0','1','1','1','\0'};

    testFunc00(testarray, 30, arrayofarray);

    int x = 0;

    while (a1[x]) {
        cout << a1[x] << endl;
        x++;
    }

    cout << endl;
    x = 0;
    while (a2[x]) {
        cout << a2[x] << endl;
        x++;
    }

    cout << endl;
    x = 0;
    while (a3[x]) {
        cout << a3[x] << endl;
        x++;
    }

    cout << endl;
    x = 0;
    while (a4[x]) {
        cout << a4[x] << endl;
        x++;
    }

    cin >> c;
    return 0;
}



void testFunc00(uchar * in, int len, uchar ** out){

    int i = 0;
    int n = 0;
    while (in[i]) {

        out[0][n] = in[i];
        i++;
        n++;
    }

    i++;
    n = 0;
    while (in[i]) {

        out[1][n] = in[i];
        i++;
        n++;
    }

    i++;
    n = 0;
    while (in[i]) {

        out[2][n] = in[i];
        i++;
        n++;
    }

    i++;
    n = 0;
    while (in[i]) {

        out[3][n] = in[i];
        i++;
        n++;
    }

}

Sorry if code is unformatted, cannot seem to preview.

Whenever you see code being repeated over and over, a light should come up saying: "I could probably use a loop for this!". For starter, you could easily condense your function into a single set of nested for-loops, as so:

void testFunc00(uchar * in, int len, uchar ** out) {
    int i = 0;
    for(int j = 0; j < 4; ++j) {
      for(int n = 0; (i < len) && (in[i] != '\0'); ++i, ++n)
        out[j][n] = in[i];
      ++i;
    }
}

You could probably add the maximum number of splits as a parameter too:

void testFunc00(uchar * in, int len, uchar ** out, int max_splits) {
    int i = 0;
    for(int j = 0; (i < len) && (j < max_splits); ++j) {
      for(int n = 0; (i < len) && (in[i] != '\0'); ++i, ++n)
        out[j][n] = in[i];
      ++i;
    }
}

Thanks for clearing this up as I've been experiancing some problems with the script!
Look forward to your new posts.

Hi mike 2000 17.
I'm happy to say that light did flash, and I've been trying to make a loop
which has been failing, by always reporting the amount of arrays as 10 (count)
and printing the garbage after the last '\0'

I will look at your code and try to implement it.
Thank you for time and support.

#include "stdafx.h"
#include <stdlib.h>
#include <iostream>
#include <sstream>
#include <stdio.h>

#define mymaxarray 256

typedef unsigned char uchar;

using namespace std;

int testFunc00(uchar *, int, uchar **);



int _tmain(int argc, _TCHAR* argv[])
{
    int arraycount = 0;
    char c;
    uchar a1[mymaxarray] = {0};
    uchar a2[mymaxarray] = {0};
    uchar a3[mymaxarray] = {0};
    uchar a4[mymaxarray] = {0};
    uchar a5[mymaxarray] = {0};
    uchar a6[mymaxarray] = {0};
    uchar a7[mymaxarray] = {0};
    uchar a8[mymaxarray] = {0};
    uchar a9[mymaxarray] = {0};
    uchar a10[mymaxarray] = {0};

    uchar * arrayofarray[10] = {a1,a2,a3,a4,a5,a6,a7,a8,a9,a10};

    uchar testarray[30] = {'1','1','1','\0','1','1','1','1','1','1','1','\0','1','1','1','1','1','1','1','1','1','1','1','1','1','\0','1','1','1','\0'};

    arraycount = testFunc00(testarray, 30, arrayofarray);

    cout << "Number of arrays filled " << arraycount << endl;

    int x = 0;

        for (int i = 0; i < arraycount; i++) {

        while (arrayofarray[i][x]) {
            cout << arrayofarray[i][x] << endl;
            x++;
        }
        cout << endl;
        x = 0;
    }

    cin >> c;
    return 0;
}



int testFunc00(uchar * in, int len, uchar ** out){

    int count = 1;
    int i = 0;
    int n = 0;

    for (int numarrays = 0; numarrays < 9; numarrays++) {

        if (in[i]) {
            while (in[i]) {

                out[numarrays][n] = in[i];
                i++;
                n++;
            }
            count++;
            i++;
        }
        else {
            return count;
        }
        n = 0;

    }

    return count;
}

Edited 4 Years Ago by Suzie999

This question has already been answered. Start a new discussion instead.