Jean-Guillaume Pyraksos said:
Is a label something like a pointer i can store in a variable ?
Not in the standard language. The code you posted uses a gcc extension
(perhaps available on some other implementations) and is properly
addressed in a gnu-specific newsgroup specific to gcc (or those other
implementations). It cannot be used in programs intended to be portable.
#include <stdio.h>
void foo(void *p)
{
printf("foo\n");
goto *p;
}
int main()
{
foo(&&L1); // ???????? Explain...
This is explained in the gcc documentation.
Always check the documentation for your
implementation (along with the C FAQ) before posting.
printf("bar\n"); // should not be executed ?
L1:
printf("quit\n");
return 0;
}
gcc invoked as a compiler for standard C should produce diagnostics like
a.c: In function 'foo':
a.c:6: warning: ISO C forbids 'goto *expr;'
a.c: In function 'main':
a.c:11: warning: taking the address of a label is non-standard
where line 6 is the 'goto *p;'m and line 11 is 'foo(&&L1);'
But even when gcc compiles its default non-standard language, called
GNU-C, the above code is malformed. The gcc documentation contains the
language "You may not use this mechanism to jump to code in a different
function. If you do that, totally unpredictable things will happen. The
best way to avoid this is to store the label address only in automatic
variables and never pass it as an argument."
So the code is
a) complete gibberish as far as the standard C language is concerned
b) incorrect even in the non-standard GNU-C language since it attempts
"to jump to code in a different function", and violates the suggestion
that one "never pass it as an argument."
Can somebody elaborate on this code i found on the net ?...
gcc compiles it but the result is strange (MacIntel Leopard, gcc 4.0.0)
$ gcc prog.c -o prog
$ ./prog
foo
r is not present to run this program
bar
quit
$
The output you see is one of the "totally unpredictable things" the gcc
documentation warns about.