[Haifux] Intermingled code and declarations and gcc
Eli Billauer
eli at billauer.co.il
Thu Sep 17 15:49:38 MSD 2009
Hi,
Yes, it's C. The problem occurred when going through the standard
configure/make procedure for gphoto2-2.3.1, and I'm positive that the
attempt to compile was with gcc (and it blew on this problem). The file
is gp-params.c. The date on the file is December 2006 (so it should work
with an old compiler).
Obviously, some people managed to compile this. I wonder what their
magic was.
The problem is *not* that the size of the declared array depends on a
runtime variable. This compiles and runs (using uninitialized memory,
but who cares):
#include <stdio.h>
int main(int argc, char *argv[]) {
char my_array[argc];
printf("%d", my_array[0]);
return 0;
}
This doesn't compile at all:
#include <stdio.h>
int main(int argc, char *argv[]) {
unsigned int my_count;
for (my_count=0; argv[my_count] != NULL; my_count++);
char my_array[my_count];
printf("%d", my_array[0]);
return 0;
}
Ideas? I suppose this issue arises every now and then when compiling
code written by wild programmers.
Eli
Orna Agmon Ben-Yehuda wrote:
> Hi Eli,
>
> Is this C? You are defining an array with an unknown length at compile
> time. It won't work anyhow, no matter what the value of the length var
> is. You need a malloc there.
>
> Unless this is C++ with some overriding?
>
> Orna.
>
>
--
Web: http://www.billauer.co.il
More information about the Haifux
mailing list