c语言:更改字符串数组的内容 [英] C Language: Change the contents of a string array
本文介绍了c语言:更改字符串数组的内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我无法改变一个变量保存一个字符串的内容。我可能这种思维文生义相对于一个int,而不是一个数组。也许必须先冲洗阵列?
不胜感谢。
//最大规模预计申报+1(用于终止0)
焦炭myString1 [20] =你好; //声明并分配一个行 - 确定
myString1 [20] =你好长; //改变内容 - 失败
myString1 [] =你好长; //改变内容 - 失败
myString1 =你好长; //改变内容 - 失败
解决方案
这是C,而不是面向对象的语言,需要复制的字符串为你的照顾。你需要使用字符串
库。例如:
字符myString1 [20] =你好;
函数strncpy(myString1,你好更长,20);
I'm having trouble changing the contents of a variable holding a string. I'm probably thinking of this too literally compared to an int and not as an array. Maybe have to flush array first? Much thanks.
// declare with maximum size expected +1 (for terminator 0)
char myString1[20] = "Hello"; //declare and assign one line - OK
myString1[20] = "Hello Longer"; // change contents - fails
myString1[] = "Hello Longer"; // change contents - fails
myString1 = "Hello Longer"; // change contents - fails
解决方案
This is C, not an object oriented language that takes care of copying strings for you. You'll need to use the string
library. For example:
char myString1[20] = "Hello";
strncpy(myString1, "Hello Longer", 20);
这篇关于c语言:更改字符串数组的内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文