By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,604 Members | 1,414 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,604 IT Pros & Developers. It's quick & easy.

what does this statement means?

P: n/a
return &dhcpInit;

where dhcpInit() is a function decleared before.
would dhcpInit be called?

Nov 16 '06 #1
Share this Question
Share on Google+
1 Reply


P: n/a
Readon Shaw said:
return &dhcpInit;

where dhcpInit() is a function decleared before.
would dhcpInit be called?
No. The name of a function is converted into a pointer to that function. In
that particular case, & is redundant. The code could equally have said:

return dhcpInit;

In either case, the function is not called, but its address is returned to
the caller. The caller can then invoke that function through the pointer.

Example follows:

#include <stdio.h>

void foo(void)
{
puts("I'm foo!");
}

void bar(void)
{
puts("I'm bar!");
}

typedef void vvfunc(void);

vvfunc *choose(int n)
{
vvfunc *ptr = foo;
if(n % 2 == 1) /* how odd */
{
ptr = bar;
}
return ptr;
}

int main(void)
{
int i = 0;
int j = 1;
int k = i + j;
vvfunc *f;
do
{
printf("%d ", i);
f = choose(i); /* get a pointer to a function */
(*f)(); /* call that function */
i = j;
j = k;
k = i + j;
} while(i < 1000);
return 0;
}

Running this program gives the following output:

0 I'm foo!
1 I'm bar!
1 I'm bar!
2 I'm foo!
3 I'm bar!
5 I'm bar!
8 I'm foo!
13 I'm bar!
21 I'm bar!
34 I'm foo!
55 I'm bar!
89 I'm bar!
144 I'm foo!
233 I'm bar!
377 I'm bar!
610 I'm foo!
987 I'm bar!

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: normal service will be restored as soon as possible. Please do not
adjust your email clients.
Nov 16 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.