473,414 Members | 1,928 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,414 software developers and data experts.

Variable Sized Struct Members?

My understanding is that struct members will remain in order such that
the following can be used to support variable sized members:

struct indexed_values {
int *values;
unsigned char bitset[0];
};

struct meta_data {
struct indexed_values foo;
unsigned char _foo_bs[8];
struct indexed_values bar;
unsigned char _bar_bs[16];
struct indexed_values zap;
unsigned char _zap_bs[4];
....
};

In this case accessing meta_data->bar[15] for example cannot overlap
with other members of runover into the zap member.

Is this legitimate C?

Thanks,
Mike
Nov 14 '05 #1
2 1861
Michael B Allen <mb*****@ioplex.com> writes:
struct indexed_values {
int *values;
unsigned char bitset[0];
};


A structure cannot contain an array of zero size.
--
"I hope, some day, to learn to read.
It seems to be even harder than writing."
--Richard Heathfield
Nov 14 '05 #2
Michael B Allen wrote:
My understanding is that struct members will remain in order such that
the following can be used to support variable sized members:

struct indexed_values {
int *values;
unsigned char bitset[0];
};


You can't have length-zero arrays - this is only a popular but
noncompliant extension that is really only useful in this case. See the
ANSI C Rationale section 3.5.4.2:

http://www.lysator.liu.se/c/rat/c5.html#3-5-4-2

More disappointingly, even the method described by the Rationale is
technically noncompliant - see question 2.6 of the FAQ:

http://www.eskimo.com/~scs/C-faq/q2.6.html

--
Derrick Coetzee
I grant this newsgroup posting into the public domain. I disclaim all
express or implied warranty and all liability. I am not a professional.
Nov 14 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Piotr Sawuk | last post by:
Hello, I'm new in this group and new to c++ programming. And I already have my first question which wasn't answered by any text-book on c++ programming I have seen so-far: How can I define a...
10
by: Adam Warner | last post by:
Hi all, With this structure that records the length of an array of pointers as its first member: struct array { ptrdiff_t length; void *ptr; };
42
by: baumann | last post by:
hi all, typedef int (*pfunc)(int , int); pfunc a_func; i know it's ok, but how can define a_func without typedef statement? thanks .
5
by: nmtoan | last post by:
Hi, I could not find any answer to this simple question of mine. Suppose I have to write a program, the main parts of it are as follows: #include <blahblah.h> struct {
4
by: fabio | last post by:
Hello, zero-sized arrays (which I find very useful, since I do a lot of packet-style programming with circular buffers) cannot be declared if they're in a struct/class that later will be inherited...
4
by: Himanshu Singh Chauhan | last post by:
Hello All!! Can anybody tell what variable sized structures are and how can they be used? regards --Himanshu
8
by: redefined.horizons | last post by:
I would like to have an array declaration where the size of the array is dependent on a variable. Something like this: /* Store the desired size of the array in a variable named "array_size". */...
3
by: ramkrishna1 | last post by:
Hi All, Consider the following piece of code: struct foo1 { unsigned char tag; unsigned char length; unsigned char value; };
8
by: Andrew Smallshaw | last post by:
I'm working on a data structure that began life as a skip list derivative, but has evolved to the point that it now only has a passing resemblance to them. Each node of this structure has a few...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.