Thread: Possible to create methods for data in structs?

Threaded 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?

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