pointers to functions in structs which take a pointer to that struct as an arg 
Author Message
 pointers to functions in structs which take a pointer to that struct as an arg

   Gurus,

   I can't find documentation on this anywhere, though I may be missing
something obvious since I am home-schooled in this sort of C stuff.

   I want to create a struct in C which (in lean form) looks like this:

typedef struct {
     int x,y,h,w;
     void (*paint)(lwin*),              
          (*born) (lwin*),              
          (*die)  (lwin*);              
     int  (*keypressed   )(lwin*,int,int),
          (*clicked      )(lwin*,int,int,int);

Quote:
} lwin;

   (That is, I want to pass a pointer to the structure when I call the
functions by the function pointers, like with c++'s 'this')

   When I compile in gcc I get errors like,

test.c:4: parse error after '*'

   for each time I try to declare an argument as type (lwin *).

   I thought declaring 'struct lwin;' before doing the typedef would fix
it -- it doesn't, nor does using the alternate (c++?) syntax "struct lwin
{.....};".

   I know what I want to do can be achieved in C++ using member
functions, but I don't want to use C++.

   My question is: Can I do this? What is the proper syntax?

   I would appreciate an e-mail cc to twm(a)cmu.edu.

Thank-you!

Tom 7



Wed, 27 Dec 2000 03:00:00 GMT  
 pointers to functions in structs which take a pointer to that struct as an arg

   typedef struct {
        int x,y,h,w;
        void (*paint)(lwin*),              
             (*born) (lwin*),              
             (*die)  (lwin*);              
        int  (*keypressed   )(lwin*,int,int),
             (*clicked      )(lwin*,int,int,int);
   } lwin;

Fixed:

   typedef struct lwin lwin;
   struct lwin {
        int x,y,h,w;
        void (*paint)(lwin*),              
             (*born) (lwin*),              
             (*die)  (lwin*);              
        int  (*keypressed   )(lwin*,int,int),
             (*clicked      )(lwin*,int,int,int);
   };

There are other possibilities too.
--
(supporter of the campaign for grumpiness where grumpiness is due in c.l.c)

Please: do not email me copies of your posts to comp.lang.c
        do not ask me C questions via email; post them instead



Wed, 27 Dec 2000 03:00:00 GMT  
 pointers to functions in structs which take a pointer to that struct as an arg

Quote:
>I want to create a struct in C which (in lean form) looks like this:

>typedef struct {
>     int x,y,h,w;
>     void (*paint)(lwin*),              
>          (*born) (lwin*),              
>          (*die)  (lwin*);              
>     int  (*keypressed   )(lwin*,int,int),
>          (*clicked      )(lwin*,int,int,int);
>} lwin;

The problem is that you're trying to reference lwin within the struct
before actually defining it.

One way of resolving this difficulty is to use the typedef FIRST...

        typedef struct lwin lwin;

        struct lwin {
                int x, y, h, w;
                void (*paint) (lwin *);
        }

or you could do this...

        struct lwin {
                int x, y, h, w;
                void (*paint) (struct LWIN *);
        }

        typedef struct lwin lwin;

You can use the lowercase lwin throughout as well since structure tags
occupy a different namespace from that of typedef's.

--
"I see you have books under your arm, brother. It is indeed a rare pleasure
these days to come across somebody that still reads, brother."

        - Anthony Burgess



Wed, 27 Dec 2000 03:00:00 GMT  
 pointers to functions in structs which take a pointer to that struct as an arg

Quote:

>         typedef struct lwin lwin;
>         struct lwin {
>                 int x, y, h, w;
>                 void (*paint) (lwin *);
>         }

This is correct.

Quote:
>         struct lwin {
>                 int x, y, h, w;
>                 void (*paint) (struct LWIN *);
>         }
>         typedef struct lwin lwin;

This is not.  Where did the uppercase `struct LWIN' come from?

--



Wed, 27 Dec 2000 03:00:00 GMT  
 pointers to functions in structs which take a pointer to that struct as an arg

Quote:
>>         typedef struct lwin lwin;
>>         struct lwin {
>>                 int x, y, h, w;
>>                 void (*paint) (lwin *);
>>         }

>This is correct.

>>         struct lwin {
>>                 int x, y, h, w;
>>                 void (*paint) (struct LWIN *);
>>         }
>>         typedef struct lwin lwin;

>This is not.  Where did the uppercase `struct LWIN' come from?

it should be "struct lwin" instead. i started out using LWIN as the
struct tag and lwin as the typedef, then remembered that i could use the
same name for both.

--
"I see you have books under your arm, brother. It is indeed a rare pleasure
these days to come across somebody that still reads, brother."

        - Anthony Burgess



Thu, 28 Dec 2000 03:00:00 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. cast pointer to struct to pointer to type of first member of struct

2. memory leak: pointer->pointer->pointer->struct

3. memory Leak: pointer->pointer->pointer->struct

4. Need DllImport expert to convert c++ to c# - same struct pointer in struct

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

6. Assign struct to struct via pointer

7. pass struct or pointer to struct ???

8. return a struct, not a struct pointer ?

9. Why pass structs? (not struct pointers)

10. pointer in struct that references a struct

11. int Pointer and struct pointer

12. Pointer to struct with pointer.

 

 
Powered by phpBB® Forum Software