在C,为什么我不能指定一个字符串转换为字符数组它的声明之后? [英] In C, why can't I assign a string to a char array after it's declared?

查看:158
本文介绍了在C,为什么我不能指定一个字符串转换为字符数组它的声明之后?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这已被窃听我一会儿。

struct person {
       char name[15];
       int age;
};
struct person me;
me.name = "nikol";

当我编译我得到这个错误:

when I compile I get this error:

错误:不兼容的类型分配给键入时'的char [15]'从类型'字符*

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

我失去了一些东西明显在这里?

am I missing something obvious here?

推荐答案

数组在C中的二等公民,他们不支持的任务。

Arrays are second-class citizens in C, they do not support assignment.

char x[] = "This is initialization, not assignment, thus ok.";

这不工作:

x = "Compilation-error here, tried to assign to an array.";

使用库函数或手动每个元素复制本身:

Use library-functions or manually copy every element for itself:

#include <string.h>
strcpy(x, "The library-solution to string-assignment.");

这篇关于在C,为什么我不能指定一个字符串转换为字符数组它的声明之后?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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