473,387 Members | 3,820 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,387 software developers and data experts.

sizeof(<function name>); return wht??

Hi,

I had just written the following code but not able to figure out why
the sizeof() operator is printing the weired output when i give a
function name as its operand?

#include<stdio.h>
#include <assert.h>

int f();
int p();
int q(int , int );

int main() {

printf("size of %d \n",sizeof(f));
printf("size of %d \n",sizeof(p));
printf("size of %d \n",sizeof(q));
}

Output:
size of 1
size of 1
size of 1

Regards
Vikas Gupta
Dec 10 '07 #1
5 1984
vicks wrote:
Hi,

I had just written the following code but not able to figure out why
the sizeof() operator is printing the weired output when i give a
function name as its operand?

#include<stdio.h>
#include <assert.h>

int f();
int p();
int q(int , int );

int main() {

printf("size of %d \n",sizeof(f));
printf("size of %d \n",sizeof(p));
printf("size of %d \n",sizeof(q));
}
Try turning your warning level up and see what your compiler tells you.

--
Ian Collins.
Dec 10 '07 #2
On Dec 9, 9:30 pm, Chris Torek <nos...@torek.netwrote:
In article <0e9b0852-c84b-4ab2-9e97-3f3531b9d...@n20g2000hsh.googlegroups.com>
The problem is that you have not told your compiler to operate as
a C compiler. Most things labeled "C compilers" are not, in fact,
C compilers by default -- you have to give them some sort of
option(s) to make them obey their part of the "Standard C contract",
as it were.

In this case, my crystal ball says that you are using gcc, which
is not a C compiler unless you also use the "-ansi -pedantic".
Doing so will cause the compiler to (correctly) "emit a diagnostic"
telling you that sizeof should not be applied to function names.
....

Well yeap after using these switched for compilation I got the
warnings but my compiler still goes to give putput.. its ok.. With
this one more thing..

If i using this switches(-ansi ) then it uses C89/c99 std why am
asking this is because It gives an error when I uses "//" comment!!
Dec 10 '07 #3
>On Dec 9, 9:30 pm, Chris Torek <nos...@torek.netwrote:
>In this case, my crystal ball says that you are using gcc, which
is not a C compiler unless you also use the "-ansi -pedantic" [flags].
In article <87**********************************@l16g2000hsf. googlegroups.com>
vicks <vi*******@gmail.comwrote:
>If i using this switches(-ansi ) then it uses C89/c99 std why am
asking this is because It gives an error when I uses "//" comment!!
This is getting off-topic (there are gcc-specific groups for
gcc-specific questions), but ... there are a bunch of different
switches for various versions of gcc. Depending on which version
of gcc you have, it will support one or more of:

-ansi
-std=c89
-std=gnu89
-std=gnu99
-std=c99

The first two mean the same thing, selecting "C89" (also sometimes
called "C90" or "ANSI C", although technically "ANSI C" should now
refer to C99, as ANSI adopted the C99 standard shortly after that
standard was available). The "-pedantic" flag is required for
actual conformance, but the -std (or -ansi) flag is required to
select the mode before turning on conformance.

C89 does not have "//" comments, hence selecting C89 conformance
makes them become syntax errors, as they should be.

Gcc (any version) does not fully support C99 despite the -std=c99
option. Selecting -std=c99, if you have it at all -- it is not
present in older versions -- gets you "as close as possible" in
that particular version of gcc. Exactly how close depends on
which version of gcc you use.

The various "gnu" standards implement the GNUC languages, which
resemble C fairly closely, but are different in a number of important
ways, including things like allowing "sizeof function" (with the
result always being (size_t)1 in at least some variants of GNUC).
Note that different variants of GNUC support *different* features,
so that using any one particular feature may prevent you from using
another version of gcc on your code. (To avoid being trapped into
particular versions of particular compilers, you can simply avoid
using the extensions. Of course, if the extension does something
you really *want*, you have to choose between "portability" and
"convenience and perhaps even accomplishment".)
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.
Dec 10 '07 #4
On Dec 10, 11:38 am, vicks <vikas....@gmail.comwrote:
Hi,

I had just written the following code but not able to figure out why
the sizeof() operator is printing the weired output when i give a
function name as its operand?

#include<stdio.h>
#include <assert.h>

int f();
int p();
int q(int , int );

int main() {

printf("size of %d \n",sizeof(f));
printf("size of %d \n",sizeof(p));
printf("size of %d \n",sizeof(q));

}

Output:
size of 1
size of 1
size of 1

Regards
Vikas Gupta
I dont know how this get compiled. those compilers which follows the
ISO C++ standard will generate error on compilation.

Regards,
Sarath
Dec 10 '07 #5
On Dec 10, 11:38 am, vicks <vikas....@gmail.comwrote:
Hi,

I had just written the following code but not able to figure out why
the sizeof() operator is printing the weired output when i give a
function name as its operand?

#include<stdio.h>
#include <assert.h>

int f();
int p();
int q(int , int );

int main() {

printf("size of %d \n",sizeof(f));
printf("size of %d \n",sizeof(p));
printf("size of %d \n",sizeof(q));

}

Output:
size of 1
size of 1
size of 1

Regards
Vikas Gupta
Ignore my previous post. I forgot that you are asking about a C
environment.
Dec 10 '07 #6

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

Similar topics

2
by: Steven T. Hatton | last post by:
I believe the answer is that the language doesn't support the ability to retain a reference to a std::indirect_array<>, and would be illadvised to try, but I figure I'll ask just to be sure. What...
0
by: ma740988 | last post by:
Consider #include <iostream> #include <string> #include <map> using namespace std; struct dstream // data_stream class {
5
by: Steve | last post by:
can someone tell me how qsort function in <stdlib.h> is used (qsort(..........))? the function has a buffer, two void * parameters and the a pointer to a compare function. Thanks.
2
by: ANaiveProgrammer | last post by:
Hi all I have been trying to send a struct over socket. I tried to assign the values of struct to unsigned char array. I have attached both .c files at the bottom. 'Client.c' takes a struct,...
9
by: alopatenko | last post by:
I have a template class template <Class W> class WS At some point I have to use a STL list WS<W>objects so, I define #include <list>
6
by: Martin | last post by:
Hi I need to maintain a <setof pointers to objects, and it must be sorted based on the values of pointed objects, not pointer values. I can achieve this easily by defining my own comparing...
3
by: Barry | last post by:
As boost::pool_alloc use singleton holder for pool allocator, client programmers have to reclaim the memory by hand through calling singleton_pool<alloc_tag, elem_size>::purge_memory() or something...
1
tekninja
by: tekninja | last post by:
I wanted to write a function to take the tedium out of dynamically determining the total number of elements in an array. Within the main method this is easy as I simply use: sizeof( <array name>...
10
by: arnuld | last post by:
WANTED: /* C++ Primer - 4/e * * Exercise: 9.26 * STATEMENT * Using the following definition of ia, copy ia into a vector and into a list. Use the single iterator form of erase to...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.