Array with no elements

P

paragk

Hi,

What does the C standard say about arrays with no elements?

Code:


#include <stdio.h>
#include <string.h>

int arr_test[]={
#ifdef FEATURE_FOO
0,
#endif
#ifdef FEATURE_BAR
0,
#endif
};

int main()
{
printf("output %d\n", sizeof(arr_test)/sizeof(arr_test[0]));
return 0;
}

It is not known apriori if FEATURE_FOO or FEATURE_BAR are defined or
not.

GCC and .NET compilers give different results.

GCC compiles fine.

..NET gives the following error.

E:\temp>cl test_empty_array.c
Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 15.00.21022.08
for 80x86
Copyright (C) Microsoft Corporation. All rights reserved.

test_empty_array.c
test_empty_array.c(11) : error C2059: syntax error : '}'
test_empty_array.c(15) : warning C4034: sizeof returns 0

What does the C standard say about empty arrays? Is the behavior
undefined or implementation specific?

To make the program portable and safe, I can add a "dummy" entry to
the array, but if possible I would prefer not to do that.

Thanks,
Parag
 
I

Ian Collins

Hi,

What does the C standard say about arrays with no elements?

You can't have one.
Code:

#include<stdio.h>
#include<string.h>

int arr_test[]={
#ifdef FEATURE_FOO
0,
#endif
#ifdef FEATURE_BAR
0,
#endif
};

int main()
{
printf("output %d\n", sizeof(arr_test)/sizeof(arr_test[0]));
return 0;
}

It is not known apriori if FEATURE_FOO or FEATURE_BAR are defined or
not.

GCC and .NET compilers give different results.

GCC compiles fine.

Not if you put it in conforming mode:

gcc -ansi -Wall -pedantic /tmp/x.c
/tmp/x.c:4: warning: ISO C forbids empty initializer braces
/tmp/x.c:4: error: zero or negative size array 'arr_test'
 
K

Kenny McCormack

paragk said:
.NET gives the following error.

E:\temp>cl test_empty_array.c
Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 15.00.21022.08
for 80x86
Copyright (C) Microsoft Corporation. All rights reserved.

Are you sure that's .NET?

I don't know, off hand (and I'm sure if I'm wrong, one of the handy CLC
human compilers will be along to tell me otherwise), but I think that
"cl" is simple C/C++ (in the Microsoft world).

I.e., not C#. Yes, I know there is .NET C++, but I think not C.

--
(This discussion group is about C, ...)

Wrong. It is only OCCASIONALLY a discussion group
about C; mostly, like most "discussion" groups, it is
off-topic Rorsharch [sic] revelations of the childhood
traumas of the participants...
 
J

John Bode

What does the C standard say about empty arrays? Is the behavior
undefined or implementation specific?

The language syntax does not allow for an empty initializer list.
Here's the grammar for an initializer:

initializer:
assignment-expression
{ initializer-list }
{ initializer-list , }

initializer-list:
designationopt initializer
initializer-list , designationopt initializer

designation:
designator-list =

designator-list:
designator
designator-list designator

designator:
[ constant-expression ]
. identifier

An assignment-expression cannot result in an empty token, so an
initializer cannot result in the token sequence "{}"; that's a syntax
error.

I *think* gcc may allow an empty initializer as an extension to the
language, but it's not conforming.
 

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

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top