473,320 Members | 1,695 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,320 software developers and data experts.

A "how does it work" type question

mdh
May I ask this.
Given the declaration:
int myf( int, int);

and a function pointer:

(*fp)=int myf(int, int);

where I am initializing fp to point at myf....or trying to..
int main (){

i=myf (3, 4);

return 0;

}

A definition of:

int myf( int a, int b){
return a+b;
}

What goes on in the workings of C when myf is called, and how does
this differ from calling myf like this?
(*fp)(3,4);

If I am not that clear with this question, then it is because I am not
that clear about what I am asking, but I am trying to get a conceptual
picture of what happens when functions are called....and marrying this
idea with function pointers. I may be making it a lot more complicated
than it really is....which is more than likely.
Well, here goes...and wearing the usual head protective gear for all
the answers :-)

May 24 '07 #1
8 1613
mdh wrote:
May I ask this.
Given the declaration:
int myf( int, int);
Okay.
and a function pointer:

(*fp)=int myf(int, int);
Gibberish. I *think* what you're talking about is

int (*fp)(int, int) = myf;

.... but that's only a guess.
where I am initializing fp to point at myf....or trying to..
int main (){

i=myf (3, 4);

return 0;

}

A definition of:

int myf( int a, int b){
return a+b;
}

What goes on in the workings of C when myf is called, and how does
this differ from calling myf like this?
(*fp)(3,4);

If I am not that clear with this question, then it is because I am not
that clear about what I am asking, but I am trying to get a conceptual
picture of what happens when functions are called....and marrying this
idea with function pointers. I may be making it a lot more complicated
than it really is....which is more than likely.
Well, here goes...and wearing the usual head protective gear for all
the answers :-)
Do you see the difference between

int x;
x = 42;

and

int x, y;
int *p = &x;
if (full_moon)
p = &y;
*p = 42;

? In the first case, the identity of `x' is known at compile
time and unchangeable at run time: The assignment always stores
a value in `x' and in no other variable. In the second case,
the assignment stores to a variable whose identity is not known
at compile time but is instead determined at run time. In some
executions of the code 42 is stored in `x' and in others it is
stored in `y'. Is that clear?

The difference between using an explicit function name and
a function pointer is the same: In one case the function being
called is known at compile time and unchangeable thereafter; in
the other, the function's type is known (just as it is known that
`*p' is an int and not a double) but its identity is determined
at run time. Let's flesh out your example in parallel with mine:

int myf(int, int);
int x = myf(3, 4);

as opposed to

int myf(int, int);
int yourf(int, int);
int x;
int (*fp)(int, int) = myf;
if (full_moon)
fp = yourf;
x = (*fp)(3, 4); /* alternatively, x = fp(3, 4); */

The function called in the final line could be either myf or
yourf, depending on the phase of the moon.

--
Eric Sosman
es*****@acm-dot-org.invalid
May 24 '07 #2
mdh <md**@comcast.netwrites:
May I ask this.
Given the declaration:
int myf( int, int);

and a function pointer:

(*fp)=int myf(int, int);
That's not valid C, but see below.
where I am initializing fp to point at myf....or trying to..
int main (){

i=myf (3, 4);

return 0;

}

A definition of:

int myf( int a, int b){
return a+b;
}

What goes on in the workings of C when myf is called, and how does
this differ from calling myf like this?
(*fp)(3,4);
[...]

Take a look at this program, particularly the comments:

#include <stdio.h>

/* Declare myf (not really necessary here). */
int myf(int, int);

/* Define myf. */
int myf(int a, int b)
{
return a + b;
}

int main(void)
{
/* Declare fp as a pointer-to-function, of a type appropriate
* so it can point to myf.
*/
int (*fp)(int, int);

int i, j;

/* Assign a value to fp so it points to myf. */
fp = myf;

/* Call the function indirectly via the pointer. */
i = fp(2, 3);

/* Call the function directly. */
j = myf(4, 5);

/* Display the results. */
printf("i = %d, j = %d\n", i, j);

return 0;
}

A function pointer points to a function. Typically it simply contains
the memory address of the function's entry point, but that's not
specified by the language; it could contain descriptor information, or
just an index into a system table.

Calling a function via a pointer does the same thing as calling it
directly. The difference (and the reason function pointers are
useful) is that you can, by assigning different values to the function
pointer, determine at run time which function is invoked by a
different call. See the standard qsort() function for an example of
this; there's no way qsort() could call your comparison function
directly, but it can call it indirectly because you've passed a
pointer to it as an argument.

You can probably ignore the following, at least for now; I'm going to
go into some of the more abstruse aspects of the language.

An indirect function call (using a function pointer) and a direct
function call (using the function's name) are really the same thing.
Any reference to a function's name (other than as the operand of a
unary "&" or "sizeof" operator) is implicitly converted to a pointer
to the function. This conversion happens even when the name is part
of a function call. So why does the function call work? Because the
function call operator requires a function *pointer* as its first
operand. Even a direct function call uses a function pointer, one
derived from the function name. (This is very similar to the way the
array indexing operator actually requires a pointer to the array's
first element, not an array value.)

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
May 24 '07 #3
mdh wrote:
May I ask this.
Given the declaration:
int myf( int, int);

and a function pointer:

(*fp)=int myf(int, int);

where I am initializing fp to point at myf....or trying to..
int main (){

i=myf (3, 4);

return 0;

}

A definition of:

int myf( int a, int b){
return a+b;
}

What goes on in the workings of C when myf is called, and how does
this differ from calling myf like this?
(*fp)(3,4);

If I am not that clear with this question, then it is because I am not
that clear about what I am asking, but I am trying to get a conceptual
picture of what happens when functions are called....and marrying this
idea with function pointers. I may be making it a lot more complicated
than it really is....which is more than likely.
Well, here goes...and wearing the usual head protective gear for all
the answers :-)
This is how I usually do it.

#include <stdio.h>

/* Define the function */
int myf(int a, int b) {
return a + b;
}

/* Define a pointer to function */
int (*fp)(int, int);

int main(void) {
fp = myf; /* Point fp to myf() */
printf("%d\n", fp(3, 4));
return 0;
}

--
Joe Wright
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
May 24 '07 #4
mdh
On May 23, 6:31 pm, Keith Thompson <k...@mib.orgwrote:

Take a look at this program, particularly the comments:
/* Declare fp as a pointer-to-function, of a type appropriate
* so it can point to myf.
*/
int (*fp)(int, int);

/* Assign a value to fp so it points to myf. */
fp = myf;

/* Call the function indirectly via the pointer. */
i = fp(2, 3);

/* Call the function directly. */
j = myf(4, 5);
A function pointer points to a function.
>
Calling a function via a pointer does the same thing as calling it
directly.
>
You can probably ignore the following, at least for now; I'm going to
go into some of the more abstruse aspects of the language.
Nope...this is the part I was particularly interested in.
>
An indirect function call (using a function pointer) and a direct
function call (using the function's name) are really the same thing.
Any reference to a function's name (other than as the operand of a
unary "&" or "sizeof" operator) is implicitly converted to a pointer
to the function. This conversion happens even when the name is part
of a function call. So why does the function call work? Because the
function call operator requires a function *pointer* as its first
operand. Even a direct function call uses a function pointer, one
derived from the function name.

Thanks...this is what I suspected...( well not exactly in those terms)
but that does make the syntax of the function pointer make more sense.
Thanks Keith.

May 24 '07 #5
mdh
On May 23, 6:27 pm, Eric Sosman <esos...@acm-dot-org.invalidwrote:
Is that clear?
Yes...I get that. I guess my question (and example) did not convey the
full meaning of what I was trying to ask. KT inadvertantly answered my
question below.

>
The difference between using an explicit function name and
a function pointer is the same: In one case the function being
called is known at compile time and unchangeable thereafter; in
the other, the function's type is known (just as it is known that
`*p' is an int and not a double) but its identity is determined
at run time.

Yes...that certainly re-inforces my understanding from a different
angle.
So, conceptually, is this a fair way of looking at it? ( which may
not exactly speak to the issue you are telling me).

Whether the fuction is explicityly named or called by a pointer, the
functions arguments and return type are known to the compiler by the
declaration. So, if an argument is expected, the next **bit/s of
information** will have to be ( ....arguments), (whether that is
preceded by the explicit function name or a function pointer.
And that is why:

int myf(arguments)

and

(*fp)(arguments)

are equivalent.(assuming fp points to myf)
May 24 '07 #6
mdh
On May 23, 6:39 pm, Joe Wright <joewwri...@comcast.netwrote:
>
This is how I usually do it.

#include <stdio.h>

/* Define the function */
int myf(int a, int b) {
return a + b;

}

/* Define a pointer to function */
int (*fp)(int, int);

int main(void) {
fp = myf; /* Point fp to myf() */
printf("%d\n", fp(3, 4));
return 0;
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---

I also like
"If you cannot explain a concept to a 4 year old, you do not
understand it yourself"

Thank you Joe.

May 24 '07 #7
"mdh" <md**@comcast.netwrote in message
news:11**********************@r19g2000prf.googlegr oups.com...
May I ask this.
Given the declaration:
int myf( int, int);

and a function pointer:

(*fp)=int myf(int, int);

where I am initializing fp to point at myf....or trying to..
Provided fp is declared correctly, you would use:

fp = myf; /* note the lack of ()s */
int main (){
i=myf (3, 4);
return 0;
}

A definition of:

int myf( int a, int b){
return a+b;
}

What goes on in the workings of C when myf is called, and
how does this differ from calling myf like this?
(*fp)(3,4);
That would normally be written:

fp(3,4); /* the same as myf(3,4); */
If I am not that clear with this question, then it is because I am
not that clear about what I am asking, but I am trying to get a
conceptual picture of what happens when functions are
called....and marrying this idea with function pointers. I may be
making it a lot more complicated than it really is....which is
more than likely.
While not exactly correct, you can think of myf -- without the ()s -- as a
function pointer that is initialized to point to the definition you gave.
Once you assign another function pointer, fp, to point to the same function,
you can call either of them the same way. A typical implementation will
treat the two calls differently under the hood, but they look the same in C.

S

--
Stephen Sprunk "Those people who think they know everything
CCIE #3723 are a great annoyance to those of us who do."
K5SSS --Isaac Asimov
--
Posted via a free Usenet account from http://www.teranews.com

May 24 '07 #8
mdh
On May 23, 6:29 pm, "Stephen Sprunk" <step...@sprunk.orgwrote:
While not exactly correct, you can think of myf -- without the ()s -- as a
function pointer that is initialized to point to the definition you gave.
Once you assign another function pointer, fp, to point to the same function,
you can call either of them the same way. A typical implementation will
treat the two calls differently under the hood, but they look the same in C.

Thanks Stephen...
Despite the fact that C is a "language" I really do find it helpful to
have some conceptual idea of what I am doing.
So thanks again

May 25 '07 #9

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

Similar topics

1
by: Pete Mahoney | last post by:
Ok I use a textarea to store data input by the user, and then upon them clicking the submit button I store this data to a database. The problem is once the user inputs too much data (about 3...
5
by: John Oliver | last post by:
I'd like the email produced by FormMail to show a specific From: address rather than postmaster@server.host.name Googling isn't helping me... not sure what to look for :-( -- * John Oliver ...
2
by: Vitali Gontsharuk | last post by:
Hi! I can't find an answer to the following question: does the "import" command in XML Schema import also element declarations? Or only type definitions? As far as I understood, the first takes...
2
by: Marek | last post by:
Actually, the very first question I should ask is "does it make sense at all?". For 2 days I've been trying to generate C# classes using XML schema and xsd.exe tool. So far the results are not...
9
by: Xiangliang Meng | last post by:
Hi, all. I see a very strange fragment code today. Uint32 enum { a = 100; b = 200; }; NOTE: Uint32 is defined to be 'unsigned' in other source files.
2
by: Tom | last post by:
I'm getting this error when I try to pass a structure to a dll. An unhandled exception of type 'System.ArgumentException' occured in Test1.exe Additional Information: Type could not be marshaled...
51
by: Tony Sinclair | last post by:
I'm just learning C#. I'm writing a program (using Visual C# 2005 on WinXP) to combine several files into one (HKSplit is a popular freeware program that does this, but it requires all input and...
5
by: mkaushik | last post by:
Hi everyone, Im just starting out with C++, and am curious to know how "delete <pointer>", knows about the number of memory locations to free. I read somewhere that delete frees up space...
5
by: Faizmysore | last post by:
This code works good in IE, function multiuser() { document.frmOpenInteraction1.Create.disabled=true; document.frmOpenInteraction1.hidmultiuser.value="Yes"; var r =...
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: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
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...
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
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.