如何使用strcat()函数? [英] How to use strcat() function?

查看:100
本文介绍了如何使用strcat()函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是C语言的新手.我正在尝试使用strcat函数.

I am very new in C language. I was trying to use strcat function.

#include <stdio.h>
#include <string.h>

int main(int argc, const char *argv[]) {
    char s1[] = "12345";
    char s2[] = "abcde";

    strcat(s1, s2);

    puts(s1);
    puts(s2);
    return 0;
}

这个正常运行,但是

#include <stdio.h>
#include <string.h>

int main(int argc, const char *argv[]) {
    char* s1 = "12345";
    char* s2 = "abcde";

    strcat(s1, s2);

    puts(s1);
    puts(s2);
    return 0;
}

最后一个未能返回结果.为什么两种不同的声明方式在strcat函数中返回不同的结果.预先感谢.

the last one failed to return a result. Why did the two different ways of declaration return different results in the strcat function. Thanks in advance.

推荐答案

在C中,函数strcat不会创建包含串联字符串的新字符数组.如果它有足够的元素来存储新字符,它将第二个字符从第二个字符串追加到第一个字符数组的第一个字符串.否则,该函数将尝试覆盖超出字符数组的内存,这将导致未定义的行为.

In C the function strcat does not create a new character array containing concatenated strings. It appends characters from the second string to the first string of the first character array provided that it has enough elements to store the new characters. Otherwise the function will try to overwrite the memory beyond the character array that results in undefined behavior.

因此,可以在第一个程序中有效使用该函数,如下所示

So a valid use of the function in the first program can look the following way

#include <stdio.h>
#include <string.h>

int main(int argc, const char *argv[]) {
    char s1[11] = "12345";
    char s2[] = "abcde";

    strcat(s1, s2);

    puts(s1);
    puts(s2);
    return 0;
} 

在此程序中,字符数组被声明为具有11个元素.因此,它可以容纳附加的字符串"abcde".

In this program the character array is declared as having 11 elements. Thus it is able to accommodate the appended string "abcde".

在第二个程序中,尝试修改指针s1指向的字符串文字. C和C ++中的字符串文字是不可变的.即使在与C ++字符串文字相反的C语言中,任何更改字符串文字的尝试都将导致未定义的行为.

In the second program there is an attempt to modify the string literal pointed to by the pointer s1. String literals in C and C++ are immutable. Any attempt to change a string literal results in undefined behavior even though in C opposite to C++ string literals have types of non-constant character arrays.

根据C标准(6.4.5字符串文字)

From the C Standard (6.4.5 String literals)

7不确定这些数组是否有区别,前提是它们的数组 元素具有适当的值. 如果程序尝试执行以下操作: 修改这样的数组,行为是不确定的.

7 It is unspecified whether these arrays are distinct provided their elements have the appropriate values. If the program attempts to modify such an array, the behavior is undefined.

因此,在第二个程序中,您再次需要使用具有足够元素的字符数组.例如

So in the second program you again need to use a character array with enough elements. For example

#include <stdio.h>
#include <string.h>

int main(int argc, const char *argv[]) {
    char s1[11] = "12345";
    char* s2 = "abcde";

    strcat(s1, s2);

    puts(s1);
    puts(s2);
    return 0;
}

或者,如果编译器支持可变长度数组(VLA)或动态分配数组,则可以使用它.例如

Or you could use either a Variable Length Array (VLA) if the compiler supports them or dynamically allocate an array. For example

#include <stdio.h>
#include <string.h>

int main(int argc, const char *argv[]) {
    char *s1 = "12345";
    char* s2 = "abcde";
    char s3[strlen( s1 ) + strlen( s2 ) + 1];    

    strcpy( s3, s1 );
    strcat( s3, s2 );

    puts(s1);
    puts(s2);
    puts(s3);

    return 0;
}

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main(int argc, const char *argv[]) {
    char *s1 = "12345";
    char* s2 = "abcde";
    char *s3 = malloc( strlen( s1 ) + strlen( s2 ) + 1 );    

    if ( s3 != NULL )
    {
        strcpy( s3, s1 );
        strcat( s3, s2 );

        puts(s1);
        puts(s2);
        puts(s3);
    }

    free( s3 );

    return 0;
}

这篇关于如何使用strcat()函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆