在 C 中为字符数组分配一个值 [英] Assigning char array a value in C

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

问题描述

有什么区别:

char fast_car[15]="Bugatti";

char fast_car[15];
fast_car="Bugatti";

因为第二个导致编译错误:

Because the second one results with compile error:

错误:从类型分配给类型‘char[15]’时类型不兼容‘字符*’

error: incompatible types when assigning to type ‘char[15]’ from type ‘char *’

虽然第一个工作正常.将字符串放在数组中与数组初始化不同的位置会很有帮助.

While the first one works fine. Putting a string in array in different place than array initialisation would be helpful.

推荐答案

第一个是初始化,第二个是赋值.由于数组在 C 中是不可修改的值,因此您无法为它们分配新值.

The first is an initialization while the second is an assignment. Since arrays aren't modifiable values in C you can't assign new values to them.

请注意,您可以修改数组内容,只是不能说fast_car = ....所以内容是可以修改的,数组本身不是.

Mind you, you can modify array contents, you just can't say fast_car = .... So the contents are modifiable, the arrays themselves are not.

对这些截然不同的概念使用相同的符号 = 是值得商榷的.

Using the same symbol = for these widely different concepts is of debatable value.

这篇关于在 C 中为字符数组分配一个值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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