what does this statement means?

R

Readon Shaw

return &dhcpInit;

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

Richard Heathfield

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top