进行字符串连接时出现运行时错误 [英] Runtime error while doing string concatenation

查看:79
本文介绍了进行字符串连接时出现运行时错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下程序有什么问题?

main( ) 
{ 
 char *str1 = "United" ; 
 char *str2 = "Front" ; 
 char *str3 ; 
 str3 = strcat ( str1, str2 ) ; 
 printf ( "\n%s", str3 ) ; 
} 

我无法编译上述程序,它总是给我运行时错误。我试图连接两个字符串。上面的程序是否有错误?

I am not able to compile the above program and it always give me runtime error. I am trying to concatenate the two string. Is there any error in the above program?

谢谢。

推荐答案

将您的 char * str1 = United 设置为

char str1 [<连接的结果字符串所需的内存>] = United

您需要为目标缓冲区 str1 分配内存。 str3 还将在结果中接收到 str1 的地址。 strcat将不会检查目标缓冲区(str1)中是否有可用空间。程序员必须照顾好它。

You need to allocate memory for the destination buffer which is str1. str3 will also receive address of str1 in the result. 'strcat' will not check for space availability in destination buffer (str1). Programmer has to take care of it.

这篇关于进行字符串连接时出现运行时错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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