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 :-) 8 1636
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
mdh <md**@comcast.n etwrites:
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_Keit h) 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"
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 ---
On May 23, 6:31 pm, Keith Thompson <k...@mib.orgwr ote:
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.
On May 23, 6:27 pm, Eric Sosman <esos...@acm-dot-org.invalidwrot e:
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.(ass uming fp points to myf)
On May 23, 6:39 pm, Joe Wright <joewwri...@com cast.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.
"mdh" <md**@comcast.n etwrote in message
news:11******** **************@ r19g2000prf.goo glegroups.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
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 paragraphs or
2020 characters) when they click on the submit button nothing happens.
When I say nothing happens I mean just that, nothing at all happens
the page just sits there as if nothing at all happened. If I remove
one line for the textarea,...
|
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 http://www.john-oliver.net/ *
* California gun owners - protect your rights and join the CRPA today! *
* http://www.crpa.org/ *
* San Diego shooters come to...
|
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 place... The spec says:
"In our example, we imported one simple type from one external
namespace, and used it for declaring attributes. XML Schema in fact
permits multiple schema components to be imported, from multiple
namespaces, and they can...
|
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 exciting but I assume I am
seriously short of knowledge, so here I am - asking for help. My current
situation is that I can define schemas that will generate very simple
classes but I have two major problems:
(1) I do not know how to write a...
|
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.
| |
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 because the length
of an embedded array instance does not match the declared length in the
layout
What does it mean?
|
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 output
to be within one directory, and I want to be able to combine files
from different directories into another directory of my choice).
My program seems to work fine, but I'm wondering about this loop:
for (int i = 0; i < numFiles; i++)
|
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 assigned to <pointerby
"new". Does "new" create a list of pointer names and the size of the
memory array they point to? I also read that some compilers may store
the number of consec mem locations a pointer points to, just before the
first data...
|
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 = showModalDialog('popup.htm','NEW',
'dialogWidth:400px;dialogHeight:155px;dialogLeft:200px;center:1;help: no; resizable: no; status: no; scroll: no;');
window.document.frmOpenInteraction1.ticket.checked=false;...
|
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...
|
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,...
| |
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...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |