Const array initialization problem

L

Louis Caron

I am facing the following problem:
- I have to build a const array containing functions pointers
- the indexes for this array are generated automatically by a tool (not my property)

How do I make sure that all of a sudden, the tool does not generate the indexes with a different order?

My first solution would have been to perform such kind of initialization:
typedef int (*func_ptr)(void);

const func_ptr func_array[MAX_INDEX];

{
func_array[AN_INDEX] = foo_an;
func_array[ANOTHER_INDEX] = foo_another;
func_array[MORE_INDEX] = foo_more;

}

but his does not work... can I initialize the array by specifying the array index(slicing?) ?

Thanks

Louis
 
R

Richard Heathfield

Louis Caron said:
I am facing the following problem:
- I have to build a const array containing functions pointers
- the indexes for this array are generated automatically by a tool (not
my property)

How do I make sure that all of a sudden, the tool does not generate the
indexes with a different order?

My first solution would have been to perform such kind of initialization:
typedef int (*func_ptr)(void);

const func_ptr func_array[MAX_INDEX];

{
func_array[AN_INDEX] = foo_an;
func_array[ANOTHER_INDEX] = foo_another;
func_array[MORE_INDEX] = foo_more;

}

but his does not work... can I initialize the array by specifying the
array index(slicing?) ?

I don't see a way to do that within the language, no.

The solution I would recommend is that you rewrite the tool to generate the
function pointer array initialisation code too - BUT you hint that you
won't be able to do this. Second-best - write your own tool, which reads
the output of the other tool and generates the function pointer array
initialisation code in such a way that the third-party tool and your end
code all match up nicely.
 
I

Ico

Louis Caron said:
I am facing the following problem: - I have to build a const array
containing functions pointers - the indexes for this array are
generated automatically by a tool (not my property)

How do I make sure that all of a sudden, the tool does not generate
the indexes with a different order?

My first solution would have been to perform such kind of
initialization: typedef int (*func_ptr)(void);

const func_ptr func_array[MAX_INDEX];

{
func_array[AN_INDEX] = foo_an;
func_array[ANOTHER_INDEX] = foo_another;
func_array[MORE_INDEX] = foo_more;
}

but his does not work... can I initialize the array by specifying the
array index(slicing?) ?

Some flavours of C allow the following syntax, I believe it's part of
the C99 standard - if its not, others will surely correct me.

const func_ptr func_array[MAX_INDEX] = {
[AN_INDEX] = foo_an,
[ANOTHER_INDEX] = foo_another,
[MORE_INDEX] = foo_more,
};
 
T

Tomás

Louis Caron posted:

const func_ptr func_array[MAX_INDEX];

{
func_array[AN_INDEX] = foo_an;
func_array[ANOTHER_INDEX] = foo_another;
func_array[MORE_INDEX] = foo_more;

}

Of course your first option would be to make the array non-const, and
then access it via a const pointer:

func_ptr a[MAX_INDEX];

a[AN_INDEX] = foo_an;
a[ANOTHER_INDEX] = foo_another;
a[MORE_INDEX] = foo_more;

const func_ptr * const array = a;
/* Use "array" now to access the array */


Another method would be something like the following:

#define ByTwo(a) a,a
#define ByThree(a) a,a,a
#define ByFive(a) a,a,a,a,a
#define BySeven(a) a,a,a,a,a,a,a
#define ByTen(a) a,a,a,a,a,a,a,a,a,a
#define ByTwenty(a) ByTen(a), ByTen(a)
#define ByTwentyFive(a) ByTen(a), ByTen(a), ByFive(a)
#define ByFifty(a) ByTwentyFive(a), ByTwentyFive(a)
#define ByOneHundred(a) ByFifty(a), ByFifty(a)

/* Let's assume you want the following

Index of foo_an = 28
Index of foo_another = 82
Index of foo_more = 118
*/

func_ptr const array[MAX_INDEX] = {
ByTwenty(0),BySeven(0), foo_an,
ByFifty(0),ByThree(0), foo_another,
ByTwentyFive(0),ByTen(0), foo_more };




-Tomás
 

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,769
Messages
2,569,582
Members
45,066
Latest member
VytoKetoReviews

Latest Threads

Top