在 C 中连接字符数组 [英] concatenate char array in C

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

问题描述

我有一个字符数组:

char* name = "hello";

我想为该名称添加一个扩展名

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

hello.txt

我该怎么做?

name += ".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天全站免登陆