Re: CODE NEWBIE class declaration and function pointers

From: Patrick Dughi (
Date: 05/12/00

> First, I was wondering how you declared a class array while passing the
> same types to the constructer over and over.

        In both C and C++, you can initalize an array of structs very
simply.  The standard syntax is similar to arrays of basic


int myarray[] = {

   This creates an array of length 3, with 3,4,5 as it's members.


struct newstruct {
  int varone;
  int vartwo;

struct newstruct myarray[] = {

  Now, here you end up with an array containing two structs, the first
with varone=3,vartwo=4 & the second with varone=5,vartwo=6.

  You do not have to declare the variable type, or anything like that, or
even the size of the array (which is easily determined by the number of
entries).  Internally, the compiler knows how to structure this; it simply
inserts the first value into the structure, and then increments by the
size of the value.  Just like all arrays, when it has finished with an
individual array member, it then moves to the next after adding any byte
padding (if needed).  The practial upshoot of this is that when you define
a variable and immediately assign values to it, you will want to give your
arguments in the same order that the struct give them.

> you can have,
> CLASS varname(firstarg,secondarg,thirdarg); etc etc and it will pass those
> arguments to the proper constructer.  What I'm desiring is something like,
> CLASS varname[] ( insert multiple arguments for multiple constructions );
> I don't know what would be needed after the varname[] ...

        C is not C++.  Remember that. It's important.

        C is not C++. C is not C++.  C is not C++.

        If you walk away only remembering one thing, remember that.

        This is important, because C, not being C++, doesn't have classes,
and constructor or destructor methods.  It doesn't have classes, or class
methods at all.  It doesn't have overloaded functions so you can't (even
if you did have constructor methods in c) write one for normal usage, and
one for an array.  That's cause C isn't C++.  An example:

struct mine {
  int one;
  float two;
  char three;

struct mine myarray[4] = {

        This will give you no errors at all. Conversions are performed
implicitly. Even compiling under gcc with -Wall, you won't get errors.
This is just a nice byproduct of how array initilization works, so if you
use it, don't think that it's going to figure out which constructor method
to run like C++.  If you use C as if it was C++, you'll be in trouble.

        Since C isn't C++, you'll just want to stick to the array
assignment above.  If you need to do something tricky, like assign the
value of a calculation to the array when you're doing a dual definition
and assignment, you're kinda sol.  You'll have to write a function to
iterate through the array after you define it to assign those values.

> Also, how can you create a pointer that points to a function.  I read
> somewhere that this is possible, however I am doubtful.

        This is pretty easy actually.  Say I want to declare a pointer to
a funtion which returns an int.

int myfunc();

        I'd then create a pointer to it like this:
int (*funcp) () = myfunc;

        Lets say I've got something a bit more complex.  How about a new

int myfunc2(char *);

        And I'd create a pointer to it like this:

int (*funcp2) (char *) =myfunc2;

       Arrays of these pointers are easy to make.

int (*funcs[3]) (char *);
funcs[0]= myfunc2;
funcs[1]= myfunc2;
funcs[2]= myfunc2;

        Yes the calling syntax is simple: funcs[0]("my string here");

        You can even make a pointer to a 3 element array of function
pointers. I'd start with a void pointer;

void *ptr;
int ((int *) (*[3]) (char *)) ptr=funcs;
                (I think. someone may want to check this one)

        But, hopefully, you'll never need that level of indirection.


     | Ensure that you have read the CircleMUD Mailing List FAQ:  |
     |  |

This archive was generated by hypermail 2b30 : 04/10/01 PDT