how to return return char array from stack

Hi everbody!

I have a code that reads a txt file with words separated by space and reorders them in alphabetical order. i push each of the words in an array of 26 stacks as char arrays and where each letter in the alphabet corresponds to one of the stacks. the problem is that i can cout these words with an inner stack object function but i do not know how to return the char array.

here is my code
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
#include <fstream>
#include <iostream>
#include <string>

using namespace std;

struct node
{
   char data[255];
   node *link;
};

class lstack
{
      private:
              node* top;

      public:
             lstack()
             {
                 top=NULL;
             }
             
             void push(char n[])
             {
                 node *tmp;
                 tmp=new node;
                 if(tmp==NULL)
                    cout<<"\nSTACK FULL";
                    
                 strcpy(tmp->data, n);
                 tmp->link=top;
                 top=tmp;
             }

             char *pop()  //function with a return type of pointer to a char array
             {
                node *tmp;
                char n[255];
                tmp=top;
                strcpy(n, tmp->data);
                top=top->link;
                delete tmp;
               return n;               //THIS DOES NOT WORK
             }
             
             bool is_empty(){
                   return (top==NULL);     
                        }

             ~lstack()
             {
                 if(top==NULL)
                    return;

                 node *tmp;
                 while(top!=NULL)
                 {
                    tmp=top;
                    top=top->link;
                    delete tmp;
                 }
             }
};

int main() {
 
lstack s[26];
char a, word[255];
ifstream fin("pasts.i3", ios::out);

while(fin)
{//clear arrays
    for(int i=0; i<255; i++){ 
    word[i]=NULL;
    }
    fin.get (a);                                  
    
        for(int i=0;fin && a!=32; i++)    
        {
                if(a>=97&&a<=122){      
                word[i]=a;
                }
        fin.get (a);
        }
s[(int)word[0]-97].push (word);    
}

for(int b=0; b<=25; b++){
    for(int i=1; !s[b].is_empty(); i++){
    cout << i << ". ";
    cout<<s[b].pop();
    cout << " !\n";
    }
}

fin.close();

system ("pause");
return 0;
}


please help me!
and maybe any other advices
Last edited on
Pop method in your class allocates the array local to the function. This array will get destroyed at the end of the function & is therefore an error to return it. You have several options to fix this. You can make this array a class member variable to keep it alive for the lifetime of the object but you may need to modify the other parts of your program to accommodate this change. The other option is to allocate the array dynamically at run time & then delete it manually when done with it. Alternatively, you could also make this array static if it is something that is possible with your program design. Finally, you could very well replace arrays with vectors and return them by values too - easy & safe option I would say. I have not gone through your code completely to give you a precise answer but I would highly recommend you to check all the above possible alternatives. Good Luck.
Last edited on
Topic archived. No new replies allowed.