Referencing an array of structs in an array of stracts 
Author Message
 Referencing an array of structs in an array of stracts


Quote:
>I have two structs looking like:
>typedef struct List{
>        void (*callback)();
>        Widget *widget;
>} List;
>and
>typedef struct Action{
>        char *label;
>        void (*callback)();
>        List *list;
>} Action;
>Compiler complains when I do something like
>action[i].list[j].callback
>I have tried to use parentheses, but the problem did not go away.
>I appreciate any suggestions.

You need to post an example of the offending code.  There is no way for
us to read this and guess what's happening.  In the hope that it might
be useful, I have prepared the following functioning skeleton code.  See
if your compiler likes it.  You might find something relevant to your
problem in it.

#include <stdlib.h>

typedef char Widget;

typedef struct List {
    void (*callback) ();
    Widget *widget;

Quote:
}   List;

typedef struct Action {
    char *label;
    void (*callback) ();
    List *list;

Quote:
}   Action;

void foo(void)
{

Quote:
}

int main()
{
    Action action[100];
    int i, j;

    for (i = 0; i < 100; i++) {
        action[i].callback = foo;
        action[i].list = malloc(100 * sizeof(List));
        for (j = 0; j < 100; j++)
            action[i].list[j].callback = foo;
    }
    for (i = 0; i < 100; i++)
        for (j = 0; j < 100; j++)
            action[i].list[j].callback();

    return 0;

Quote:
}

--

* Chicago, IL (USA)    


Sun, 15 Mar 1998 03:00:00 GMT  
 Referencing an array of structs in an array of stracts

Quote:

>>I have two structs looking like:
>>typedef struct List{
>>        void (*callback)();
>>        Widget *widget;
>>} List;
>>and
>>typedef struct Action{
>>        char *label;
>>        void (*callback)();
>>        List *list;
>>} Action;
>>Compiler complains when I do something like
>>action[i].list[j].callback
>>I have tried to use parentheses, but the problem did not go away.
>>I appreciate any suggestions.
>You need to post an example of the offending code.  There is no way for
>us to read this and guess what's happening.

Yessir, there is!

Fact is that "list" is a pointer to a List, not an array of Lists!

Assuming that action is defined as

Action action[NUMACTIONS]

or

Action *action;

then the access to the callback function must be

        (*action[i].list[j]->callback)()
                           ^^

Hope this helps,

Josef

Disclaimer: This message may not reflect the official opinion of my
emplyer wrt the use of arrays and structures in the C language.
--

DISCLAIMER: The statements made above are my personal opinion only!
            They do not reflect company policy in any way!



Sun, 15 Mar 1998 03:00:00 GMT  
 Referencing an array of structs in an array of stracts

Quote:
> >>I have two structs looking like:

> >>typedef struct List{
> >>        void (*callback)();
> >>        Widget *widget;
> >>} List;
> >>typedef struct Action{
> >>        char *label;
> >>        void (*callback)();
> >>        List *list;
> >>} Action;

> >>Compiler complains when I do something like

> >>action[i].list[j].callback

<snip>

Quote:
> Yessir, there is!
> Fact is that "list" is a pointer to a List, not an array of Lists!

True, but there is nothing to prevent list from POINTING to an
array.

Quote:

> Assuming that action is defined as

> Action action[NUMACTIONS]

> or

> Action *action;

> then the access to the callback function must be

>    (*action[i].list[j]->callback)()

Sorry, but the code above does not work.  You must use either

(*action[i].list[j].callback) () OR

(*action[i].list->callback) ()

In the first case, if list does point to an array, you can
dereference the pointer with [].  In the second case, you must
use -> and you're looking at the first element if list points to
an array, or just dereferencing a pointer to a structure.

The posters original line was :

action[i].list[j].callback;

which is not a call to anything!
callback is a pointer to a function.  This line should not
generate an error (a warning maybe, but I doubt that.)



Sun, 15 Mar 1998 03:00:00 GMT  
 Referencing an array of structs in an array of stracts

Quote:



>>>I have two structs looking like:
>>>typedef struct List{
>>>        void (*callback)();
>>>        Widget *widget;
>>>} List;
>>>and
>>>typedef struct Action{
>>>        char *label;
>>>        void (*callback)();
>>>        List *list;
>>>} Action;
>>>Compiler complains when I do something like
>>>action[i].list[j].callback
>>>I have tried to use parentheses, but the problem did not go away.
>>>I appreciate any suggestions.
>>You need to post an example of the offending code.  There is no way for
>>us to read this and guess what's happening.
>Yessir, there is!

No, imho there is no way to guess what is happening. Given the guesswork
below:

Quote:
>Fact is that "list" is a pointer to a List, not an array of Lists!

Please note that it is _not_ an array of pointers to List, nor is
it a pointer to a pointer to List!

Quote:
>Assuming that action is defined as
>Action action[NUMACTIONS]
>or
>Action *action;
>then the access to the callback function must be
>    (*action[i].list[j]->callback)()
>                       ^^
>Hope this helps,

How can this help? Let us assume that "action" is indeed defined
as an array of "Action" or as a pointer to "Action". In both cases,
action[i] is an expression that yields an "Action". "action[i].list"
is an expression that yields a pointer to "List", and
"action[i].list[j]" is an expression that yields a "List".

Now, "action[i].list[j].callback" is a member of a List, and
in ANSI C is can be called as either

  action[i].list[j].callback();

or

  (*action[i].list[j].callback)();

or, if you really like arrows so much

  ((action + i)->list + j)->callback();

or, if you really like the pre-ANSI syntax to make the use of
a function pointer explicit:

  (*((action + i)->list + j)->callback)();

Kurt
--
| Kurt Watzka                             Phone : +49-89-2180-6254




Sun, 15 Mar 1998 03:00:00 GMT  
 Referencing an array of structs in an array of stracts

Quote:

>I have two structs looking like:

>typedef struct List{
>        void (*callback)();
>        Widget *widget;
>} List;

>and

>typedef struct Action{
>        char *label;
>        void (*callback)();
>        List *list;
>} Action;

>Compiler complains when I do something like

>action[i].list[j].callback

>I have tried to use parentheses, but the problem did not go away.

>I appreciate any suggestions.

>-Simon

There might be other problems but on first glance you will need syntax

action[i]->list[j]->callback();



Mon, 16 Mar 1998 03:00:00 GMT  
 Referencing an array of structs in an array of stracts

Quote:


>>I have two structs looking like:

>>typedef struct List{
>>        void (*callback)();
>>        Widget *widget;
>>} List;

>>and

>>typedef struct Action{
>>        char *label;
>>        void (*callback)();
>>        List *list;
>>} Action;

>There might be other problems but on first glance you will need syntax
>action[i]->list[j]->callback();

Since this is the second post containing code that should not even compile,
and the second post stating that the "correct" syntax is a syntax error,
the question whether there is a common reason behind those errors arises,
at least for me. Given the original posters declarations, and assuming
that "action" is a pointer to Action (or, since that deos not matter in
this context, an array of Action), "action[i].list[j]" is _not_ a pointer
to list. We could assume, since the original poster has not told us
anything about it, that action is an array of pointers to Action. In
this case, "action[i]" is a pointer to Action, and
"action[i]->list[j].callback()" could make sense. The second "arrow"
cannot be correct, because we _know_ that the "list" in an Action
is a pointer to List, not a pointer to (or array of) pointer to List.

Kurt
--
| Kurt Watzka                             Phone : +49-89-2180-6254




Mon, 16 Mar 1998 03:00:00 GMT  
 Referencing an array of structs in an array of stracts




SP>I have two structs looking like:

SP>typedef struct List{
SP>        void (*callback)();
SP>        Widget *widget;
SP>} List;

SP>and

SP>typedef struct Action{
SP>        char *label;
SP>        void (*callback)();
SP>        List *list;
SP>} Action;

SP>Compiler complains when I do something like

SP>action[i].list[j].callback

SP>I have tried to use parentheses, but the problem did not go away.
SP>I appreciate any suggestions.

MA>You need to post an example of the offending code.  There is no way for
MA>us to read this and guess what's happening.

JM>Yessir, there is!
JM>Fact is that "list" is a pointer to a List, not an array of Lists!

The fact that I posted functioning code using the form
action[i].list[j].callback() in reply to SP should give you a hint that
you have misdiagnosed the problem, as well as not having read the
answer.

As soon as one allocates space for list to point to, a form like
*(list + j) become legal as long as it points to the allocated space.
This can be rewritten as list[j] in the code.

[snip]

JM>then the access to the callback function must be

JM>        (*action[i].list[j]->callback)()
JM>                           ^^
This will definitely _not_ work.

JM>Hope this helps,

I can't see how this incorrect response to a working and legal answer
could help anyone.

JM>Disclaimer: This message may not reflect the official opinion of my
JM>emplyer wrt the use of arrays and structures in the C language.

One can only hope this is true.
--

* Chicago, IL (USA)    



Mon, 16 Mar 1998 03:00:00 GMT  
 
 [ 7 post ] 

 Relevant Pages 

1. Referencing multi-d array of struct

2. passing an array of structs - Is it by value or reference

3. array of pointers to array of structs

4. qsorting & managing struct arrays, pointer arrays

5. problem realloc array inside array of struct

6. problem realloc array inside array of struct

7. Problem with struct/array inside a struct using fread/fwrite

8. struct.array works - struct->pointer not

9. Initialisation of structs containing arrays of structs...?

10. native dll calling with structs containing arrays of nested structs

11. char array (array of array...) question

12. Pass an array as reference

 

 
Powered by phpBB® Forum Software