[英]Creating an integer 2d array (as compared to a string 2d array) without mentioning the dimension
I was trying to create a 2d array
without mentioning the dimensions like as follows: 我试图创建一个
2d array
而没有提及如下尺寸:
int m1[][] = {{1,2}, {3,4}};
I got the following error when compiled: 编译时出现以下错误:
error: array type has incomplete element type
Is it not possible to create a 2d array
on the stack
(as opposed to dynamic memory allocation on heap
) without mentioning the row
and column
? 不提及
row
和column
,就不可能在stack
上创建2d array
(与heap
动态内存分配相反)吗?
If compiler can't determine the dimension for an integer 2d array, how does it determines the space requirement for string 2d array. 如果编译器无法确定整数2d数组的维数,它将如何确定字符串2d数组的空间要求。 For example,
例如,
char *keywords[] = {"auto", "static", "extern", "volatile"};
正确,b / c编译器将a)无法确定要分配多少空间,b)无法确定给定元素在数组中的位置。
You can ommit the outer dimension, but not the inner. 您可以省略外部尺寸,但不能省略内部尺寸。 So this is okay
所以没关系
int m1[][2] = {{1,2}, {3,4}};
To your second question: char *keywords[]
is NOT a 2d array! 第二个问题:
char *keywords[]
不是二维数组! It is an array of pointers. 它是一个指针数组。 Pointers are not arrays;
指针不是数组。 Arrays are not pointers!
数组不是指针! (It's only that arrays decay into pointers to the first element of an array, if used as an rvalue).
(只有当数组用作右值时,它才会衰减为指向数组第一个元素的指针)。
Update: To actually answer your question: The strings will typically be statically "allocated" in readonly storage (for example directly written in object files/your program). 更新:实际回答您的问题:字符串通常会在只读存储中静态“分配”(例如,直接写在目标文件/程序中)。 So it's also wrong to declare your array as
char *[]
- it should be a const char *[]
. 因此,将数组声明为
char *[]
也是错误的-它应该是const char *[]
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.