CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Aug 2011
    Posts
    1

    Very basic question

    This is pretty embarrassing... I've been coding in C++ off and on for 5 years now. I'm trying to run the following routine but getting a seg failt when I try to assign the 'f' . It doesn't matter if I try to assign it to str[0] or str[3] or if I try to assign 'o' or '7'. I also get the same problem if I declare str as char str[5].
    Help?

    int main(){
    char* str;
    str = new char[5];
    str = "test";
    cout << str << endl;
    str[1] = 'f'; // causes seg fault
    cout << str << endl;
    return 0;
    }

  2. #2
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,637

    Re: Very basic question

    "test" is a string literal.
    str = "test"; assigns the address of the string literal to str.
    str[1] = 'f'; tries to modify the string literal which you can't do.

    you need
    Code:
    strcpy(str, "test");
    to assign a value to str.

  3. #3
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Very basic question

    If you are really interested in programming C++ rather than C, then consider declaring str as a std::string rather than a char array/pointer.

  4. #4
    Join Date
    Feb 2009
    Posts
    326

    Re: Very basic question

    Yes GCDEF is correct.

    Just thought i would add some points (correct me if i am wrong):
    1) C-Strings

    C-Strings are treated as an array of characters ending with a null character '\0'
    C-String pointer would contain the starting address of the C-String (length of the string is determined based on the occurrence of the null character '\0')

    2) I suppose a warning should have been thrown for the following statement (warning messages tend to vary based on the compiler):

    Code:
    str = "test";
    GCC warning message:
    Code:
    warning: deprecated conversion from string constant to ‘char*’
    It means that str is declared as char* but is being assigned the address of constant char

    Therefore a char* variable is being assigned a const char* value which is not correct.

    "test" is a literal whose value can't be changed and therefore the address of this literal would be of the const char*

    This warning would not be thrown, if str was declared as a const char*
    and if str was declared as const* then str[1] = 'f' wouldn't be allowed

    3) As GCDEF has stated, strcpy is what you intended to use

    strcpy will copy the the characters that "test" to the memory location that str points to.

    4) Memory for the literal is allocated and is available till the end of the program

    So printing the address of the literal "test" several times in the same program would be same address till the end of the program

    5) print the address of str to understand better before and after the statement str = test

    Note - This program still will throw a warning (at least in GCC)

    Code:
    #include <iostream>
    using std :: cout;
    using std :: endl;
    
    int main()
    {
        char* str;
        str = new char[5];
        cout << "str = " << static_cast<void*>(str) << endl;
            
        str = "test";
        cout << "str = " << static_cast<void*>(str) << endl;
            
        cout << str << endl;
        str[1] = 'f'; // causes seg fault
        cout << str << endl;
        return 0;
    }
    Note - static_cast is used so that cout doesn't print the value of the C-string str point to and instead prints the address of the C-String

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured