char * str = {"foo",...}和char str [] [5] = {"foo",...}数组定义之间有什么区别? [英] What is the difference between char*str={"foo",...} and char str[][5]={"foo",...} array definitions?
问题描述
案例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!!
-
最后,
在您的第一种情况下,数组具有相同的逻辑.这使
str
成为一个指针数组.因此,数组成员是可分配的,所以in your first case, the same logic for arrays hold. This makes
str
an array of pointers. So, the array members, are assignable, so,str[i]="newstring"; // just overwrites the previous pointer
完全可以.但是,作为数组成员存储的指针是指向 string文字的指针,因此出于上述相同的原因,您调用
is perfectly OK. However, the pointers, which are stored as array members, are pointers to string literal, so for the very same reason mentioned above, you invoke undefined behavior, when you want to modify one of the elements of the memory belonging to the string literal
str[i][j]='j'; //still invalid, as above.
这篇关于char * str = {"foo",...}和char str [] [5] = {"foo",...}数组定义之间有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!