remove \0 from char *

the book i began reading taught me to use char* variable; to store strings, this may not be what i'm looking for... if i'm not mistaken when i tried char * ss=new char[]; it didn't work the way a normal pointer did, i'm using dev c++ now, and i need to find another way to do what i'm doing. The segmentation fault is marked with comments, at least that's the point debug told me a segmentation fault occurred...

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
#ifndef __unix__
#include <cstdlib>
#include <iostream>
#include <fstream>

using namespace std;
char*strung;
char*big(char*);
char*ar1(char*);

/* return 1 = usage */
/* return 2 = didn't make the file */
int n=1;

      char* big(char*oneatatime)
{
      
      char * store=NULL;
      strcat(store," ");
      strcat(store,oneatatime);
      return store;                  // ACCESS VIOLATION OCCURRED SOMETIME AROUND HERE
}
  
  char * ar1(char*add_once)
{
     char* temp;
     temp=add_once;
//     strcat(temp," ");
     return temp;
}





int main(int argc, char *argv[])
{                                                                   // MAIN 
            ofstream doc;
                      
            switch(argc)
                      {
                      case 1:
                           cout << "USAGE: np <filename>\n";
                           return 1;
                      case 2:
                          doc.open(argv[1],ios::out);                      
                      
                      
                      if(doc.is_open())
                      {
                      cout << "FILE OPENED SUCCESSFULLY";
                      doc.close();
                      return 0;
                      }
                      else{
                           cout << "FILE WAS NOT MADE.";
                           return 2;
                           }
 
#ifdef TESTING                      
                      default:
                              cout << "SEE MANUAL";
                              return 5;
                              }
#endif                      
                      
                      
                      
                      
                           default:
                                   
                                   strung=NULL;
                                   /* functions used to make up for my diminished
                                   mental capacity */
                                   strung=ar1(argv[1]);
                                   for(int n=2;n!=argc;n++) 
                                   {                         
                                    strcat(strung,big(argv[n]));
                                   }
            /* AT THIS POINT I THINK WE HAVE A NICE STRING FILLED WITH EVERYTHING YOU TYPED
            ON THE CONSOLE AFTER "NP" LIKE SAY... "NP BILL GATES" 
            STRING CONTAINS BILL GATES
            */
                                   
                                   if(!(strlen(strung))>255)
                                   {                                            //if strung>255
                                   doc.open(strung,ios::out);
                                   /* NESTING IF TO CHECK IF OPEN */
                                   
                                   if(doc.is_open())
                                   {                                            //
                                            cout << "DOCUMENT CREATION SUCCESS";
                                            
                                            doc.close();        
                                   return 0;
                                   }                                            //
                                   
                                   else{                                        //just to make sure this else is for the nested if, right?
                                        cout << "DOCUMENT CREATION FAILED.";
                                        return 2;
                                        }                                       //                                        }                                       //UNKNOWN
                      
                      
                                        }                                       //if strung>255
                      
                      }                                                   // switch closing brace
                      return 0; // this shouldn't get executed
}                                                                         //main closing brace


#endif                                



I know someone else's code can get pretty horrible to look through sometimes, but, any help would be appreciated, please direct me toward a better programming style and such.

of course the access violation occurred when using more than 1 argument.
Last edited on
I don't see new anywhere. All I see is this:
1
2
3
char * store=NULL;
strcat(store," ");
strcat(store,oneatatime);

which is awful.
hmm... give me a bit and i'll try to use char* store= new char[];, but.. you should suggest some alternatives
If you want to return a char *, there are no other alternatives. And don't forget to initialize the array to zero (or at least the first element) before calling strcat().
do you mean.. store=0;
or bzero(store,sizeof(store));
or some other alternative
I mean *store=0; or store[0]=0;
strcat() assumes that the first parameter is a valid C-string, and begins copying the second parameter at strlen(str1).
std::string is the other alternative. I see that you are including C++ headers so why bother using char* at all? Any time that you declare a pointer you have to have a plan for filling the pointer with an address to allocated memory or allocate the memory yourself. You can never pass a null or uninitialized pointer to a c-run time function.
Topic archived. No new replies allowed.