简体   繁体   English

为什么以下语句不起作用?

[英]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. 今天我进行了测试,其中写下了以下问题,并且由于我是C ++的新手,所以我对以下问题感到困惑。

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. strcat第二个参数必须是指向字符串的指针,但是您正在传递字符常量。

The correct call would be strcat(str, "!"); 正确的调用应该是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"; (请注意, "而不是“ ' ),但您还需要在str保留足够大的空间,该空间仅足以容纳"Hello"字符串。例如,对于您的测试,可以使用char str[64] = "Hello";保留更多字节char str[64] = "Hello";

strcat() calls for pointer for both arguments. strcat()调用两个参数的指针。

'!' will converted to an (invalid for many chance) pointer by implementation-defined manner, then the program may crash for Segmentation Fault. 将以实现定义的方式转换为(很多机会无效)指针,然后程序可能因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 : strcat的签名是:

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

so, second parameter is of type const char* and not char . 因此,第二个参数的类型为const char*而不是char You must pass either string literal or variable of type const char* . 您必须传递字符串文字或const char*类型的变量。 Actually string literals are of type const char[] but they decay to const char* when being assigned to. 实际上,字符串文字的类型为const char[]但在分配给它们时会衰减为const char*

strcat function expects two strings. strcat函数需要两个字符串。 '!' 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. ,并且str[]str[8]或更多。

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* . 直接的问题是第二个参数必须是const char* strcat reads the memory starting at that location, until the \\0 is reached. strcat从该位置开始读取内存,直到达到\\0 If no \\0 can be reached then the function behaviour is undefined. 如果无法达到\\0则功能行为未定义。

  2. It's up to you to make sure that str is large enough to receive the concatenated string. 您需要确保str足够大以接收串联的字符串。 If not, then the behaviour is undefined. 如果不是,则行为是不确定的。

On the second point, you can write something like char str[100] = "Hello"; 在第二点上,您可以编写类似char str[100] = "Hello"; That will reserve 100 bytes of memory, populating the first 6 elements with Hello\\0 . 这将保留100个字节的内存,并用Hello\\0填充前6个元素。

Why did you decide that these statements 您为什么决定这些陈述

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

do not work?:) 不工作?:)

All depends on how the function strcat is defined. 一切都取决于函数strcat的定义方式。

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. 如果您的示例中的strcat是标准的C函数std::strcat那么这并不意味着该语句将不起作用。 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 "!" 那是使用字符串文字而不是字符文字,那么实际上这些语句将无法正常工作,因为数组str没有足够的空间来附加字符串文字"!" .

The array should be defined at least like 数组至少应定义为

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

and the function should be called like 该函数应该像

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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