jdi*_*zle 21 c arrays pointers
编辑:显然其中一些不允许/已在各种C标准中发生变化.对于我自己的假设好处,让我们假装我们使用gcc test.c没有标准或警告选项.
特别是我正在研究引擎盖下的细节.我加入了目前的理解.我对吗?
char **c1; //Size for a pointer is allocated on the stack. sizeof(c1) == sizeof(void*)
char *c2[0]; //Nothing is allocated on the stack. sizeof(c2) == 0
Run Code Online (Sandbox Code Playgroud)
这两个我不知道的情况(除了sizeof)还有其他的区别吗?
struct a {
int i;
char c[0]; //sizeof(a) is sizeof(int)? a.c == (&i)+1?
};
Run Code Online (Sandbox Code Playgroud)
据我了解,这通常用于结构末端的可变长度数组.但是关于
struct b {
char *c[0] //sizeof(b) is 0? where does c point?
};
int j;
struct b myb; //myb.c == (&j)+1 == $esp?
Run Code Online (Sandbox Code Playgroud)
如果零指针的地址从未在任何地方分配,那么零长度数组的地址如何知道呢?我认为常规数组的地址已知的方式相同,但我现在正在努力绕过它.
And*_*ett 12
我唯一一次看到实际使用的零长度数组就是你想要一个可变长度的结构.
如本例取自这里
struct line {
int length;
char contents[0];
};
struct line *thisline = (struct line *)
malloc (sizeof (struct line) + this_length);
thisline->length = this_length;
Run Code Online (Sandbox Code Playgroud)
您不希望在上面的结构中使用指针,因为您希望内容成为结构的已分配内存的一部分.
如果在上面的结构上执行sizeof,则不包含任何内容空间.我不确定这是否已成为标准,它会在各种编译器上发出警告.
ISO C禁止0长度数组.
char **c1;
Run Code Online (Sandbox Code Playgroud)
这定义了一个c1指向指向char的指针类型的对象.
char *c2[0];
Run Code Online (Sandbox Code Playgroud)
这是一个编译错误.不允许.不是C语言,不是C++语言.
struct a {
int i;
char c[0]; //sizeof(a) is sizeof(int)? a.c == (&i)+1?
};
Run Code Online (Sandbox Code Playgroud)
如前所述的错误 - 数组的大小必须大于零.
struct a {
int i;
char c[1];
};
Run Code Online (Sandbox Code Playgroud)
也称为struct-hack.几乎所有操作系统都使用低级代码 - Linux,Windows.
C99确实为我们提供了一个无量纲阵列,更好地称为灵活阵列成员:
struct a {
int i;
char c[]; /* note: no size */
};
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
8768 次 |
| 最近记录: |