Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Char array initialisation problem

  1. Jun 25, 2008 #1
    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: Jun 25, 2008
  2. jcsd
  3. Jun 25, 2008 #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.
     
  4. Jun 25, 2008 #3

    jtbell

    User Avatar

    Staff: Mentor

    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 (Text):

    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 (Text):

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

    Code (Text):

    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: Jun 25, 2008
  5. Jun 26, 2008 #4
    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:
     
  6. Jun 26, 2008 #5

    jtbell

    User Avatar

    Staff: Mentor

    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 (Text):

    ifstream myfile;
    string filename;

    cout << "Name of input file: ";
    cin >> filename;
    myfile.open (filename.c_str());
     
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?



Similar Discussions: Char array initialisation problem
  1. Arrays problem (Replies: 1)

  2. Two char arrays (Replies: 11)

Loading...