P
pemo
Couple of questions about the following code (playing with variable length
arrays):
#include <stdio.h>
#include <string.h>
void f(int n, char * argv[])
{
again:
{
char c[n++];
// puts(c);
if(strlen(argv[0]) > n + 1)
{
goto again;
}
else
{
strcpy(c, argv[0]);
puts(c);
}
}
return;
}
int main(int argc, char * argv[])
{
f(0, argv);
getchar();
return 0;
}
1. My understanding of vla is that 'c' should be destroyed/recreated each
time I jump to again: This seems to be the case - uncomment the //
puts(c) - with gcc, 'c' indeed contains garbage each time [a bit of a 'risky
test', but worth it I thought].
Anyway, is my understanding correct here, e.g., that the code is working as
the c99 std says it should?
2. If you were to remove the compound statement[ the { before char c[n++];
and the } before the return; ], gcc gives an error:
'syntax error before "char"'
It obviously doesn't like a definition appearing after a label.
That surprised me, and I'm not sure that it's correct, i.e., I haven't yet
found anything in the stds that says this is defined behaviour. Opinions?
arrays):
#include <stdio.h>
#include <string.h>
void f(int n, char * argv[])
{
again:
{
char c[n++];
// puts(c);
if(strlen(argv[0]) > n + 1)
{
goto again;
}
else
{
strcpy(c, argv[0]);
puts(c);
}
}
return;
}
int main(int argc, char * argv[])
{
f(0, argv);
getchar();
return 0;
}
1. My understanding of vla is that 'c' should be destroyed/recreated each
time I jump to again: This seems to be the case - uncomment the //
puts(c) - with gcc, 'c' indeed contains garbage each time [a bit of a 'risky
test', but worth it I thought].
Anyway, is my understanding correct here, e.g., that the code is working as
the c99 std says it should?
2. If you were to remove the compound statement[ the { before char c[n++];
and the } before the return; ], gcc gives an error:
'syntax error before "char"'
It obviously doesn't like a definition appearing after a label.
That surprised me, and I'm not sure that it's correct, i.e., I haven't yet
found anything in the stds that says this is defined behaviour. Opinions?