Thread: Changing one character in a char[]

  1. #1
    Registered User codegirl's Avatar
    Join Date
    Jun 2003
    Posts
    76

    Changing one character in a char[]

    This is probably a really silly question but I haven't been able to find an answer... But Visual C++ will not let me do the following code (I get a run-time error):

    Code:
    // In header file
    char *filename;
    
    // In constructor
    filename = "frame0000.tif";
    
    // Later in another method
    ++filename[8];  // This is where I get the error
    How can I can I change one character in a string? MSDN says the above code is illegal (even though I know I've done it in other compilers!) but I can't figure out what to do instead. I've also tried initializing filename using strncpy so it will no longer be constant...

    Thanks for your help!
    My programs don't have bugs, they just develop random features.

  2. #2
    unleashed alphaoide's Avatar
    Join Date
    Sep 2003
    Posts
    696
    What do you want this statement to do?
    ++filename[8]; You can't increment "char." If you want to change the size, increment the index.
    Suggestion: declare "filename" as string and if you want to convert to characters array do "filename.c_string."
    If you want to replace n-th character in a string: filename[n] = 'c'
    Last edited by alphaoide; 09-15-2003 at 03:03 PM.

  3. #3
    Registered User codegirl's Avatar
    Join Date
    Jun 2003
    Posts
    76
    If filename == "frame0000.tif", then that line of code should set filename to "frame0001.tif." I've even got the old program where I've pulled that trick, but it must have only worked under that compiler. If I can't increment char's, how I can I do this without using a ton of if statements?
    My programs don't have bugs, they just develop random features.

  4. #4
    Registered User
    Join Date
    May 2003
    Posts
    161
    It is perfectly legal to modify a character in a string like that - however, it is not legal to modify a string constant.

    Code:
    char* str = "hello world";
    That code sets str to point to the string literal "hello world" in the read only data segment of your program.

    You'll have to malloc some memory and use strcpy(), or use an array.

    Code:
    char* filename = new char[14];
    strcpy(filename, "frame0000.tif";
    ++filename[8];
    delete[] filename;
    or

    Code:
    char filename[] = "frame0000.tif";
    ++filename[8];
    Personally, I would just use a std::string:

    Code:
    std::string filename("frame0000.tif");
    ++filename[8];
    Hope that helps.

  5. #5
    unleashed alphaoide's Avatar
    Join Date
    Sep 2003
    Posts
    696
    I got the question, now, but I guess the thread above answered it. Yup, I would use the last coded myself.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Errors including <windows.h>
    By jw232 in forum Windows Programming
    Replies: 4
    Last Post: 07-29-2008, 01:29 PM
  2. <string> to LPCSTR? Also, character encoding: UNICODE vs ?
    By Kurisu33 in forum C++ Programming
    Replies: 7
    Last Post: 10-09-2006, 12:48 AM
  3. Remove character from string
    By nooksooncau in forum C Programming
    Replies: 11
    Last Post: 06-05-2006, 09:37 AM
  4. about wide character and multiple byte character
    By George2 in forum C Programming
    Replies: 3
    Last Post: 05-22-2006, 08:11 PM
  5. Character handling help
    By vandalay in forum C Programming
    Replies: 18
    Last Post: 03-29-2004, 05:32 PM