How to return an array

So i know that it has the strings library but I prefer to create my own splicer for the experience... what i have is not working for only one reason... what the function returns is different than what the array should equal...

It is using a 1D method to hold an array of arrays since my 2d was fighting with me...

using all the code i paste will give you my simple diagnostics which shows that the function is finishing with the correct output, but the output is not becoming the correct input for the new char*... (everything is commented...)

please inspect the return type, the actual return, the creation of my char*'s and the assignment of one char* to another...

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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#include <iostream>

using namespace std;

static char* CASplice (char* List, char* Splicers)
{
    //Found represents whether a splice was found
    bool Found = false;
    //Last represents whether the last char was a splice
    bool Last = true;
    //Pieces represents the number that the Final[][] will initialize the Final[x][]
    //It starts at 1 because Final[0] represents the number of strings
    int Pieces = 2;
    //Represents the number of chars in the string initially given (always has a null)
    int CharCount = 1;
    //Contains the number of chars in the longest char* that will be made (Will need room for two ints and a null char)
    int LongCont = 3;
    //Contains the current number of chars needed to create the current char* (always has a null)
    int CurCont = 1;
    //For loop to iterate through each char in List[]
    for (int c=0; List[c]!=0; ++c)
    {
        //For loop to iterate through each Splicer
        for (int s=0; Splicers[s]!=0 && !Found; ++s)
        {
            //Checks each char for a splice
            if (Splicers[s]==List[c])
            {
                //If Last was a splice then pieces does not increment
                if (!Last && List[c + 1]!=0)
                {
                ++Pieces;
                cout << "   new piece" << Splicers[s] << List[c] << Pieces << "\n";
                }
                else
                {
                cout << "no new piece" << Splicers[s] << List[c] << Pieces << "\n";
                }
                Found = true;
            }
        }
        //If char is not a splice then...
        //    1. increase the char count by one
        //    2. increase the CurCont
        if (!Found)
        {
            ++CharCount;
            ++CurCont;
            //If the CurCont is greater than LongCont, set LongCont to CurCont
            if(CurCont>LongCont)
            {
                LongCont=CurCont;
            }
        }
        else
        {
            CurCont = 1;
        }
        //Set Last to the value of found
        Last = Found;
        //Reset default value of Found
        Found = false;
    }
    
    //Create Char* to hold all values (It will act as a char**)
    cout << Pieces * LongCont << " " << Pieces << " " << LongCont << "\n";
    char Final[Pieces * LongCont];
    //Set first value to hold the number of strings
    Final[0] = Pieces;
    //Set second value to hold the number of chars held in each string
    Final[1] = LongCont;
    //Give null value to end of the string
    Final[2] = '\0';
    //Set all values to null
    for (int i = 3;i <= Final[0]*Final[1]; ++i)
    {
        Final[i] = '\0';
    }
    //s holds the current string line
    int l=1;
    //p holds the current char position
    int p=0;
    //reset Found
    Found = false;
    //reset Last
    Last = true;
    //For loop to iterate through each char in List
    for (int c = 0;List[c]!=0;++c)
    {
        //For loop to iterate through each char in Splicers
        for (int s = 0;Splicers[s]!=0&&!Found;++s)
        {
            //If splicer equals char then set found to true
            if (List[c]==Splicers[s])
            {
                Found = true;
            }
        }
        //If found and the last char was not a splice
        if (Found && !Last)
        {
            //Increment l by one to move to the next array indice
            ++l;
            //Set position to 0
            p = 0;
            //this is so you can see the output nicer...
            cout << "\n";
        }
        //If not found
        else if (!Found)
        {
            //Set final[l][p] = List[c]
            Final[(Final[1]*l) + p] = List[c];
            //this is so you can see the output nicer...
            cout << List[c] << " Sent to position" << Final[1]*l + p << "\n";
            //This increments the position by one
            ++p;
        }
        //Set last to represent whether the last char was splice
        Last = Found;
        //Reset Found
        Found = false;
    }
    //Also just so that you can see that it came out properly...
    for (int i; i<Final[0]*Final[1];++i)
    {
        cout << Final[i] << " " << i << "\n";
    }
    //Also just to exemplify that it works...
    for (l = 0; l < Final[0]; ++l)
    {
        for (p = 0; Final[l*Final[1]+p]!=0; ++p)
        {
            cout << Final[l*Final[1]+p];
        }
        cout << ".\n";
    }
    cout << "\n";
    //Returns final
    return Final;
}
int main()
{
    char* outputholder= CASplice("type phrase to test here","type delimiter set here...");
    for (int i = 0; i <142; ++i)
    {
        cout << outputholder[i] << " " << i << "\n";
    }
    cin.get();
}
There's a bug somewhere..
Last edited on
Where would the bug be?

in the main you were supposed to change the phrase and the delimiters... and then you would see that the array was dynamically made...

the 142 was just a arbitrary number... unfortunately the n[0]*n[1] is not a proper length so i just chose a long number...
closed account (DSLq5Di1)
These are your problems, well the ones the compiler picked up anyhow..
(67): error C2057: expected constant expression
(67): error C2466: cannot allocate an array of constant size 0
(67): error C2133: 'Final' : unknown size
(69): warning C4244: '=' : conversion from 'int' to 'char', possible loss of data
(71): warning C4244: '=' : conversion from 'int' to 'char', possible loss of data
(140): warning C4172: returning address of local variable or temporary
1
2
char* Final = new char[Pieces * LongCont]; // this is dynamic allocation
delete[] Final; // dont forget clean up when it is no longer needed 
OMG! That fixed it! Thank you soo much!
Topic archived. No new replies allowed.