some strange for me

P

profjwang

I have a structrue like this:
struct object_t{
struct object *next;
char data[0];
}
and another one:
struct bucket_t{
int a;
struct object objects[0];
}

but the VC compiler tell me have error occurred in struct object
object[0], why??
 
W

Walter Roberson

I have a structrue like this:
struct object_t{
struct object *next;
char data[0];
}
and another one:
struct bucket_t{
int a;
struct object objects[0];
}
but the VC compiler tell me have error occurred in struct object
object[0], why??

Direct constraint violation.


C89 3.5.4.2 Array Declarators

Constraints
The expression delimited by [ and ] (which specifies the size of
an array) shall be an integral constant expression that has a
value greater than zero.
 
M

Mark McIntyre

I have a structrue like this:
struct object_t{
struct object *next;
char data[0];
}
and another one:
struct bucket_t{
int a;
struct object objects[0];
}

but the VC compiler tell me have error occurred in struct object
object[0], why??

there is no definition of "struct object".
 
K

Keith Thompson

I have a structrue like this:
struct object_t{
struct object *next;
char data[0];
}
and another one:
struct bucket_t{
int a;
struct object objects[0];
}

but the VC compiler tell me have error occurred in struct object
object[0], why??

I see at least two possible problems in the code you posted. One is
that you declare "struct object_t" but refer to "struct object".
Another is that you attempt to declare zero-sized arrays, which is not
allowed in C.

I suspect the code you posted is not the actual code you're having
problems with, which makes it difficult to tell what your real problem
might be. In addition, you tell us that your compiler tells you an
error occurred, but you don't show us the actual error message.

If you copy-and-paste (don't re-type) the *exact* code and the *exact*
error message, we can be a lot more helpful.

See <http://www.catb.org/~esr/faqs/smart-questions.html>.
 
S

santosh

I have a structrue like this:
struct object_t{
struct object *next;
char data[0];
}
and another one:
struct bucket_t{
int a;
struct object objects[0];
}

but the VC compiler tell me have error occurred in struct object
object[0], why??

You have several typos there. Anyway, array declarations must consist of
at least one element, and it must be a constant expression known at
compile time. C99 has variable length arrays, but I don't know them
enough to talk much about them. In any case, a possible alternative is:

struct bucket_t {
int a;
struct object_t *objects;
};

This declares a pointer to struct object_t which you can initialise at
runtime to point to one or more instances of object_t, through malloc.

Static allocation is easy but not very flexible. Dynamic allocation
demands more work and attention, and may involve a very small runtime
cost, but is the most flexible scheme.
 
J

jameskuyper

Walter said:
I have a structrue like this:
struct object_t{
struct object *next;
char data[0];
}
and another one:
struct bucket_t{
int a;
struct object objects[0];
}
but the VC compiler tell me have error occurred in struct object
object[0], why??

Direct constraint violation.


C89 3.5.4.2 Array Declarators

Constraints
The expression delimited by [ and ] (which specifies the size of
an array) shall be an integral constant expression that has a
value greater than zero.

True. Note, however, that some popular compilers have allowed a value
of 0 as an extension, to implement the "struct hack" for dynamically
allocating structures containing arrays of unspecified length. For
compilers without that extension, a length of 1 would often work, even
though it was not clear that the C89 standard allowed this. This
concept has been officially adopted in C99 as a "flexible array"
member, but the syntax is different: instead of using a length of
either 0 or 1, the length is left out entirely.
 
C

CBFalconer

I have a structrue like this:
struct object_t{
struct object *next;
char data[0];
}
and another one:
struct bucket_t{
int a;
struct object objects[0];
}

but the VC compiler tell me have error occurred in struct object
object[0], why??

0 length arrays are forbidden in C.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,043
Latest member
CannalabsCBDReview

Latest Threads

Top