473,321 Members | 1,916 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,321 software developers and data experts.

applying sizeof on function type. is it correct

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
Nov 2 '08 #1
3 1863
sh******@gmail.com wrote:
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.

--
Eric Sosman
es*****@ieee-dot-org.invalid
Nov 2 '08 #2
sh******@gmail.com writes:
[...]
/** 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.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Nov 2 '08 #3
sh******@gmail.com wrote:
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.

--
jacob navia
jacob at jacob point remcomp point fr
logiciels/informatique
http://www.cs.virginia.edu/~lcc-win32
Nov 2 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

70
by: Roy Yao | last post by:
Does it mean "(sizeof(int))* (p)" or "sizeof( (int)(*p) )" ? According to my analysis, operator sizeof, (type) and * have the same precedence, and they combine from right to left. Then this...
9
by: vijay | last post by:
Hello, I am new to C Programming and just started reading K&R. I was about to finish the pointers chapter but got very confused with: 1. int arr; >From what I have read, arr is a pointer to...
42
by: Christopher C. Stacy | last post by:
Some people say sizeof(type) and other say sizeof(variable). Why?
12
by: sonu | last post by:
#include<stdio.h> main() { int x=10,y; y=sizeof(++x); printf("x=%d\ny=%d\n",x,y); } Oput Put
90
by: pnreddy1976 | last post by:
Hi, How can we write a function, which functionality is similar to sizeof function any one send me source code Reddy
17
by: Jack | last post by:
For the structure below: struct A{ int a; double b; char c; }; struct A sa; printf("sizeof(A): %d\n", sizeof(sa));
15
by: subramanian100in | last post by:
Is it possible to measure the size of an array without using the sizeof operator ?
17
by: venkat | last post by:
Hi, I have written a program void main() { printf("%d %d\n", sizeof main, sizeof(main())); } in this program the output is 1 and 4,
8
by: c.lang.myself | last post by:
whenever we pass an array to function, we have to also pass its size.e.g if i am making a soritng function then void sort(int b,int size); Now I thought about using sizeof operator(macro?)...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.