指定字符指针为char和char数组变量 [英] assigning char pointer to char and char array variable
本文介绍了指定字符指针为char和char数组变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
为什么以下好吗?
char *a;
char b[]="asdf";
a=b;
但下面是不是?
char a[10];
char b[]="asdf";
a=b;
以上给出的错误:不兼容的类型分配
The above gives error: incompatible types in assignment.
推荐答案
当你说
char a[10];
一实际上是
char * const a = malloc(sizeof(char) * 10); // remember to free it, can use alloca() instead
和'一'被初始化为指向10 * sizeof的(char)的分配内存。
and 'a' is initialized to point to 10 * sizeof(char) of allocated memory.
所以
a[1] = 't';
*(a + 1) = 't';
是允许的。
但
are allowed. But
char *b = "some string";
a = b;
是不允许的。
这篇关于指定字符指针为char和char数组变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文