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

Global initialization of function pointer array

P: n/a
I want to initialize an array of function pointers (global)
If I do it like:

/* definition of foo_1, foo_2, foo_3 all return void and take no args */
void (* foo[3]) ();
foo[0] = foo_1 ;
foo[1] = foo_2 ;
foo[2] = foo_3 ;

I get a compile time error, but if initialized like :

/* definition of foo_1, foo_2, foo_3 all return void and take no args */
void (* foo[3]) () = {foo_1 , foo_2, foo_3 };

There is no error, why?
Nov 14 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
On 9 Dec 2003 23:15:09 -0800, an***********@yahoo.co.in (Aniruddha) wrote:
I want to initialize an array of function pointers (global)
If I do it like:

/* definition of foo_1, foo_2, foo_3 all return void and take no args */
void (* foo[3]) ();
foo[0] = foo_1 ;
foo[1] = foo_2 ;
foo[2] = foo_3 ;

I get a compile time error, but if initialized like :

/* definition of foo_1, foo_2, foo_3 all return void and take no args */
void (* foo[3]) () = {foo_1 , foo_2, foo_3 };

There is no error, why?


There shouldn't be an error.

Perhaps the error for the assignments is due to having those assignments
outside any function.

Without further information it's impossible to say -- post a minimal
complete program that demonstrates the problem.

Nov 14 '05 #2

P: n/a
al***@start.no (Alf P. Steinbach) wrote in message news:<3f****************@News.CIS.DFN.DE>...
On 9 Dec 2003 23:15:09 -0800, an***********@yahoo.co.in (Aniruddha) wrote:
I want to initialize an array of function pointers (global)
If I do it like:

/* definition of foo_1, foo_2, foo_3 all return void and take no args */
void (* foo[3]) ();
foo[0] = foo_1 ;
foo[1] = foo_2 ;
foo[2] = foo_3 ;

I get a compile time error, but if initialized like :

/* definition of foo_1, foo_2, foo_3 all return void and take no args */
void (* foo[3]) () = {foo_1 , foo_2, foo_3 };

There is no error, why?


There shouldn't be an error.

Perhaps the error for the assignments is due to having those assignments
outside any function.


The initialization is not in any function (global), but the second
version works not the first.
Nov 14 '05 #3

P: n/a
On 9 Dec 2003 23:15:09 -0800, an***********@yahoo.co.in (Aniruddha) wrote:

I want to initialize an array of function pointers (global)
If I do it like:

/* definition of foo_1, foo_2, foo_3 all return void and take no args */
void (* foo[3]) ();
foo[0] = foo_1 ;
foo[1] = foo_2 ;
foo[2] = foo_3 ;


What error do you get?

void foo_1( void )
{
return;
}

void foo_2( void )
{
return;
}

void foo_3( void )
{
return;
}

void (* foo[3]) ();

int main( void )
{
foo[0] = foo_1 ;
foo[1] = foo_2 ;
foo[2] = foo_3 ;
return 0;
}

Generates no errors. Are you putting

foo[0] = foo_1 ;
foo[1] = foo_2 ;
foo[2] = foo_3 ;

inside a function? You must.
--
#include <standard.disclaimer>
_
Kevin D Quitt USA 91387-4454 96.37% of all statistics are made up
Per the FCA, this address may not be added to any commercial mail list
Nov 14 '05 #4

P: n/a
On 10 Dec 2003 20:47:07 -0800, an***********@yahoo.co.in (Aniruddha)
wrote:
al***@start.no (Alf P. Steinbach) wrote in message news:<3f****************@News.CIS.DFN.DE>...
On 9 Dec 2003 23:15:09 -0800, an***********@yahoo.co.in (Aniruddha) wrote:
>I want to initialize an array of function pointers (global)
>If I do it like:
>
>/* definition of foo_1, foo_2, foo_3 all return void and take no args */
>void (* foo[3]) ();
> foo[0] = foo_1 ;
> foo[1] = foo_2 ;
> foo[2] = foo_3 ;
>
>I get a compile time error, but if initialized like :
>
>/* definition of foo_1, foo_2, foo_3 all return void and take no args */
>void (* foo[3]) () = {foo_1 , foo_2, foo_3 };
>
>There is no error, why?


There shouldn't be an error.

Perhaps the error for the assignments is due to having those assignments
outside any function.


The initialization is not in any function (global), but the second
version works not the first.

In the second, the initialization is part of the definition.

In the first, the initialization is separate from the definition. The
definition is complete before any of the assignment statements are
processed. The initialization statements are not part of any
definition or declaration. But definitions and declarations are the
only things allowed outside of a function. Consequently, the
initialization statements are not placed properly and a diagnostic is
required.
<<Remove the del for email>>
Nov 14 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.