的char *和char []用C之间的差异 [英] Difference between char* and char[] in C

查看:170
本文介绍了的char *和char []用C之间的差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


  

可能重复:结果
  Ç - 之间与ldquo差; VAR的char []”的和“字符* VAR和rdquo ;?


我已经写了下面的C code

 #包括LT&;&stdio.h中GT;
诠释的main()
{
    焦炭名[31];
    字符*温度;
    INT I;
    scanf函数(%S,名);
    TEMP =名称;
    名称=温度;}

我编译时有以下错误

分配给输入时

 不兼容的类型'的char [31]'从类型'字符*'

数组名是一个指向一个元素(这里字符指针..right?)。不是吗?以上code意味着字符数组和char *是不同的类型..是真的吗?
为什么类型的名称!=字符*
为什么我不能在另一个字符指针赋值给一个字符指针(数组名)


解决方案

  

数组名是一个指向一个元素(这里字符指针..right?)。对吧?


 字符名称[31];
字符*温度;
/ * ... * /
名称=温度;

名称=温度任务, name的值转换为指针字符。该值被转换,不可以的对象。对象仍然是一个数组和数组是不可修改的左值。由于分配操作数的约束要求赋值运算符的左操作数是一个修改的左值,你得到了一个错误。

Possible Duplicate:
C - Difference between “char var[]” and “char *var”?

I have written the following C code

#include<stdio.h>
int main()
{
    char name[31];
    char *temp;
    int i ;
    scanf("%s",name);
    temp  = name;
    name = temp;

}

I got the following error when compiling

incompatible types when assigning to type 'char[31]' from type 'char *'

Array name is a pointer to first element(here char pointer ..right?). right?The above code means that character array and char* are different types ..Is it true? Why type of name != char * why i cannot assign another char pointer to a char pointer(array name)

解决方案

"Array name is a pointer to first element(here char pointer ..right?). right?"

char name[31];
char *temp;
/* ... */
name = temp;

In the name = temp assignment, the value of name is converted to a pointer to char. The value is converted, not the object. The object is still an array and arrays are not modifiable lvalues. As the constraints of the assignment operand require the left operand of the assignment operator to be a modifiable lvalue, you got an error.

这篇关于的char *和char []用C之间的差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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