如何连接指针数组?

编程入门 行业动态 更新时间:2024-10-28 06:30:19
本文介绍了如何连接指针数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

可能重复:结果  串联字符数组用C

如何连接:

char *a="abcd"; char *b="1234";

使用的

的strcat()?当用户输入回答同样的* a和* B?

using the strcat()? And answer to the same when user enters *a and *b?

修改错过了这一点:不使用另一个阵列

EDIT missed out this: without using another array.

推荐答案

您不能与你已经初始化字符串猴子各地 - 他们坐在存储兼地方;不能/不应该被重写。

You can't monkey around with the string literals you've already initialized - they're sitting somewhere in memory & can't/shouldn't be rewritten.

如果你不希望其他静态定义的数组,但不介意动态分配,code以下可以完成你在找什么:

If you don't want another statically-defined array but don't mind dynamic allocation, the code below may accomplish what you're looking for:

char *a = "abcd"; char *b = "1234"; char *out; if((out = (char *)malloc(strlen(a) + strlen(b) + 1)) != NULL) { strcpy(out, a); strcat(out, b); } else { //you don't have enough memory, handle it }

如果这仍然不能接受,可以考虑不同的方法来初始化您的字符串。

If that's still unacceptable, consider a different approach to initializing your string literals.

更多推荐

如何连接指针数组?

本文发布于:2023-07-29 08:34:51,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1239121.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数组   指针

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!