声明意义 [英] declaration meaning
问题描述
大家好,
char a [10];
将从索引0开始声明大小为10的字符数组。
what是以下声明的含义?什么是
这样的
声明?
char a [0];
谢谢,
Krishna
Hi all,
char a[10];
will declare character array of size 10 starting with the index 0.
what is the meaning of the following declaration? what is the purpose
of such
declarations?
char a[0];
thanks,
Krishna
推荐答案
这是非法的。编译器会抱怨。如果你说例如
char a [1];
它将是一个字符,A将指向它。
指定零大小的数组是非法的。
It''s illegal. Compiler would complain. If you said for example
char a[1];
it would be one character, and the A would be a pointer to it.
Specifying zero sized arrays is illegal.
" krishna" < KR ************ @ gmail.com>写道:
"krishna" <kr************@gmail.com> wrote:
大家好,
char a [10];
将声明从索引0开始的大小为10的字符数组。
什么是以下声明的含义是什么?这种
声明的目的是什么?
char a [0];
Hi all,
char a[10];
will declare character array of size 10 starting with the index 0.
what is the meaning of the following declaration? what is the purpose
of such
declarations?
char a[0];
它们有时被用来提供一个名字对于可变大小的数组,
位于''a''的地址。
例如:
#include < stdlib.h>
struct message
{
int type;
int length ;
char数据[0];
};
typedef struct message message_t;
message_t * create_message(int type,int length)
{
message_t * temp;
temp = malloc(length + sizeof(message_t)) ;
if(temp!= NULL)
{
temp-> type = type;
temp - >长度=长度;
}
返回温度;
}
int main ()
{
message_t * abc;
abc = create_message(10);
返回EXIT_SUCCESS;
}
They are used sometimes to provide a name for a variable size array,
located at the adress of ''a''.
For example:
#include <stdlib.h>
struct message
{
int type;
int length;
char data[0];
};
typedef struct message message_t;
message_t *create_message(int type, int length)
{
message_t *temp;
temp = malloc(length + sizeof(message_t));
if (temp != NULL)
{
temp->type = type;
temp->length = length;
}
return temp;
}
int main()
{
message_t *abc;
abc = create_message(10);
return EXIT_SUCCESS;
}
如果这样,那么它的艺术作品。 :)当然 - 如果要将
声明为变量,则会产生编译错误。但是,
在一个结构中声明它并没有自行分配,我们只是说b $ b获得一个名称,我们后来在引用(m)分配的内存时保留这个名称。
Genialno。 :)所以,它实际上只适用于结构?
If this works, then it''s a work of art. :) Of course - if one was to
declare it as a variable, it would get a compiler error. However,
declaring it in a structure doesn''t do the allocating itself, we just
get a name we later hold for when referring to (m)allocated memory.
Genialno. :) So, it actually works only for structs?
这篇关于声明意义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!