指定字符串之间的区别 [英] Difference between assigning strings

查看:106
本文介绍了指定字符串之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C我试图在两种不同的方式分配刺痛变量

In C I tried to assign a sting to variable in two different ways

char question[200];
strcpy( question, "This is the question" );

char question[] = "This is the question";

和这两个作品...
但什么是这两种方法之间的区别?

and both works... but what's the difference between these two methods?

推荐答案

,这种差异可能的灵活性。这

The difference is probably flexibility. This

strcpy( question, "This is the question" );

在声明变量之后,你可以做任何时候。
而这样的:

you can do anytime after you declare the variable. Whereas this:

char question[] = "This is the question";

您必须在申报时直接使用。

You have to use directly during declaration time.

在第二
例如问题的长度得到固定长度
对于空终止文本+1。你不能改变
这个变量的长度后,所以你不能分配较大
字符串,它的例子。

In the second example the length of question got fixed to length of text +1 for null terminator. You can't change length of this variable later, so you can't assign larger string to it for example.

这篇关于指定字符串之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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