Thread: Difference between char x[n] and char* x=new char[n]

  1. #1
    Registered User
    Join Date
    Aug 2007
    Posts
    85

    Difference between char x[n] and char* x=new char[n]

    Hi. noob warning.

    Why does this work:
    Code:
    char* filename=new char[256];
    and this does not:
    Code:
    char filename[256];
    in this context:
    Code:
    filename=argv[2];
    (error is: 25 G:\proiecte\xorcrypt\main.cpp incompatible types in assignment of `char*' to `char[256]') . Aren't they both char ?

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    You aren't copying the string in argv[2] in that last line. You are assigning the pointer that points to the string to the filename variable. When you used new, filename was a pointer, when you did not it was an array. You can assign a pointer to a pointer but not a pointer to an array.

    However, it doesn't really matter, since it is doubtful that you want either. Presumably you want to copy the string, in which case you would use strcpy, not operator=.

    And of course, in C++, it is generally best to use C++ strings. Assigning with operator= would work if you used the C++ string class.

  3. #3
    Registered User
    Join Date
    Aug 2007
    Posts
    85
    I can't pass that string to an ifstream filename if it's a string.

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Use this.
    Code:
    std::string filename = "file.txt";
    std::ifstream file;
    
    file.open(filename.c_str());
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  5. #5
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    you will use c_str() member in this case
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  6. #6
    Registered User
    Join Date
    Aug 2007
    Posts
    85
    hey, that works. thanks.

    just as a thing i hit right by mistake:
    Code:
            char ch;
            while( fin.get( ch ) ) {
                filedata.append( &ch );
                fsize++;
            }
    append() needs a & operator before a char? I wasn't able to pass a char, but all works fine this way.
    By the way, is this the right way to read everything from a file into a variable (ios::binary set) ?

  7. #7
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >append() needs a & operator before a char?
    To append a single char, use push_back():
    Code:
                filedata.push_back( ch );
    Or you can also use:
    Code:
                filedata += ch;

  8. #8
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> I wasn't able to pass a char, but all works fine this way.
    You got "lucky". Passing a pointer to char (which is what the & did) means that append reads each char in memory one at a time until it finds the null character. The next char in memory was probably just the null character (which has the value 0), so it only appended one character.

    If you are reading a binary file into a variable, then reading character by character might be inefficient. If your program doesn't have any performance issues then it's fine. Otherwise, you might want to read into a buffer with read(), then append that buffer to your string.

Popular pages Recent additions subscribe to a feed