Help initializing char**

So a lot of this code is kinda excess but it is more or less for me to know how it flows through my for commands... it is mostly notes, and var names are kinda inate... My problem though is making the char**

I know I could use strings and use something from it to splice it but it is more fun to create my splicer so please do not tell me to include strings...

I want it to return an array of char arrays

I want the first item char[0][] to be the number of arrays in the array hence the "+ 1" for determining

I want the char array length to be as long as the longest one +1 to be able to fit a null character at the end... but my real problem is creating the char**

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
inline static char** CASplice (char* List, char* Splicers)
{
    //For loop to iterate through each char
    bool Found = false;
    int Pieces = 1;
    int CharLength = 0;
    int CharCount = 0;
    int LongCont = 0;
    int CurCont = 0;
    for (;List[CharLength]!=0;++CharLength)
    {
        //For loop to iterate through each Splicer
        for (int s=0;Splicers[s]!=0 && !Found;++s)
        {
            //If Char is a splicer then... Increment pieces by one and set Found to true
            if (Splicers[s]==List[CharLength])
            {
                ++Pieces;
                Found = true;
                cout << Splicers[s] << List[CharLength] << Pieces << "\n";
            }
        }
        //If char is not a splice then... increase the CurConst count by one
        if (!Found) {++CharCount;++CurCont; if(CurCont>LongCont) {LongCont=CurCont;}}
        //If char is found then set CurCont to 0
        if (Found) {CurCont = 0;}
        //Reset default value of Found
        Found = false;
    }
    cout << CharLength;
    char hi[(CharLength - CharCount) + 1][LongCont + 1];
    hi[0][0] = *pie;
    hi[0][1] = '\0';
    cout << Pieces << " " << CharCount << " " << List << " " << Splicers << "\n";
    return hi;
}
Last edited on
Currently you are returning an array that is defined on the stack, which won't work, stack is gone after function returns. You need to use dynamic memory.

// this will give you an array of char pointers
char **pArrayOfPtrs = new char * [ some_size ];

Then you need to allocate each array of chars (I'm only allocating first one as example)
pArrayOfPtrs[0] = new char [ some_length ];
Topic archived. No new replies allowed.