473,671 Members | 2,588 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

array of size 0

Hi,

I know that C90 and C9x (N869) doesn't allow to define an array of size 0.

int x[0]; /* constraint violation */

I was under the impression that this was possible in C99, due to the common
C90 practice:

struct a
{
int x;
char y[1];
};

that allows to build a linear structure with an array bigger than one,

struct a
{
int x;
char y[0];
};

char x[123];
struct a *p = (struct a*) x;

or is it just an extension of gcc?

Thanks to clarify my close-to-melt brain.
(we have 40°C in Paris, hopefully, I go to Ireland for holydays next Sunday)

--
-ed- em**********@no os.fr [remove YOURBRA before answering me]
The C-language FAQ: http://www.eskimo.com/~scs/C-faq/top.html
<blank line>
FAQ de f.c.l.c : http://www.isty-info.uvsq.fr/~rumeau/fclc/
Nov 13 '05 #1
5 8796
Emmanuel Delahaye <em**********@n oos.fr> wrote:
Hi,

I know that C90 and C9x (N869) doesn't allow to define an array of size 0.

int x[0]; /* constraint violation */

I was under the impression that this was possible in C99, due to the common
C90 practice:

struct a
{
int x;
char y[1];
};

that allows to build a linear structure with an array bigger than one,

struct a
{
int x;
char y[0];
};
No, the C99 flexible array member looks like:

struct a
{
int x;
char y[];
};
char x[123];
struct a *p = (struct a*) x;
You can't do that, x isn't necessarily correctly aligned for access as
"struct a".

struct a *p = malloc(123);
Thanks to clarify my close-to-melt brain.
(we have 40?C in Paris, hopefully, I go to Ireland for holydays next Sunday)


Bah, we get 40C here every summer :)

- Kevin.

Nov 13 '05 #2
In 'comp.lang.c', Kevin Easton <kevin@-nospam-pcug.org.au> wrote:
(we have 40?C in Paris, hopefully, I go to Ireland for holydays next
Sunday)


Bah, we get 40C here every summer :)


With air conditionning, I guess. No such a thing here! Trust me, it's hell!

--
-ed- em**********@no os.fr [remove YOURBRA before answering me]
The C-language FAQ: http://www.eskimo.com/~scs/C-faq/top.html
<blank line>
FAQ de f.c.l.c : http://www.isty-info.uvsq.fr/~rumeau/fclc/
Nov 13 '05 #3
Emmanuel Delahaye <em**********@n oos.fr> wrote in
news:Xn******** *************** ****@130.133.1. 4:
(we have 40?C in Paris, hopefully, I go to Ireland for holydays next
Sunday)


Bah, we get 40C here every summer :)


With air conditionning, I guess. No such a thing here! Trust me, it's
hell!


Good grief, how can you not have A/C? Do you have running water? :-) BTW,
what's the dew point? That's what really makes you uncomfortable.

--
- Mark ->
--
Nov 13 '05 #4
In <Xn************ *************** @130.133.1.4> Emmanuel Delahaye <em**********@n oos.fr> writes:
struct a
{
int x;
char y[0];
};

char x[123];
struct a *p = (struct a*) x;
Remember what we keep telling the beginners? Casts are evil in C!
If you don't know what you're doing when casting a pointer, don't do it!
Yeah, all this applies to you, too! If the x array is not properly
aligned for a struct a access, the behaviour of the cast is undefined!
or is it just an extension of gcc?


Why not ask gcc itself?

fangorn:~/tmp 2321> cat test.c
struct a
{
int x;
char y[0];
};
fangorn:~/tmp 2322> gcc -c -std=c99 -pedantic test.c
test.c:4: warning: ISO C forbids zero-size array `y'

To make the struct definition correct for C99, drop the 0 (declare y as a
sizeless array). This is a "flexible array member" in the C99 lingo.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 13 '05 #5
In <Xn************ *************** *******@130.133 .1.4> "Mark A. Odell" <no****@embedde dfw.com> writes:
Emmanuel Delahaye <em**********@n oos.fr> wrote in
news:Xn******* *************** *****@130.133.1 .4:
(we have 40?C in Paris, hopefully, I go to Ireland for holydays next
Sunday)

Bah, we get 40C here every summer :)


With air conditionning, I guess. No such a thing here! Trust me, it's
hell!


Good grief, how can you not have A/C?


Only the servers and the tape robot in the machine room are considered
worthy of it ;-(

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 13 '05 #6

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

Similar topics

13
34369
by: Noah Spitzer-Williams | last post by:
Hello guys, I would like to do something seemingly simple: find out if an element in an array that is passed to my function exists. I used to think I could just do: if (arr) ... However, if this element's value happens to be 0, the conditional will return false, even though the element actually exists. Any ideas? Can you check if the element == null?
58
10125
by: jr | last post by:
Sorry for this very dumb question, but I've clearly got a long way to go! Can someone please help me pass an array into a function. Here's a starting point. void TheMainFunc() { // Body of code... TCHAR myArray; DoStuff(myArray);
3
2474
by: ritchie | last post by:
Hi all! Still working on this program! Just to recap, I am writing a program to sort an array with four different sort algorythms. I am having a little trouble at the moment though! Now, I am trying to calculate, with each sort, how many times during the sort the array elements are compared and swapped.
8
4605
by: Gerald | last post by:
I have a problem with an array of pointers. In a program I'm writing, I have to read a file, containing thousands of short lines. The content of another file will be compared against each line later on. Before that happens there has been a problem that causes a crash of the program. This is a little program "test.exe" I wrote to test what happens. compiler: mingw 3.1.01 for win32 command line
6
4140
by: Herrcho | last post by:
in K&R Chapter 6.3 it mentions two methods to calculate NKEYS. and points out the first one which is to terminate the list of initializers with a null pointer, then loop along keytab until the end is found is less efficient than using sizeof operator , since size of the array is completely determined at compile time. i don't quite understand this. Could anyone explain to me in detail ?
22
2450
by: Wynand Winterbach | last post by:
I think every C programmer can relate to the frustrations that malloc allocated arrays bring. In particular, I've always found the fact that the size of an array must be stored separately to be a nightmare. There are of course many solutions, but they all end up forcing you to abandon the array syntax in favour of macros or functions. Now I have two questions - one is historical, and the other practical. 1.) Surely malloc (and...
7
7405
by: simkn | last post by:
Hello, I'm writing a function that updates an array. That is, given an array, change each element. The trick is this: I can't change any elements until I've processed the entire array. For example, the manner in which I update element 1 depends on several other (randomly numbered) elements in the array. So, I can't change an element until I've figured out how every element changes.
12
112090
by: manochavishal | last post by:
Hi, I have a question. How can i know the size of array when it is passed to a function. For Example i have this code: #include <stdio.h> #include <stdlib.h>
20
2311
by: Cyn | last post by:
Hi, I want to create a general array structure which can hold all types. Something like this: struct ARRAY { void **array; size_t size; };
28
4562
Nepomuk
by: Nepomuk | last post by:
Hi! I've read, that in C++ there's no predefined method, to calculate the size of an array. However, it's supposed to work withsizeof(array)/sizeof(array)Now, this does work in some situations, but not in others. Here's what I mean:#include <iostream> int length(int * array){return sizeof(array)/sizeof(array);} int main() { int array1 = {3,2,1,0}; std::cout << "Length of array1: " << sizeof(array1)/sizeof(array1) << "\n" <<...
0
8403
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8930
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
7446
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6238
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5704
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4417
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2819
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2062
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1816
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.