Char array initialisation problem

  • Thread starter GregA
  • Start date
  • Tags
    Array
In summary, 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. 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. If you really need your own array, you can use c_str() to initialize it.
  • #1
GregA
210
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
  • #2
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.
 
  • #3
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:
  • #4
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:
 
  • #5
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());
 

What is a "char array initialisation problem"?

A "char array initialisation problem" refers to an issue encountered when attempting to initialize a character array (char array) in a computer program. This can occur when the array is not properly declared or when the initialization values are incorrect.

Why is char array initialisation important?

Char arrays are commonly used in computer programming for storing and manipulating strings of characters. Proper initialization of a char array ensures that the correct memory space is allocated and that the array is ready for use in the program.

What are some common causes of char array initialisation problems?

Some common causes of char array initialisation problems include incorrect syntax, mismatched data types, and missing or incorrect initialization values. These issues can lead to errors or unexpected behavior in the program.

How can I troubleshoot a char array initialisation problem?

To troubleshoot a char array initialisation problem, you can start by checking the syntax of your code and ensuring that all data types and initialization values are correct. You can also use debugging tools or print statements to track the flow of your program and identify any potential issues.

How can I prevent char array initialisation problems in the future?

To prevent char array initialisation problems, it is important to follow proper coding conventions and double check your code for any errors or typos. It can also be helpful to use built-in functions or libraries for working with char arrays, as these are often designed to handle potential issues and errors.

Similar threads

  • Programming and Computer Science
3
Replies
89
Views
4K
  • Programming and Computer Science
Replies
1
Views
527
  • Programming and Computer Science
Replies
5
Views
2K
  • Programming and Computer Science
2
Replies
66
Views
4K
  • Programming and Computer Science
Replies
10
Views
6K
  • Programming and Computer Science
Replies
3
Views
1K
  • Programming and Computer Science
Replies
2
Views
16K
  • Programming and Computer Science
Replies
13
Views
1K
  • Programming and Computer Science
Replies
4
Views
2K
Replies
10
Views
956
Back
Top