char * str = {"foo",...}和char str [] [5] = {"foo",...}数组定义之间有什么区别? [英] What is the difference between char*str={"foo",...} and char str[][5]={"foo",...} array definitions?

查看:116
本文介绍了char * str = {"foo",...}和char str [] [5] = {"foo",...}数组定义之间有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

案例1:我写的时候

char*str={"what","is","this"};

然后str[i]="newstring";有效,而str[i][j]='j';无效.

案例2:我写的时候

char str[][5]={"what","is","this"};

然后str[i]="newstring";无效,而str[i][j]='J';有效.

为什么会这样?我是一个初学者,在阅读了其他答案后已经很困惑.

Why is it so? I am a beginner who already get very confused after reading the other answers.

推荐答案

首先:建议:请阅读有关

First of all: A suggestion: Please read about arrays are not pointers and vice-versa!!

也就是说,要启发这种特殊情况,

That said, to enlighten this particular scenario,

  • 第一种情况

char*str={"what","is","this"};

不执行您认为的操作.这是一个违反约束的行为,需要根据第6.7.9/P2章的要求从任何符合C的实现中进行诊断:

does not do what you think it does. It is a constraint violation, requiring a diagnostic from any conforming C implementation, as per chapter§6.7.9/P2:

任何初始值设定项都不应尝试为实体中未包含的对象提供值 被初始化.

No initializer shall attempt to provide a value for an object not contained within the entity being initialized.

如果启用警告,则(至少 )请参见

If you enable warnings, you'd (at least) see

警告:标量初始化程序中的多余元素

warning: excess elements in scalar initializer

  char*str={"what","is","this"};

但是,打开了严格符合性的(ny)编译器应拒绝编译代码.以防万一,编译器仍然选择编译并生成二进制文件,其行为不符合C语言定义的范围,这取决于编译器的实现(因此,可以有很大的不同).

However, a(ny) compiler with strict conformance turned on, should refuse to compile the code. In case, the compiler chose to compile and produce a binary anyway, the behavior is not withing the scope of definition of C language, it's up to the compiler implementation (and thus, can vary widely).

在这种情况下,编译器决定此语句在功能上仅与char*str= "what";

In this case, compiler decided this statement to make functionally only same as char*str= "what";

因此,这里str是指向char的指针,该指针指向字符串文字. 您可以重新分配指针

So, here str is a pointer to a char, which points to a string literal. You can re-assign to the pointer,

str="newstring";  //this is valid

但是,像这样的语句

 str[i]="newstring";

将是无效,因为此处试图将指针类型转换并存储为与类型不兼容的char类型.在这种情况下,编译器应发出有关无效转换的警告.

would be invalid, as here, a pointer type is attempted to be converted and stored into a char type, where the types are not compatible. The compiler should throw a warning about the invalid conversion in this case.

此后,类似

str[i][j]='J'; // compiler error

在语法上是无效的,因为您在不是指向完整对象类型的指针"的对象上使用Array下标[]运算符,例如

is syntactically invalid, as you're using the Array subscripting [] operator on something which is not "pointer to complete object type", like

str[i][j] = ...
      ^^^------------------- cannot use this
^^^^^^ --------------------- str[i] is of type 'char', 
                             not a pointer to be used as the operand for [] operator.

  • 另一方面,在第二种情况下

    str是一个数组数组.您可以更改单个数组元素,

    str is an array of arrays. You can change individual array elements,

     str[i][j]='J'; // change individual element, good to go.
    

    但是您不能分配给数组.

    but you cannot assign to an array.

     str[i]="newstring";  // nopes, array type is not an lvalue!!
    

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