473,836 Members | 2,206 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

dereferencing function pointer types

Hi

I have program1.c:

typedef int (*fn_t)(int);

int fn( int f ){
return f;
}

int main( void ){

fn_t f= fn;

return f( 0 );
}

and program2.c

typedef int (*fn_t)(int);

int fn( int f ){
return f;
}

int main( void ){

fn_t f= fn;

return (*f)( 0 );
}

They both compile with no errors/warnings/comments under
gcc -Wall -ansi -pedantic

and in fact produce identical executables.

What is the story with function pointers? Is it good style to
dereference them to call them? Gcc doesn't even complain about return
(**f)(0); !

I would have guessed that since I can write f= fn; that f and fn have the
same type, but then Gcc doesn't complain about f= & fn; either, which
makes more sense when you look at the typedef.

So should I write f= fn or f= &fn, and should I write f(0); or (*f)(0);

The former in each case is what you would do if functions were like an
array type, and the latter makes sense if pointers were like scalars.

Surely the standard can't permit both - that seems quite sloppy.

Thanks,
viza
Jul 1 '08 #1
12 3943
On Jul 1, 9:11 pm, viza <tom.v...@gmil. comwrote:
Hi

I have program1.c:

typedef int (*fn_t)(int);

int fn( int f ){
return f;

}

int main( void ){

fn_t f= fn;

return f( 0 );

}

and program2.c

typedef int (*fn_t)(int);

int fn( int f ){
return f;

}

int main( void ){

fn_t f= fn;

return (*f)( 0 );

}

They both compile with no errors/warnings/comments under
gcc -Wall -ansi -pedantic

and in fact produce identical executables.

What is the story with function pointers? Is it good style to
dereference them to call them? Gcc doesn't even complain about return
(**f)(0); !
Dereferencing a function pointer does nothing.
I would have guessed that since I can write f= fn; that f and fn have the
same type, but then Gcc doesn't complain about f= & fn; either, which
makes more sense when you look at the typedef.

So should I write f= fn or f= &fn, and should I write f(0); or (*f)(0);
I prefer f = fn. As for the latter, some programmers/projects prefer
to have (*f)(0) to make clear 'f' is a function pointer; I prefer f(0)
though.
The former in each case is what you would do if functions were like an
array type, and the latter makes sense if pointers were like scalars.
Function pointers are scalars. But the effect of * and & does not
apply to them.
Surely the standard can't permit both - that seems quite sloppy.
It does

Jul 1 '08 #2
vi******@gmail. com wrote:
On Jul 1, 9:11 pm, viza <tom.v...@gmil. comwrote:
>Hi

I have program1.c:

typedef int (*fn_t)(int);

int fn( int f ){
return f;

}

int main( void ){

fn_t f= fn;

return f( 0 );

}

and program2.c

typedef int (*fn_t)(int);

int fn( int f ){
return f;

}

int main( void ){

fn_t f= fn;

return (*f)( 0 );

}

They both compile with no errors/warnings/comments under
gcc -Wall -ansi -pedantic

and in fact produce identical executables.

What is the story with function pointers? Is it good style to
dereference them to call them? Gcc doesn't even complain about return
(**f)(0); !
Dereferencing a function pointer does nothing.
>I would have guessed that since I can write f= fn; that f and fn have the
same type, but then Gcc doesn't complain about f= & fn; either, which
makes more sense when you look at the typedef.

So should I write f= fn or f= &fn, and should I write f(0); or (*f)(0);
I prefer f = fn. As for the latter, some programmers/projects prefer
to have (*f)(0) to make clear 'f' is a function pointer; I prefer f(0)
though.
>The former in each case is what you would do if functions were like an
array type, and the latter makes sense if pointers were like scalars.
Function pointers are scalars. But the effect of * and & does not
apply to them.
That's very close to being correct.
But if it were correct,
then (&&f)(0) would mean the same thing as (&f)(0),
and it doesn't.
(&&f)(0) is undefined.
An expression of function type is converted to an
expression of pointer type, in all but two contexts:
1 & (function type expression)
2 sizeof(function type expression)

Since sizeof is not defined for expressions of function type,
the only thing that you can do
with an expression of function type
in a correct C program, is to derive a pointer from it.

A function call is described a pointer to a function type expression
followed by the function call operator,
which is the parentheses in a function call.

So, if func is the name of a function
in a function call,
it is converted to a pointer.

func();
does the same thing as
(&func)();

In
(*func)();
the name of the function is converted to a pointer implicitly;
the indirection operator yields an expression of function type
and that expression is converted to a pointer.
So,
(************** *************** ****func)();
also does the same thing as
func();
But with
(&func)();
you have the result of the address operator
from a function type operand.
That's a function pointer, and if you apply & to that,
you get the address of the result of the address operator
and that means nothing.

--
pete
Jul 1 '08 #3
On Jul 1, 11:28 pm, pete <pfil...@mindsp ring.comwrote:
vipps...@gmail. com wrote:
On Jul 1, 9:11 pm, viza <tom.v...@gmil. comwrote:
The former in each case is what you would do if functions were like an
array type, and the latter makes sense if pointers were like scalars.
Function pointers are scalars. But the effect of * and & does not
apply to them.

That's very close to being correct.
But if it were correct,
then (&&f)(0) would mean the same thing as (&f)(0),
and it doesn't.
(&&f)(0) is undefined.
No, I'm correct. (&&f)(0) is a syntax error. && is the logical
operator, it has nothing to do with my claim that the effect of '&'
does not apply to function pointers.
Jul 1 '08 #4
On Tue, 01 Jul 2008 15:37:43 -0700, vippstar wrote:
On Jul 1, 11:28 pm, pete <pfil...@mindsp ring.comwrote:
>vipps...@gmail .com wrote:
On Jul 1, 9:11 pm, viza <tom.v...@gmil. comwrote:
The former in each case is what you would do if functions were like
an array type, and the latter makes sense if pointers were like
scalars.
Function pointers are scalars. But the effect of * and & does not
apply to them.

That's very close to being correct.
But if it were correct,
then (&&f)(0) would mean the same thing as (&f)(0), and it doesn't.
(&&f)(0) is undefined.
No, I'm correct. (&&f)(0) is a syntax error. && is the logical operator,
it has nothing to do with my claim that the effect of '&' does not apply
to function pointers.
pete surely meant (& &f)(0). He is correct that this is not allowed in C,
because &f does have an effect: it takes the address of function f, and
gives an rvalue[1] expression of type pointer-to-function. You cannot
apply the operator twice, because you cannot apply the unary & operator to
_any_ rvalue.

[1] value of object type that is not an lvalue
Jul 1 '08 #5
On Jul 2, 1:56 am, Harald van D©¦k <true...@gmail. comwrote:
On Tue, 01 Jul 2008 15:37:43 -0700, vippstar wrote:
On Jul 1, 11:28 pm, pete <pfil...@mindsp ring.comwrote:
vipps...@gmail. com wrote:
On Jul 1, 9:11 pm, viza <tom.v...@gmil. comwrote:
The former in each case is what you would do if functions were like
an array type, and the latter makes sense if pointers were like
scalars.
Function pointers are scalars. But the effect of * and & does not
apply to them.
That's very close to being correct.
But if it were correct,
then (&&f)(0) would mean the same thing as (&f)(0), and it doesn't.
(&&f)(0) is undefined.
No, I'm correct. (&&f)(0) is a syntax error. && is the logical operator,
it has nothing to do with my claim that the effect of '&' does not apply
to function pointers.

pete surely meant (& &f)(0). He is correct that this is not allowed in C,
because &f does have an effect: it takes the address of function f, and
gives an rvalue[1] expression of type pointer-to-function. You cannot
apply the operator twice, because you cannot apply the unary & operator to
_any_ rvalue.

[1] value of object type that is not an lvalue
ah I see. Yes, I do agree with this.
Jul 1 '08 #6
Harald van D?k wrote:
vippstar wrote:
>pete <pfil...@mindsp ring.comwrote:
.... snip ...
>>
>>then (&&f)(0) would mean the same thing as (&f)(0), and it
doesn't. (&&f)(0) is undefined.

No, I'm correct. (&&f)(0) is a syntax error. && is the logical
operator, it has nothing to do with my claim that the effect of
'&' does not apply to function pointers.

pete surely meant (& &f)(0). He is correct that this is not
allowed in C, because &f does have an effect: it takes the
address of function f, and gives an rvalue[1] expression of type
pointer-to-function. You cannot apply the operator twice, because
you cannot apply the unary & operator to _any_ rvalue.

[1] value of object type that is not an lvalue
No, functions are peculiar things. If f(whatever) is a function,
the expression f means the address of the code of that function.
The expression &f says take the address, and means the same thing.
Similarly &&f, and &&&&&&&f.

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home .att.net>
Try the download section.

Jul 2 '08 #7
CBFalconer <cb********@yah oo.comwrites:
[...]
No, functions are peculiar things. If f(whatever) is a function,
the expression f means the address of the code of that function.
The expression &f says take the address, and means the same thing.
So far, so good.
Similarly &&f, and &&&&&&&f.
You are mistaken on at least two counts.

First of all, &&f is tokenized as "&&" "f", so &&f is a syntax error.
vippstar mentioned this in text that you quoted (but apparently
neglected to read).

Second, &f is an expression of pointer-to-function type, but it's not
an lvalue, so & &f violates a constraint.

It's true that the following are all equivalent:
&f
f
*f
**f
***f
****f
etc.
because (a) "**" isn't a single token, and (b) the unary "*" operator
doesn't require an lvalue as its operand.

(I'm sure the trolls are chortling in their caves about one of the
"regulars" criticizing another. They are welcome, as always, to ...
never mind, this is a family newsgroup.)

--
Keith Thompson (The_Other_Keit h) 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"
Jul 2 '08 #8
vi******@gmail. com wrote:
(&&f)(0) is a syntax error. && is the logical
operator, it has nothing to do with my claim that the effect of '&'
does not apply to function pointers.
This one has something to do with it:

(&(&f))(0)

See what your compiler says.

--
pete
Jul 2 '08 #9
Keith Thompson said:

<snip>
(I'm sure the trolls are chortling in their caves about one of the
"regulars" criticizing another. They are welcome, as always, to ...
never mind, this is a family newsgroup.)
The trolls are themselves regulars, and they criticise other regulars all
the time, if the replies I see are anything to go by.

But comp.lang.c isn't about constructing a nice little community - it's
about discussing C. When people get the C wrong, they should expect to be
corrected. When they get it wrong a lot, they should expect to be
criticised. When they get it wrong practically all the time, it is likely
that such criticism will be particularly heavy, sometimes taking shape as
mockery or scorn.

If people want to avoid criticism, the answer is not "become one of the
'clique'" because the 'clique' in comp.lang.c is a myth. Rather, the
answer is "get your C right".

A 'clique' is an inner circle to which admittance is restricted by
arbitrary and perhaps unwritten rules. If comp.lang.c has an inner circle,
it is composed of *all* those people who know the C language, i.e. C
experts - which means that clc's 'inner circle' has people in it who have
never posted so much as a single article. They may never even have heard
of clc. You don't need to be a regular clc contributor to be a C expert,
but you do need to be a C expert if you want comp.lang.c to treat you as
one.

--
Richard Heathfield <http://www.cpax.org.uk >
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Jul 2 '08 #10

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

Similar topics

51
3202
by: BigMan | last post by:
Does the C++ standard define what should happen in case of NULL pointer dereferencing. If not, does it say that it is illegal? Where, if so, does it say it?
6
3102
by: Shankar | last post by:
Hello, We have a smart pointer class which provides the dereference operator -> to access the underlying object pointer. Now, we have a new requirement where a different type of object (e.g from memory, disk, network etc) needs to be returned by the smart pointer on access. I was thinking of using the function call operator () since that can take arguments e.g :-
3
3410
by: John Ratliff | last post by:
When I dereference a pointer, does it make a copy of the object? Say I had a singleton, and wanted an static method to retrieve it from the class. class foo { private: static foo *bar; foo() {} // no public creation!
4
16168
by: Pushkar Pradhan | last post by:
I have some functions which take as i/p a buffer (it can be float, char, or 16 bit, int etc.). The result is another o/p buffer, its type is also flexible (it could be a float, char etc.). I try to pass both as "void *buf" so that it can accept any data type. But since I access the buffer and try to assign its elements to another I get compile errors (I have pasted at the end). Now my question is how can I pass the i/p and o/p buffers...
28
2448
by: Martin Jørgensen | last post by:
Hi, I have a "funny" question, which I think is pretty "healthy" to examine... This program is being investigated: - - - - - - - #include <iostream> using namespace std; #define DAYS 7
9
3211
by: Cyron | last post by:
Hello friends, Recently I have begun exploring the features that the STL map collection provides. While learning how it worked, I encountered a result that confused me. Up until now, I had always been under the impression that if I had a pointer variable, p which contained the member "first", I could access this member variable either by: (*p).first OR p->first . That is, in general I thought p->x was just shorthand for (*p).x ?
4
5238
by: Pritam | last post by:
line 7: error: dereferencing pointer to incomplete type 1. #include<stdio.h> 2. #include<sys/stat.h> 3. #include<stdlib.h> 4. void execname() { 5. struct task_struct *my; 6. my = find_task_by_id(getpid()); 7. printf("%s",my->comm); error: dereferencing pointer to incomplete type
1
1917
by: David Mathog | last post by:
We've established that a line of code like: (void) function( (long *) &(something) ); may (and probably should) generate one of these warning: dereferencing type-punned pointer will break strict-aliasing rules if "something" is of some other type, say "int".
20
3239
by: prashant.khade1623 | last post by:
I am not getting the exact idea. Can you please explain me with an example. Thanks
0
9812
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9658
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10824
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10533
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
6975
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5644
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5813
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4443
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4003
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.