widget pointer initialization

I haev a list of widgets that all call the same callback. To determine
that the callback was called from the n’th widget I have a list such as:

static const PtWidget_t * list [] = { ABW_widget1, ABW_widget2, etc ];

I iterate through my list comparing the widget address to the address
that was called in the callback. When I have a match I know that the
selected widget was in the n’th position.

OK. This was working great when the list was inside a callback. A
second callback in the same source file needs to determine the n’th
position of a widget too. So I moved the list to global scope of the
same file.

There are no compile or link errors. Now however, all of the widget
pointers in the list are initialized to 0. What gives?

How can I create a single list in global scope?

Bill Caroselli wrote:


I haev a list of widgets that all call the same callback. To determine
that the callback was called from the n’th widget I have a list such as:

static const PtWidget_t * list [] = { ABW_widget1, ABW_widget2, etc ];

I iterate through my list comparing the widget address to the address
that was called in the callback. When I have a match I know that the
selected widget was in the n’th position.

OK. This was working great when the list was inside a callback. A
second callback in the same source file needs to determine the n’th
position of a widget too. So I moved the list to global scope of the
same file.

There are no compile or link errors. Now however, all of the widget
pointers in the list are initialized to 0. What gives?

How can I create a single list in global scope?

You’ll need to have a function that will initialize the array “list”. Call
that function from within the setup function of the window/dialog that the
widget1, widget2 etc belong to, so that ABW_widget1 etc have been filled
with valid widget pointers.
Otherwise if you let the compiler do the initialization, it will be done
when ABW_widget1 etc are NULL.

“Bill Caroselli” <qtps@earthlink.net> wrote in message
news:bvp502$8eg$1@inn.qnx.com

I haev a list of widgets that all call the same callback. To determine
that the callback was called from the n’th widget I have a list such as:

static const PtWidget_t * list [] = { ABW_widget1, ABW_widget2, etc ];

I iterate through my list comparing the widget address to the address
that was called in the callback. When I have a match I know that the
selected widget was in the n’th position.

OK. This was working great when the list was inside a callback. A
second callback in the same source file needs to determine the n’th
position of a widget too. So I moved the list to global scope of the
same file.

There are no compile or link errors. Now however, all of the widget
pointers in the list are initialized to 0. What gives?

You’re trying to grab the widget pointers from the ABW variables before the
widgets are created. Global objects are initialized before main() runs.

When your array was defined inside a function, it wasn’t initialized until
the function first ran; and since the function is a callback, there was
nothing to call it before the widgets were created and their widget pointers
stored in their ABW variables.

How can I create a single list in global scope?

Instead of initializing your array with values of the ABWs, initialize to
with their addresses:

static PtWidget_t *const *const list [] = { &ABW_widget1, &ABW_widget2,
etc ];

Yeah! good Wojtek!
Alain.

Wojtek Lerch a écrit:

“Bill Caroselli” <> qtps@earthlink.net> > wrote in message
news:bvp502$8eg$> 1@inn.qnx.com> …


I haev a list of widgets that all call the same callback. To determine
that the callback was called from the n’th widget I have a list such as:

static const PtWidget_t * list [] = { ABW_widget1, ABW_widget2, etc ];

I iterate through my list comparing the widget address to the address
that was called in the callback. When I have a match I know that the
selected widget was in the n’th position.

OK. This was working great when the list was inside a callback. A
second callback in the same source file needs to determine the n’th
position of a widget too. So I moved the list to global scope of the
same file.

There are no compile or link errors. Now however, all of the widget
pointers in the list are initialized to 0. What gives?



You’re trying to grab the widget pointers from the ABW variables before the
widgets are created. Global objects are initialized before main() runs.

When your array was defined inside a function, it wasn’t initialized until
the function first ran; and since the function is a callback, there was
nothing to call it before the widgets were created and their widget pointers
stored in their ABW variables.



How can I create a single list in global scope?



Instead of initializing your array with values of the ABWs, initialize to
with their addresses:

static PtWidget_t *const *const list [] = { &ABW_widget1, &ABW_widget2,
etc ];
\

Guest <someadress@some.site.com> wrote:
G > Bill Caroselli wrote:


I haev a list of widgets that all call the same callback. To determine
that the callback was called from the n’th widget I have a list such as:

static const PtWidget_t * list [] = { ABW_widget1, ABW_widget2, etc ];

I iterate through my list comparing the widget address to the address
that was called in the callback. When I have a match I know that the
selected widget was in the n’th position.

OK. This was working great when the list was inside a callback. A
second callback in the same source file needs to determine the n’th
position of a widget too. So I moved the list to global scope of the
same file.

There are no compile or link errors. Now however, all of the widget
pointers in the list are initialized to 0. What gives?

How can I create a single list in global scope?

G > You’ll need to have a function that will initialize the array “list”. Call
G > that function from within the setup function of the window/dialog that the
G > widget1, widget2 etc belong to, so that ABW_widget1 etc have been filled
G > with valid widget pointers.
G > Otherwise if you let the compiler do the initialization, it will be done
G > when ABW_widget1 etc are NULL.

I saw that the ABW_* fields were defines of AbGetABW( ABN_* ). So I
changed my list to:
static const int list [] = { ABN_widget1, ABN_widget2, etc };
and I chanegd my compare from:
if( widget == list )
to
if( widget == AbGetABW( list[n] )
and all works well now.

Wojtek Lerch <wojtek_l@yahoo.ca> wrote:

WL > Instead of initializing your array with values of the ABWs, initialize to
WL > with their addresses:

WL > static PtWidget_t *const *const list [] = { &ABW_widget1, &ABW_widget2,
WL > etc ];

Thanks Wojtek. I now have this solved but,

since ABW_widget1 is a define of AbGetABW( ABN_widget1 ), I can’t take
the address of it.

Bill Caroselli wrote:

I saw that the ABW_* fields were defines of AbGetABW( ABN_* ). So I
changed my list to:
static const int list [] = { ABN_widget1, ABN_widget2, etc };
and I chanegd my compare from:
if( widget == list > )
to
if( widget == AbGetABW( list[n] )
and all works well now.

It works now, but it relies on an undocumented macro that may
disappear or change its meaning the next time you generate. I don’t
think it’s likely to ever happen, but that’s just my personal opinion.
You have been warned.

Bill Caroselli wrote:

Wojtek Lerch <> wojtek_l@yahoo.ca> > wrote:

WL > Instead of initializing your array with values of the ABWs, initialize to
WL > with their addresses:

WL > static PtWidget_t *const *const list [] = { &ABW_widget1, &ABW_widget2,
WL > etc ];

Thanks Wojtek. I now have this solved but,

since ABW_widget1 is a define of AbGetABW( ABN_widget1 ), I can’t take
the address of it.

Yes you can. You haven’t noticed how AbGetABW() is defined, have you?..

#define AbGetABW( n ) ( AbWidgets[ n ].wgt )