struct a
{
int i;
char a[1];
};
这样定义有什么意义吗?
14 个解决方案
#1
呵。。。。
#2
你在哪儿遇到的?
贴出来看看
贴出来看看
#3
定义一个结构体贝
#4
我觉得该设计有它适用的地方
char a[1]--a 可表是地址
char a ---&a 才表示地址
char a[1]--a 可表是地址
char a ---&a 才表示地址
#5
这个一半时表示一个可变长的结构,如果你,因为最后一个是一个数组,当你分配一个大于结构大小的内存时,最后多余的空间都可以用char a[]引用,相当常用的技巧。
#6
这种情况用于结构长度变化而又不想保存指针的情况
使用这个结构可能会写出这样的话:
struct a b;
strcpy(b.a,"hello world!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
使用这个结构可能会写出这样的话:
struct a b;
strcpy(b.a,"hello world!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
#7
这样可以当字符串用.
贴出来看看
贴出来看看
#8
楼上这样恐怕不行吧,越界访问了
#9
我也认为会越界访问
有谁去测试一下
有谁去测试一下
#10
struct a* b = new char[sizeof(struct a) + 1024];
strcpy(b->a,"hello world!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
这还差不多
strcpy(b->a,"hello world!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
这还差不多
#11
大大的不同了。
同意楼上
同意楼上
#12
C也有,可变长结构,GCC编译器支持char a[0]这样的语法,看这段stallman的解释:
Arrays of Length Zero
Zero-length arrays are allowed in GNU C. They are very useful as the last element of a structure which is really a header for a variable-length object:
struct line {
int length;
char contents[0];
};
{
struct line *thisline = (struct line *)
malloc (sizeof (struct line) + this_length);
thisline->length = this_length;
}
In standard C, you would have to give contents a length of 1, which means either you waste space or complicate the argument to malloc.
Arrays of Length Zero
Zero-length arrays are allowed in GNU C. They are very useful as the last element of a structure which is really a header for a variable-length object:
struct line {
int length;
char contents[0];
};
{
struct line *thisline = (struct line *)
malloc (sizeof (struct line) + this_length);
thisline->length = this_length;
}
In standard C, you would have to give contents a length of 1, which means either you waste space or complicate the argument to malloc.
#13
长知识
#14
有意思
#1
呵。。。。
#2
你在哪儿遇到的?
贴出来看看
贴出来看看
#3
定义一个结构体贝
#4
我觉得该设计有它适用的地方
char a[1]--a 可表是地址
char a ---&a 才表示地址
char a[1]--a 可表是地址
char a ---&a 才表示地址
#5
这个一半时表示一个可变长的结构,如果你,因为最后一个是一个数组,当你分配一个大于结构大小的内存时,最后多余的空间都可以用char a[]引用,相当常用的技巧。
#6
这种情况用于结构长度变化而又不想保存指针的情况
使用这个结构可能会写出这样的话:
struct a b;
strcpy(b.a,"hello world!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
使用这个结构可能会写出这样的话:
struct a b;
strcpy(b.a,"hello world!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
#7
这样可以当字符串用.
贴出来看看
贴出来看看
#8
楼上这样恐怕不行吧,越界访问了
#9
我也认为会越界访问
有谁去测试一下
有谁去测试一下
#10
struct a* b = new char[sizeof(struct a) + 1024];
strcpy(b->a,"hello world!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
这还差不多
strcpy(b->a,"hello world!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
这还差不多
#11
大大的不同了。
同意楼上
同意楼上
#12
C也有,可变长结构,GCC编译器支持char a[0]这样的语法,看这段stallman的解释:
Arrays of Length Zero
Zero-length arrays are allowed in GNU C. They are very useful as the last element of a structure which is really a header for a variable-length object:
struct line {
int length;
char contents[0];
};
{
struct line *thisline = (struct line *)
malloc (sizeof (struct line) + this_length);
thisline->length = this_length;
}
In standard C, you would have to give contents a length of 1, which means either you waste space or complicate the argument to malloc.
Arrays of Length Zero
Zero-length arrays are allowed in GNU C. They are very useful as the last element of a structure which is really a header for a variable-length object:
struct line {
int length;
char contents[0];
};
{
struct line *thisline = (struct line *)
malloc (sizeof (struct line) + this_length);
thisline->length = this_length;
}
In standard C, you would have to give contents a length of 1, which means either you waste space or complicate the argument to malloc.
#13
长知识
#14
有意思