Thread: Possible to create methods for data in structs?

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Dec 2004
    Posts
    35

    Possible to create methods for data in structs?

    Hi there,
    I am rather new to C, and I am having a bit of trouble finding information on this thing that I want to do (damn google is good - but it's a shame that the creator of C didn't know about it when he named the language after a letter of the alphabet!)

    Basically, I have a struct that looks like:

    Code:
    typedef struct {
            constpart * electrons;
            double energy;
            double weight ;
            double potential;
            double kinetic;
            char dead;
    } diffuser;
    and I want to define a method to call on the diffuser object (sorry about this object oriented lingo, I realise that a struct isn't really an object...) that might look like:

    Code:
    void zeroEnergy(void)
    {
            potential = 0.0;
            kinetic = 0.0;
            energy = 0.0;
    }
    is this possible?
    I have been searching around for info, and it does seem like it's do-able. Unfortunately my C book doesn't cover unions, and all the websites I have encountered do lots of things with pre-processor directives (and if anyone could point me to a good book on that topic, and on properly structuring programs by properly allocating the various bits of code between header files and program files then I'd be most grateful) - but the websites give examples that are a little hard to follow and one even included a header that I don't have! (oocp.h or something)...

    Any ideas?

    many thanks in advance
    James


    *edit*
    oh I should probably clarify - RE the title.
    I called this post what I did because I thought it would mean people might understand what I wanted to achieve functionality-wise... i know that structs are not objects and so cannot have 'methods' (i guess)... but I figured people might see the functionality I wanted and tell me the C way of doing it... thanks!
    Last edited by eccles; 12-10-2004 at 11:32 PM.
    VIM + gcc + beer... does life get any better?

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Code:
    void zeroEnergy(diffuser *d)
    {
            d->potential = 0.0;
            d->kinetic = 0.0;
            d->energy = 0.0;
    }
    ???

    [edit]unedit
    Last edited by Dave_Sinkula; 12-10-2004 at 11:35 PM.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered User
    Join Date
    Dec 2004
    Posts
    35
    well of course I could do that... but... I had hoped that there might be a way to package the function up into the struct as part of its definition, so it could be called like:
    Code:
    diffuser walker;
    walker.zeroEnergy();
    or is this just one of those 'well why don't you just go LIVE in java!' questions?
    VIM + gcc + beer... does life get any better?

  4. #4
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Code:
    typedef struct tag_diffuser
    {
       //constpart * electrons;
       double energy;
       double weight ;
       double potential;
       double kinetic;
       char dead;
       void (*foo)(struct tag_diffuser *this);
    } diffuser;
    
    void zeroEnergy(struct tag_diffuser *this)
    {
       this->potential = 0.0;
       this->kinetic = 0.0;
       this->energy = 0.0;
    }
    
    int main(void)
    {
       diffuser diff;
       diff.foo = zeroEnergy;
       diff.foo(&diff);
       return 0;
    }
    ???
    Last edited by Dave_Sinkula; 12-10-2004 at 11:37 PM. Reason: Posted reply instead of previous edit.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  5. #5
    Registered User
    Join Date
    Dec 2004
    Posts
    35
    ooh thankyou I didn't expect a reply this fast!

    to turn your example around a bit, would this be possible?:
    Code:
    typedef struct tag_diffuser
    {
       //constpart * electrons;
       double energy;
       double weight ;
       double potential;
       double kinetic;
       char dead;
       void (*zeroEnergy)(struct tag_diffuser *this);
    } diffuser;
    
    void ze(struct tag_diffuser *this)
    {
       this->potential = 0.0;
       this->kinetic = 0.0;
       this->energy = 0.0;
    }
    diff.zeroEnergy = ze;
    
    int main(void)
    {
       diffuser diff;
       diff.zeroEnergy(&diff);
       return 0;
    }
    ???

    and do i need to explicity pass a reference to the object or is there another way?
    (I realise that's how you've defined the function, but is there a way to do it that circumvents that requirement?)
    VIM + gcc + beer... does life get any better?

  6. #6
    Registered User
    Join Date
    Dec 2004
    Posts
    35
    C doesn't support that sort of construct (but C++ does )
    ok in that case my next question must be (and as you may have gathered from some of the variable names that this is a scientific computing application, and one that does not scale well with system size (ie number of particles in the molecular system)): are there any performance hits i'd take if i converted it to a C++ application? and would this be the way to go? or is C smaller, faster and sweeter?

    as i understand it, C++ is a superset of C, so would I just be able to use the OOP style stuff where I wanted it but keep the rest of it pretty much just C?

    are there arguments against this type of thing given the nature of the program?
    VIM + gcc + beer... does life get any better?

  7. #7
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Code:
    typedef struct tag_diffuser
    {
       void (*foo)(struct tag_diffuser *this);
       //constpart * electrons;
       double energy;
       double weight ;
       double potential;
       double kinetic;
       char dead;
    } diffuser;
    
    void zeroEnergy(struct tag_diffuser *this)
    {
       this->potential = 0.0;
       this->kinetic = 0.0;
       this->energy = 0.0;
    }
    
    int main(void)
    {
       diffuser diff = {zeroEnergy};
       diff.foo(&diff);
       return 0;
    }
    ???

    [edit]

    >and do i need to explicity pass a reference?

    Yes.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  8. #8
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    C doesn't support that sort of construct (but C++ does ). just use:

    Code:
    void zeroEnergy(diffuser * this)
    {
            this->potential = 0.0;
            this->kinetic = 0.0;
            this->energy = 0.0;
    }
    [edit]
    beat me to it
    [/edit]
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  9. #9
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    There are some things that are allowed in C that don't work in C++. Functions with an implicit return type, for example, or passing any amount of parameters to a function with an empty argument list. Old-style parameter lists are also disallowed in C++. (Not that you often see them - the only working piece of code I know that uses them is the zlib library.)
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  10. #10
    Registered User
    Join Date
    Dec 2004
    Posts
    35
    so do you have any thoughts then on the relative virtues of C/C++ in scientific computing applications?
    VIM + gcc + beer... does life get any better?

  11. #11
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Often, C will be a little faster. Not always. C++ usually allows better modeling of the problem, thus making implementation simpler. In general, C++ requires a little less code to do the same thing.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  12. #12
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    C will generally operate at a lower level then C++. Meaning that it will use fewer function calls and less memory then C++. The speed issue really depends on the program and for the most part is a non issue.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Multidimentional structs + memcpy() == FAIL
    By Viper187 in forum C Programming
    Replies: 8
    Last Post: 06-18-2008, 02:46 AM
  2. Turn Based Stradegy System Methods
    By TylerMoyer in forum Game Programming
    Replies: 2
    Last Post: 07-30-2007, 10:45 PM
  3. How to create a file association program?
    By eShain in forum Windows Programming
    Replies: 1
    Last Post: 03-06-2006, 12:15 PM
  4. Cannot create MDI Client Win
    By JaWiB in forum Windows Programming
    Replies: 1
    Last Post: 10-31-2005, 10:05 PM
  5. Methods for Sorting Structures by Element...
    By Sebastiani in forum C Programming
    Replies: 9
    Last Post: 09-14-2001, 12:59 PM