sizeof argument

B

Ben C

Why does this compile with no warnings, what, if anything, does it mean,
and why does the program print out 1?

#include <stdio.h>

int main(void)
{
printf("%d\n", sizeof(int()));
return 0;
}

I know I probably "meant" sizeof (int), not sizeof (int()). I'm just
curious.

Thanks in advance for any explanations.
 
G

Guillaume

Ben said:
Why does this compile with no warnings, what, if anything, does it mean,
and why does the program print out 1?

#include <stdio.h>

int main(void)
{
printf("%d\n", sizeof(int()));
return 0;
}

I know I probably "meant" sizeof (int), not sizeof (int()). I'm just
curious.

Good one. ;-)

'int ()' is actually a function type. (A function taking no argument
and returning an int.)

sizeof on a function type is undefined behavior. Whether it returns
1 or 10000, doesn't matter. It makes no sense.

With a appropriate compiler options, you should get a warning:
for instance, with GCC:

gcc -Wall -pedantic

Test1.c: In function `main':
Test1.c:5: warning: invalid application of `sizeof' to a function type
 
S

santosh

Ben said:
Why does this compile with no warnings, what, if anything, does it mean,
and why does the program print out 1?

#include <stdio.h>

int main(void)
{
printf("%d\n", sizeof(int()));
return 0;
}

I know I probably "meant" sizeof (int), not sizeof (int()). I'm just
curious.

Maybe your warning level is not high enough? For me gcc with -Wall
-ansi and -pedantic options reports:
2.c: In function `main':
2.c:5: warning: invalid application of `sizeof' to a function type

But it compiles and prints 1 as output when run.
 
B

Ben C

Ben said:
Why does this compile with no warnings, what, if anything, does it mean,
and why does the program print out 1?

#include <stdio.h>

int main(void)
{
printf("%d\n", sizeof(int()));
return 0;
}
[...]
'int ()' is actually a function type. (A function taking no argument
and returning an int.)

sizeof on a function type is undefined behavior. Whether it returns
1 or 10000, doesn't matter. It makes no sense.

Of course, a function type! I was using -Wall but not -pedantic. Thanks.

The original example (which is on comp.programming) was this:

typedef void (*voidf)();
... sizeof (voidf()) ...

Well, not sizeof (voidf()) actually, but va_arg(ap, voidf()).

Here voidf() is also a function type-- a function that returns a pointer
to a function that returns void and takes no arguments that takes no
arguments. Surely not what was intended.
 
M

Martin Ambuhl

Ben said:
Why does this compile with no warnings,

Because you have not got your diagnositics turned on.
gcc, even with the source language specified as gnu99,
warns

In function 'main':
5: warning: invalid application of 'sizeof' to a function type
5: warning: format '%d' expects type 'int', but argument 2 has type
'long unsigned int'

what, if anything, does it mean,

nothing, except as a non-portable extension provided by your implementation
and why does the program print out 1?

because that's what you implementation decided to interpret this
non-portable construct as meaning.
#include <stdio.h>

int main(void)
{
printf("%d\n", sizeof(int()));
return 0;
}

I know I probably "meant" sizeof (int), not sizeof (int()). I'm just
curious.

Thanks in advance for any explanations.

Bill collectors write "thanks in advance." It is language used to
create dominance in the expectation of compliance. Polite human beings
do not use it.
 
G

Guillaume

Ben said:
Of course, a function type! I was using -Wall but not -pedantic. Thanks.

Yet another one of these "GCCisms". I don't think this should be allowed
at all. A warning doesn't seem enough to me here: and you only get one
if you go "-pedantic" with GCC. Weird.

Some other C compilers report this construct as an error, and I think
they should as well.
 
J

jaysome

Martin Ambuhl wrote:

[snip]
Bill collectors write "thanks in advance." It is language used to
create dominance in the expectation of compliance. Polite human beings
do not use it.

Really?

I guess that people like myself and probably the OP who have never
talked to or heard from a bill collector should be expected to know
better. Where did we go wrong?
 
P

pemo

Martin said:
Because you have not got your diagnositics turned on.
gcc, even with the source language specified as gnu99,
warns

In function 'main':
5: warning: invalid application of 'sizeof' to a function type
5: warning: format '%d' expects type 'int', but argument 2 has type
'long unsigned int'



nothing, except as a non-portable extension provided by your
implementation

because that's what you implementation decided to interpret this
non-portable construct as meaning.


Bill collectors write "thanks in advance." It is language used to
create dominance in the expectation of compliance. Polite human
beings do not use it.

Who's 'Bill Collectors' - any relation to 'Bill Posters?
 
R

Rod Pemberton

Martin Ambuhl said:
Bill collectors write "thanks in advance." It is language used to
create dominance in the expectation of compliance. Polite human beings
do not use it.

Wow. And, Plauger told me, "My, you do have a twisty way of thinking, don't
you?"

I may not be the correct person from your perspective to defend BenC, but, I
haven't seen a harsh word yet. I'd say it's a polite statement and nothing
more.
 
D

Default User

Martin Ambuhl wrote:

Bill collectors write "thanks in advance." It is language used to
create dominance in the expectation of compliance. Polite human
beings do not use it.

I'm going to have to call BS that. I don't believe it's in any way
impolite.



Brian
 
B

Ben C

Wow. And, Plauger told me, "My, you do have a twisty way of thinking, don't
you?"

I may not be the correct person from your perspective to defend BenC, but, I
haven't seen a harsh word yet. I'd say it's a polite statement and nothing
more.

Thank you! Certainly no implication of dominance or compliance was
intended, I am sorry if anyone took it that way.
 
K

Keith Thompson

Ben C said:
Thank you! Certainly no implication of dominance or compliance was
intended, I am sorry if anyone took it that way.

Some people are offended by the phrase "thanks in advance", thinking
it implies some sort of obligation. Others (myself included) don't
think it's at all a big deal. (I occasionally use "AtDhVaAnNkCsE"
myself.) I suggest that those who use the term should probably avoid
it, and those who are offended by it should consider that it's
probably not meant to be offensive.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top