<kzelec...@e3tech.local> writes:
> (sizeof main == 1) because (main) is treated as a logical value. Compare:
> { if (main) yadda; }
No, that's not the reason.
Consider that an `int` can also be treated as a logical value;
`if (1)` is valid, but `sizeof 1` is the same as `sizeof (int)`,
which is rarely 1.
The expression `main`, or any function name, is implicitly converted,
in most contexts, to a pointer whose type depends on how you declared
and/or defined the function.
Any pointer value can be used as a condition; it's implicitly
checked for inequality to 0, i.e., to the null pointer.
if (main) yadda;
is well defined but not useful, since `main`, after conversion,
can never be a null pointer.
But when it's the argument of `&` or `sizeof`, this implicit
conversion doesn't happen. `&main` gives you the address of the
`main` function, just like `main` by itself does. `sizeof main`
*would* be the size of a function, but there is no such concept in C,
so it's a constraint violation, requiring a diagnostic.
> Użytkownik "Myth__Buster" napisał w wiadomości grup
> dyskusyjnych:clcm-2013...@plethora.net...
>
> On a Linux system with gcc, I am just wondering why sizeof(main) can
> be 1 or sizeof when applied on any function name can yield 1 ever? Or
> is it only gcc's perspective to say sizeof of an implicit function
> pointer to be 1 since it gives sizeof(void) to be 1 based on the
> backward compatibility with the pre C99 notion that void* had its
> predecessor char* and usually sizeof(char) being 1?
>
> Also, I tried the invariably buggy code to see if at all sizeof(main)
> = 1 can be realized.
I think you mean pre C89 or pre-C90. C89/C90 introduced the `void`
keyword and the `void*` pointer type; C99 didn't make any real
changes in that area. But this has nothing to do with backward
compatibility; the same standard that introduced the `void` keyword
and type also prohibited applying `sizeof` to it.
(And `sizeof (char) == 1` by definition.)
`sizeof main == 1` because of a gcc extension. Standard C does
not permit pointer arithmetic on void* or on function pointers.
Gcc permits both, and specifies that, for example, adding 1 to a
void* or function pointer advances it by one byte (like adding 1 to
a char*). The authors of gcc chose to implement this by pretending
that the size of the type `void`, and the size of any function type,
is 1 (neither has a defined size in standard C).
I can see that pointer arithmetic on `void*` might be convenient in
some cases. The usefulness of arithmetic on function pointers is
less obvious, but I suppose it could have its uses. It's a pity
that implementing such arithmetic had the size effect of making
both `sizeof (void)` and `sizeof main` equal to 1; both results
are nonsensical.
--
Keith Thompson (The_Other_Keith)
ks...@mib.org <
http://www.ghoti.net/~kst>
Working, but not speaking, for JetHead Development, Inc.
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"