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
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