Help in initializing function pointers within structures 
Author Message
 Help in initializing function pointers within structures

Hi.

I have a pointer to a function within a structure and I want it
initialized "automatically". For example,

/* The header file */
#ifndef SOP_H
#define SOP_H
struct sop
{
  int (*func)(struct sop *s, FILE *ifp);
  initialize_sop() {
    func = sop_func;
  }

Quote:
};

#endif /* SOP_H */

/* The supporting program */
#include "sop.h"

int sop_func( sop *s, FILE *ifp )
{
/* stuff here */

Quote:
}

I am getting an error message when I declare func. Any hints would be
appreciated.

john
(Have I heard of C++? Yeah. I want to learn how to do it in C.)

--
-----
John F. Chionglo                    Dofasco Inc.
B.A.Sc., P.Eng.                     Box 2460, Hamilton, ON
Industrial Engineer                 L8N 3J5 Canada
Tel: 905-548-7200 x3665             Fax: 905-548-4580



Mon, 04 Sep 2000 03:00:00 GMT  
 Help in initializing function pointers within structures

Since there are no constructors with C, you will have to initialize with an
assignment or a function call.  If you consistently remember to do it at
declaration time, you should be ok.  There really is simply _no_ way to make
it happen automagically.
--
Hypertext C-FAQ: http://www.eskimo.com/~scs/C-faq/top.html
C-FAQ ftp: ftp://rtfm.mit.edu, C-FAQ Book: ISBN 0-201-84519-9
Try "C Programming: A Modern Approach" ISBN 0-393-96945-2
Want Software?  Algorithms?  Pubs? http://www.infoseek.com



Mon, 04 Sep 2000 03:00:00 GMT  
 Help in initializing function pointers within structures


: Hi.

: I have a pointer to a function within a structure and I want it
: initialized "automatically". For example,

: /* The header file */
: #ifndef SOP_H
: #define SOP_H
: struct sop
: {
:   int (*func)(struct sop *s, FILE *ifp);
:   initialize_sop() {
:     func = sop_func;
:   }
: };
: #endif /* SOP_H */

: /* The supporting program */
: #include "sop.h"

: int sop_func( sop *s, FILE *ifp )
: {
: /* stuff here */
: }

: I am getting an error message when I declare func. Any hints would be
: appreciated.

You can't do that in C.  The nearest you can come is something like
this (note that it's a very bad idea to initialise structures in
headers, and that sop is a declaration, not a definition).

#include <stdio.h>

struct Sop;

typedef int (*func_t)(struct Sop *);

int init_sop(struct Sop *);
int run_sop(struct Sop *);

struct Sop
{
  func_t running_sop;
  func_t initialize_sop;
  int digit;

Quote:
} sop = { init_sop, run_sop, 0 };

int init_sop(struct Sop *s)
{
  if (s->digit == 0) {
        s->digit = 99;
        return 99;
  }
  return 0;

Quote:
}

int run_sop(struct Sop *s)
{
  return s->digit;

Quote:
}

int main(void)
{
  (void) (sop.initialize_sop)(&sop);
  printf("Data is %d\n", (sop.running_sop)(&sop));

  return 0;

Quote:
}

Will



Tue, 05 Sep 2000 03:00:00 GMT  
 Help in initializing function pointers within structures

: struct Sop
: {
:   func_t running_sop;
:   func_t initialize_sop;
:   int digit;
: } sop = { init_sop, run_sop, 0 };

Wrong.  Try

  } sop = { run_sop, init_sop, 0 };

That'll teach me to edit code and not re-test it.

Will



Tue, 05 Sep 2000 03:00:00 GMT  
 Help in initializing function pointers within structures



Quote:
>Hi.

>I have a pointer to a function within a structure and I want it
>initialized "automatically". For example,

This won't happen in C. If you absolutely need such a feature, use
some other language like C++. In C++, you could declare a constructor
for the structure which will be called whenever such a structure
is instantiated, and can initialize some or all of its fields.

Quote:
>/* The header file */
>#ifndef SOP_H
>#define SOP_H
>struct sop
>{
>  int (*func)(struct sop *s, FILE *ifp);
>  initialize_sop() {
>    func = sop_func;
>  }
>};

Sorry, this just isn't C. In C, function declarations or definitions
must not appear inside a structure or union.

You can initialize a structure using an initializer; however, all of
the initializer elements must be constant expressions. This isn't
a problem in your case because the address of a function is an
``address constant''. So you can do:

        struct sop {
                int (*func)(struct sop *s, FILE *ifp);
        }
        struct sop sop_instance = { sop_func };

provided that sop_func has been suitably declared.

In traditional UNIX kernels, there is a static declaration which initializers
a whole array of structures with function pointers, much like:

        #include "foo.h"      /* foo driver */
        #include "bar.h"      /* bar driver */

        struct operations {
                int (*open)(/* .. args .. */);
                int (*close)(/* .. args .. */);
                /* ... other operations ... */
        } device_table[MAX_DEVICES] = {
                { foo_open, foo_close,  /* ... */ },
                { bar_open, bar_close,  /* ... */ },
        }

Each structure points to the basic operations of a device driver. The ``device
major number'' of a UNIX device has traditionally just been its index within
this array.  (Though these days, drivers are dynamically loaded).  Since C
originated as a language for implementing UNIX, it is useful to look at it for
hints of how some of the ways the language was intended to be used.



Tue, 05 Sep 2000 03:00:00 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. help: accessing a pointer to a list within an array of structures

2. vtable pointer problem with objects initialized within DLL

3. Initializing a pointer to a structure with a literal

4. Need some help on pointers,structures and functions....

5. Pointers to Structure that contains pointer to Function

6. Assigning structure pointer to function pointer

7. pointers within an array of structures

8. Accessing fields within structures with pointers...

9. pointers within a structure

10. How to use (pointer to function), and function and pointer to (pointer to function)

11. Functions within structures

12. How to initialize a global function pointer?

 

 
Powered by phpBB® Forum Software