字符数组的初始化 [英] Character Array initialization

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

问题描述

我目前打了一下C和试图了解字符串。可有人请解释为什么这是工作:

 字符测试[] =测试;

为什么下面不?

 字符测试[255];
测试=测试;


解决方案

由于这是一个的初始化的:

 字符测试[] =测试;

和这是一个分配的:

 测试=测试;

和您不能分配在C数组(和字符串中的C是刚数组)。

您最好的选择是使用复制字符串的strcpy()(或者是安全的,的 函数strncpy() )。

I'm currently playing a bit with C and trying to understand strings. Can somebody please explain why this is working:

char test[] = "test";

And why the following does not?

char test[255];
test = "test";

解决方案

Because this is an initialization:

char test[] = "test";

and this is an assignment:

test = "test";

and you cannot assign arrays in C (and strings in C are just arrays).

Your best bet is to copy the string using strcpy() (or to be safe, strncpy()).

这篇关于字符数组的初始化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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