串联使用C字符数组 [英] concatenate char array in C

查看:139
本文介绍了串联使用C字符数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个字符数组:

char* name = "hello";

我要添加扩展到该名称,使其

I want to add an extension to that name to make it

hello.txt

我怎样才能做到这一点?

How can I do this?

名称+ =.TXT将无法正常工作

推荐答案

有一个看 strcat的功能。

在特别的,你可以试试这个:

In particular, you could try this:

const char* name = "hello";
const char* extension = ".txt";

char* name_with_extension;
name_with_extension = malloc(strlen(name)+1+4); /* make space for the new string (should check the return value ...) */
strcpy(name_with_extension, name); /* copy name into the new var */
strcat(name_with_extension, extension); /* add the extension */

这篇关于串联使用C字符数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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