473,395 Members | 1,670 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,395 software developers and data experts.

zero lengh arrays.

hi,
How to use Zero lengh Arrays..
I searched but could not get much information regarding this
where Do i get links abt this

Jun 16 '06 #1
5 1626
$hiv..... schrieb:
How to use Zero lengh Arrays..
I searched but could not get much information regarding this
where Do i get links abt this


Zero length arrays cannot be explicitly declared
in standard C. As they make no sense in all but the
most esoteric contexts, you are better off without
them.

<OT> Your compiler may have an extension to enable
struct {
....
sometype array[0];
}
to simplify applying the so-called "struct hack" or
something similar.
This is off-topic around here, so you need to read
your compiler's documentation or ask in a newsgroup
for your compiler.
You still cannot really "use" the zero length array.
</OT>

Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
Jun 16 '06 #2

$hiv..... wrote:
hi,
How to use Zero lengh Arrays..
I searched but could not get much information regarding this
where Do i get links abt this


example:

struct tag {
int tag_type;
int tag_len;
char tag_data[0];
} ;

int sample_tag_len = 10;
struct tag *sample_tag = (struct tag *)malloc(sizeof(struct
pppoe_tag)+sizeof(char)*sample_tag_len);
sample_tag->tag_type = 0xffff;
sample_tag->tag_len = sample_tag_len;
sample_tag->tag_data[0]=....
....

free(sample_tag);

Jun 16 '06 #3

seni.yin wrote:
$hiv..... wrote:
hi,
How to use Zero lengh Arrays..
I searched but could not get much information regarding this
where Do i get links abt this


example:

struct tag {
int tag_type;
int tag_len;
char tag_data[0];
} ;

int sample_tag_len = 10;
struct tag *sample_tag = (struct tag *)malloc(sizeof(struct
pppoe_tag)+sizeof(char)*sample_tag_len);
sample_tag->tag_type = 0xffff;
sample_tag->tag_len = sample_tag_len;
sample_tag->tag_data[0]=....
...

free(sample_tag);


Would it make any difference if you simply declared tag_data[] to
have size 1 ?

Jun 16 '06 #4
sp****@gmail.com wrote:
seni.yin wrote:
example:

struct tag {
int tag_type;
int tag_len;
char tag_data[0];
} ;

int sample_tag_len = 10;
struct tag *sample_tag = (struct tag *)malloc(sizeof(struct
pppoe_tag)+sizeof(char)*sample_tag_len);
sample_tag->tag_type = 0xffff;
sample_tag->tag_len = sample_tag_len;
sample_tag->tag_data[0]=....
...

free(sample_tag);


Would it make any difference if you simply declared tag_data[] to
have size 1 ?


Yes; the code would then be marginally more conformant. Fix the malloc()
call while you're at it.

For the right way to do this, read the FAQ:
<http://c-faq.com/struct/structhack.html>.

Richard
Jun 16 '06 #5
<sh********@huawei.com> wrote:
How to use Zero lengh Arrays..
...


See this thread:
http://groups.google.com/group/comp....5b1fc37446bd82
Jun 16 '06 #6

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

Similar topics

0
by: Thomas | last post by:
Hi! How can i get the optimal richtext width size, when depending on the variable text lengh. The text can be colored or with some differnt fontsizes. I dont want use scrollbars. Is there a...
4
by: Steven T. Hatton | last post by:
I mistakenly set this to the comp.std.c++ a few days back. I don't believe it passed the moderator's veto - and I did not expect or desire anything different. But the question remains: ISO/IEC...
25
by: prashna | last post by:
Hi all, I have seen a piece of code(while doing code review) which declared an array of size 0.One of my friend told although it is not standard C,some compilers will support this..I am very...
1
by: John Smith | last post by:
I have a two dimentional char array. Before filling it using strtok(), I reset its elements to '\0' using two nested for loops. The code works as I hope it would but I wonder whether I really need...
8
by: pranav.choudhary | last post by:
Is it legal to keep the size of an array 0. gcc 3.4.2 did not give any error for the declaration int a; If the declaration is legal, then what will be the implications of saying a = 10;
22
by: spam.noam | last post by:
Hello, I discovered that I needed a small change to the Python grammar. I would like to hear what you think about it. In two lines: Currently, the expression "x" is a syntax error. I suggest...
37
by: Ajai Jose | last post by:
Hi , I work on an ARM processor based embedded system. Code is mainly in C language. The project has a huge source base. We are supposed to optimise it. Most datastructures are declared as static...
33
by: Zytan | last post by:
I want to make a zero element array. I know that Nothing is not the same as a zero element array, since I can't get the length of, or iterate through, an array = Nothing. I could make a zero...
10
by: tshad | last post by:
Is there a way to zero fill a numeric into a string that is a fixed lengh of 10 chars? In Sql I would do something like: Right("0000000000" + Convert(varchar,amt),10). Is there an easy way to...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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...

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.