title: OOP in C, implicitly pass self as parameter
body: <p>I've been working on an example to learn OOP in C. Currently I've come up with this code which is working, however I'm interested in making the methods implicitly pass self as a parameter.</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdbool.h&gt;
  #include &lt;stdlib.h&gt;
  //#include "Stopwatch.h"
  
  
  typedef struct stopwatch_s
  {
      unsigned int milliseconds;
      unsigned int seconds;
      unsigned int minutes;
      unsigned int hours;
  
      bool is_enabled;
  
      void ( *tick )      ( struct stopwatch_s* );
      void ( *start )     ( struct stopwatch_s* );
      void ( *stop )      ( struct stopwatch_s* );
      void ( *reset )     ( struct stopwatch_s* );
  } stopwatch_t;
  
  static void tick (stopwatch_t* _self)
  {
      stopwatch_t * self = _self;
      if (self-&gt;is_enabled)
      {
          self-&gt;milliseconds++;
          if (self-&gt;milliseconds &gt;= 1000)
          {
              self-&gt;milliseconds = 0;
              self-&gt;seconds++;
  
              if (self-&gt;seconds &gt;= 60)
              {
                  self-&gt;seconds = 0;
                  self-&gt;minutes++;
  
                  if (self-&gt;minutes &gt;= 60)
                  {
                      self-&gt;minutes = 0;
                      self-&gt;hours++;
                  }
              }
          }
      }
  }
  
  static void start (stopwatch_t* _self)
  {
      stopwatch_t * self = _self;
      self-&gt;is_enabled = true;
  }
  
  static void stop (stopwatch_t* _self)
  {
      stopwatch_t * self = _self;
      self-&gt;is_enabled = false;
  }
  
  static void reset (stopwatch_t* _self)
  {
      stopwatch_t * self = _self;
      self-&gt;is_enabled = false;
      self-&gt;milliseconds = 0;
      self-&gt;seconds = 0;
      self-&gt;minutes = 0;
      self-&gt;hours = 0;
  }
  
  void * new_stopwatch()
  {
      stopwatch_t * newInstance = (stopwatch_t *)calloc(1, sizeof(stopwatch_t));
      newInstance-&gt;is_enabled = false;
      newInstance-&gt;milliseconds = 0;
      newInstance-&gt;seconds = 0;
      newInstance-&gt;minutes = 0;
      newInstance-&gt;hours = 0;
      newInstance-&gt;tick = &amp;tick;
      newInstance-&gt;start = &amp;start;
      newInstance-&gt;stop = &amp;stop;
      newInstance-&gt;reset = &amp;reset;
  
      return newInstance;
  }
  
  void main()
  {
      struct stopwatch_s * Stopwatch = new_stopwatch();
      printf ("Initial: %d\n", Stopwatch-&gt;milliseconds);
      Stopwatch-&gt;start (Stopwatch);
      Stopwatch-&gt;tick (Stopwatch);
      Stopwatch-&gt;tick (Stopwatch);
      Stopwatch-&gt;tick (Stopwatch);
      printf ("Started: %d\n", Stopwatch-&gt;milliseconds);
      Stopwatch-&gt;stop (Stopwatch);
      Stopwatch-&gt;tick (Stopwatch);
      printf ("Stopped: %d\n", Stopwatch-&gt;milliseconds);
      Stopwatch-&gt;reset (Stopwatch);
      printf ("Reset: %d\n", Stopwatch-&gt;milliseconds);    
  }
  </code></pre>
  
  <p>I've tried reading and following <a href="http://www.cs.rit.edu/~ats/books/ooc.pdf" rel="noreferrer">Object Oriented Programming with ANSI-C</a>, but can't wrap my head around how to structure my "object" so instead of </p>
  
  <pre><code>Stopwatch-&gt;tick(Stopwatch);
  </code></pre>
  
  <p>I can write</p>
  
  <pre><code>Stopwatch-&gt;tick();
  </code></pre>