简体   繁体   中英

Why doesn't the following statement work?

Today I gave a test there was the following question written and because I am new to C++, I became confused to the following question.

Why doesn't the following statement work?

char str[ ] = "Hello" ;
strcat ( str, '!' ) ;
char str[] = "Hello";
strcat (str, '!') ;

strcat second argument must be a pointer to a string, but you are passing a character constant.

The correct call would be strcat(str, "!"); (note the " instead of the ' ) but you also need to reserve enough space in str which is only large enough to hold the "Hello" string. For example, for your test, you can reserve more bytes with char str[64] = "Hello";

strcat() calls for pointer for both arguments.

'!' will converted to an (invalid for many chance) pointer by implementation-defined manner, then the program may crash for Segmentation Fault.

Note that

char str[ ] = "Hello" ;
strcat ( str, "!" ) ;

won't work well either due to lack of buffer.

char str[ ] = "Hello" ;
strcat ( str, '!' ) ;
              ^^^ --- this is of type char

strcat signature is :

char * strcat ( char * destination, const char * source );
                                    ^^^^^^^^^^^^^^^^^^^

so, second parameter is of type const char* and not char . You must pass either string literal or variable of type const char* . Actually string literals are of type const char[] but they decay to const char* when being assigned to.

strcat function expects two strings. '!' is a character. in order to concatenate safely,your array must be big enough to hold the other string,so change '!' to "!" ,and str[] to str[8] or more.

int main(void)
{
    char str[20] = "Hello" ;
    strcat ( str, "!" ) ;
    printf("%s\n",str);

}

Two problems:

  1. The immediate issue is that the second parameter must be a const char* . strcat reads the memory starting at that location, until the \\0 is reached. If no \\0 can be reached then the function behaviour is undefined.

  2. It's up to you to make sure that str is large enough to receive the concatenated string. If not, then the behaviour is undefined.

On the second point, you can write something like char str[100] = "Hello"; That will reserve 100 bytes of memory, populating the first 6 elements with Hello\\0 .

Why did you decide that these statements

char str[ ] = "Hello" ;
strcat ( str, '!' ) ;

do not work?:)

All depends on how the function strcat is defined.

For example it can be defined the following way

char * strcat( char *s, char c )
{
    size_t n = std::strlen( s );
    if ( n ) s[n-1] = c;

    return s;
}

Or the following way

char * strcat( char *s, char c )
{
    size_t n = std::strlen( s );
    char *t = new char[n + 2];

    std::strcpy( t, s );

    t[ n - 2 ] = c;
    t[ n - 1 ] = '\0';

    return t;
}

If strcat in your example is the standard C function std::strcat then again it does not mean that the statements will not work. It means that the program with such statements will not compile because the second argument has a wrong type.

But if you specify a correct value for the second argument like this

strcat ( str, "!" ) ;

that is using a string literal instead of the character literal then indeed the statements will not work as you are expecting because array str does not have enough space to append string literal "!" .

The array should be defined at least like

char str[7] = "Hello" ;
        ^^^

and the function should be called like

strcat ( str, "!" ) ;
              ^^^

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM