Char array initialisation problem

  • Thread starter Thread starter GregA
  • Start date Start date
  • Tags Tags
    Array
Click For Summary

Discussion Overview

The discussion revolves around the initialization of char arrays in C++, particularly in the context of reading data from a text file into a char array. Participants explore the constraints of static array sizes and the use of dynamic memory allocation.

Discussion Character

  • Technical explanation
  • Exploratory

Main Points Raised

  • One participant encounters an issue with initializing a char array using a variable length derived from a string's length, leading to confusion about the rules of array initialization in C++.
  • Another participant clarifies that static arrays must have their size known at compile time, suggesting dynamic allocation as a solution.
  • Dynamic allocation is proposed using the syntax char* instuff = new char(len+1); to accommodate the null terminator.
  • Participants discuss the utility of the c_str() member function of strings for passing to functions that require C-style strings, suggesting it as an alternative to manually managing char arrays.
  • One participant expresses newfound understanding regarding the necessity of dynamic allocation and the use of c_str() after receiving assistance.
  • A participant shares their common practice of using c_str() when prompting users for file names, highlighting its relevance in file handling.

Areas of Agreement / Disagreement

Participants generally agree on the need for dynamic allocation when the size of the array is not known at compile time, but there is no consensus on the necessity of using char arrays versus string methods like c_str().

Contextual Notes

Limitations include the assumption that the text file's length is variable and the implications of using static versus dynamic arrays in C++. The discussion does not resolve the broader implications of memory management in C++.

Who May Find This Useful

This discussion may be useful for C++ programmers dealing with string manipulation, file handling, and memory management, particularly those transitioning from static to dynamic array usage.

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());
 

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
4K
  • · Replies 12 ·
Replies
12
Views
3K