Char array initialisation problem

  • Thread starter Thread starter GregA
  • Start date Start date
  • Tags Tags
    Array
Click For Summary
The discussion centers around the challenges of initializing a char array in C++ when the size is determined at runtime. The user initially attempts to create a char array with a size based on the length of a string read from a file, but encounters an error because static arrays require a size known at compile time. The solution involves using dynamic memory allocation with `new`, allowing for the creation of a char array with a size defined at runtime. It is emphasized that when using char arrays as C-style strings, a null terminator ('\0') must be included, which is why the size is set to `len + 1`. The use of the `c_str()` member function of the string class is recommended for passing strings to functions expecting C-style strings, simplifying the process of handling string data. The user expresses gratitude for the insights, particularly regarding the use of `c_str()` for file operations.
GregA
Messages
210
Reaction score
0
I'm currently playing around with strings and char arrays and having problems initialising a char array properly. Basically I've got a text file whose length I want to check before putting the contents into a char array

I define my ifstream variable as in_data and a string as str1.
Then opening some small text file on my computer I use getline(in_data, str1)
I then initialise const int strlen = str1.length()
Now if I initialise char instuff[strlen] I get a problem saying I've initialised my array with a variable. Of course if I put a number in there directly I'm fine but surely I've already defined instuff to be constant (whatever value strlen took) and am scratching my head as to why I'm having a problem.

I apologise for the poor variable names (I'm just experimenting at the mo), and for not putting the entire program here (it is on a computer that isn't online)

Can anyone help?
 
Last edited:
Technology news on Phys.org
have found out that "strlen" isn't something I should be using as a variable :redface:...but using any other variable name such as "len" gives the same problem.
 
In C++, the size of a static array has to be known at compilation time. If the size isn't known until you actually run the program, you have to allocate the array dynamically:

Code:
char* instuff = new char(len+1);

Remember that char arrays used as C-style strings have to have a null ('\0') at the end. That's why I used len+1. If you're getting the chars one by one from a C++ style string, you need to insert the null yourself.

Why do you need a char array? If you just need to pass it to a function that expects to receive a const char*, you can use the string's c_str() member function:

Code:
foofunc (str1.c_str());

If you really need your own array, you can use c_str() to initialize it:

Code:
char* instuff = new char(len+1);
strcpy (instuff, str1.c_str());

I think this takes care of the terminating null for you, too.
 
Last edited:
jtbell said:
In C++, the size of a static array has to be known at compilation time. If the size isn't known until you actually run the program, you have to allocate the array dynamically:

Code:
char* instuff = new char(len+1);

Remember that char arrays used as C-style strings have to have a null ('\0') at the end. That's why I used len+1. If you're getting the chars one by one from a C++ style string, you need to insert the null yourself.

Why do you need a char array? If you just need to pass it to a function that expects to receive a const char*, you can use the string's c_str() member function:

Code:
foofunc (str1.c_str());

If you really need your own array, you can use c_str() to initialize it:

Code:
char* instuff = new char(len+1);
strcpy (instuff, str1.c_str());

I think this takes care of the terminating null for you, too.

Hmm...I didn't consider that the size of that variable would only be known after the program has compiled and it toddles off to read my text file...I didn't know about the c_str() member neither!

Thankyou very much for your help :smile:
 
I most often use c_str() when I want the user to enter the name of a file to open, because open() requires a C-style string:

Code:
ifstream myfile;
string filename;

cout << "Name of input file: ";
cin >> filename;
myfile.open (filename.c_str());
 
Learn If you want to write code for Python Machine learning, AI Statistics/data analysis Scientific research Web application servers Some microcontrollers JavaScript/Node JS/TypeScript Web sites Web application servers C# Games (Unity) Consumer applications (Windows) Business applications C++ Games (Unreal Engine) Operating systems, device drivers Microcontrollers/embedded systems Consumer applications (Linux) Some more tips: Do not learn C++ (or any other dialect of C) as a...

Similar threads

  • · Replies 89 ·
3
Replies
89
Views
6K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 10 ·
Replies
10
Views
7K
  • · Replies 66 ·
3
Replies
66
Views
6K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 2 ·
Replies
2
Views
17K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 28 ·
Replies
28
Views
3K
  • · Replies 12 ·
Replies
12
Views
3K