473,385 Members | 1,588 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Global initialization of function pointer array

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
4 9592
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Felix Kater | last post by:
Hi, I've got a function with an array of 12 function pointers as an argument. When calling it: Is there a way to directly insert and fill the array argument (using braces {} or whatever) -- or do...
26
by: aruna.mysore | last post by:
Hi all, I have a specific problem passing a function pointer array as a parameter to a function. I am trying to use a function which takes a function pointer array as an argument. I am too sure...
5
by: Immortal Nephi | last post by:
I would like to design an object using class. How can this class contain 10 member functions. Put 10 member functions into member function pointer array. One member function uses switch to call...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
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,...
0
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
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...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.