applying sizeof on function type. is it correct

S

sh.vipin

In the following program, with
gcc version 3.2.3 20030502 (Red Hat Linux 3.2.3-56)

there is no error. I have two queries.

1. isn't it illegal to apply sizeof on function type.
6.5.3.4 The sizeof operator
The sizeof operator shall not be applied to an expression that has
func
tion type....

in fact VS 2005 gives error on this.

2. If compiler is taking f as a pointer and not as function name,
which is legal to do though i am not sure if it is advisable to do so
here in this case, then why the sizeof f is "1" and not 4 as for
sizeof g

/** fully compilable program */
#include <stdio.h>
void f(void ){

}

void (*g)(void);
int main(int argc, char *argv[]){
g = f;
printf("\n%u %u",sizeof f , sizeof g);
}


-- vIpIn
 
E

Eric Sosman

In the following program, with
gcc version 3.2.3 20030502 (Red Hat Linux 3.2.3-56)

there is no error. I have two queries.

1. isn't it illegal to apply sizeof on function type.
6.5.3.4 The sizeof operator
The sizeof operator shall not be applied to an expression that has
func
tion type....

in fact VS 2005 gives error on this.

Yes, you have read 6.5.3.4 correctly.
2. If compiler is taking f as a pointer and not as function name,
which is legal to do though i am not sure if it is advisable to do so
here in this case, then why the sizeof f is "1" and not 4 as for
sizeof g

/** fully compilable program */
#include <stdio.h>
void f(void ){

}

void (*g)(void);
int main(int argc, char *argv[]){
g = f;
printf("\n%u %u",sizeof f , sizeof g);
}

By default, gcc accepts a language that is not exactly C,
but "C with extras." One of those extras allows sizeof to be
used on some kinds of operands that C forbids. You will find
this documented in gcc's "info" as a gcc extension.

The "info" also has a section on how to invoke gcc so it
compiles actual C instead of "gccC." If you compile your
program with the "-ansi" or "-std=c89" or "-std=c99" flag
(see the "info" for their meanings), you should see a different
outcome.
 
K

Keith Thompson

/** fully compilable program */
#include <stdio.h>
void f(void ){

}

void (*g)(void);
int main(int argc, char *argv[]){
g = f;
printf("\n%u %u",sizeof f , sizeof g);
}

Eric Sosman already answered your question (this is a gcc-specific
extension), and you can get gcc to warn you about it with the proper
options). There are also two (unrelated) problems in your printf
call.

"%u" is the format for printing a value of type unsigned int; the
result of sizeof is of type size_t, which is an unsigned type that may
or may not be unsigned int. If, for example, unsigned int is 32 bits
and size_t is 64 bits, your program could misbehave.

You print the new-line character at the beginning of the line. You
should print it at the end. A new-line before the start of your
output is unnecessary. A missing new-line at the end of your output
can cause your program to misbehave, depending on the implementation.

A more portable version (without fixing the sizeof problem):
printf("%lu %lu\n",
(unsigned long)sizeof f,
(unsigned long)sizeof g);

C99 specifies "%zu" for printing a size_t value directly, but
unfortunately C99 support is not universal.

Finally, since main returns an int, you should return an int; add
"return 0;" before the closing "}". It's not required in C99, but in
my opinion it's good style.
 
J

jacob navia

In the following program, with
gcc version 3.2.3 20030502 (Red Hat Linux 3.2.3-56)

there is no error. I have two queries.

1. isn't it illegal to apply sizeof on function type.
6.5.3.4 The sizeof operator
The sizeof operator shall not be applied to an expression that has
func
tion type....

in fact VS 2005 gives error on this.

lcc-win gives an error too.
 

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

Forum statistics

Threads
474,265
Messages
2,571,069
Members
48,771
Latest member
ElysaD

Latest Threads

Top