Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Global initialization of function pointer array

Reply
Thread Tools

Global initialization of function pointer array

 
 
Aniruddha
Guest
Posts: n/a
 
      12-10-2003
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?
 
Reply With Quote
 
 
 
 
Alf P. Steinbach
Guest
Posts: n/a
 
      12-10-2003
On 9 Dec 2003 23:15:09 -0800, (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.

 
Reply With Quote
 
 
 
 
Aniruddha
Guest
Posts: n/a
 
      12-11-2003
(Alf P. Steinbach) wrote in message news:<>...
> On 9 Dec 2003 23:15:09 -0800, (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.
 
Reply With Quote
 
Kevin D. Quitt
Guest
Posts: n/a
 
      12-11-2003
On 9 Dec 2003 23:15:09 -0800, (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
 
Reply With Quote
 
Barry Schwarz
Guest
Posts: n/a
 
      12-12-2003
On 10 Dec 2003 20:47:07 -0800, (Aniruddha)
wrote:

> (Alf P. Steinbach) wrote in message news:<>...
>> On 9 Dec 2003 23:15:09 -0800, (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>>
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
pointer to an array vs pointer to pointer subramanian100in@yahoo.com, India C Programming 5 09-23-2011 10:28 AM
initialization of array as a member using the initialization list aaragon C++ 2 11-02-2008 04:57 PM
Initialization of a const matrix implemented as pointer-to-pointer Andrea Taverna (Tavs) C Programming 17 09-24-2008 12:28 PM
Proper Initialization of Pointer to Pointer bwaichu@yahoo.com C Programming 11 02-09-2008 05:33 PM
array initialization in initialization list. toton C++ 5 09-28-2006 05:13 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57