Char array initialisation problem

  • Thread starter Thread starter GregA
  • Start date Start date
  • Tags Tags
    Array
Join the discussion
Ask a follow-up here, or get your own question answered by working scientists, mathematicians and engineers — people, not an autocomplete.
Real named experts · corrections over time · the nuance an AI answer skips
4 replies · 6K views
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:
Physics 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());