title: Polmorphism application problem in C language
body: <p>this is my first question in stackowerflow. So i'm excited :)
  I am trying to practice polymorphism in C. I have read many articles. I wrote a small sample code with what I understood. my goal is to create one shape class. this class includes field and length methods. The rectangle and circle classes that derive from this class make their own calculation of the field and length methods.</p>
  <p>But there is a problem during compilation:
  <a href="https://i.stack.imgur.com/nCZGP.png" rel="nofollow noreferrer">enter image description here</a>
  Exception thrown at 0x000000000000000A in OOP_in_C_example_3_polymorphism_3.exe: 0xC0000005: Access violation executing location 0x000000000000000A.</p>
  <p>it gets better when I delete one of the lines. but this time it operates according to the last called constructor:
  <a href="https://i.stack.imgur.com/rcHoj.png" rel="nofollow noreferrer">enter image description here</a></p>
  <p>Where am I having problems?</p>
  <p>shape.h:</p>
  <pre><code>#include &lt;stdint.h&gt;
  
  struct Vtable;
  
  typedef struct{
      struct Vtable* vptr;
      uint16_t x;
      uint16_t y;
  
  }Shape;
  
  struct Vtable {
      void(*area)(Shape* me);
      void(*lenght)(Shape* me);
  };
  
  inline void area(Shape* me) {
      (*me-&gt;vptr-&gt;area)(me);
  }
  
  inline void lenght(Shape* me) {
      (*me-&gt;vptr-&gt;lenght)(me);
  }
  
  void shape_ctor(Shape* me, int x, int y);
  </code></pre>
  <p>shape.c:</p>
  <pre><code>#include &quot;shape.h&quot;
  
  void shape_ctor(Shape* me,int x, int y) {   
      struct Vtable vtbl = { &amp;area,&amp;lenght };
      me-&gt;vptr = &amp;vtbl;
      me-&gt;x   = x;
      me-&gt;y   = y;
  }
  </code></pre>
  <p>rect.h:</p>
  <pre><code>#include &quot;shape.h&quot;
  
  typedef struct  {
      Shape super;
      uint16_t en;
      uint16_t boy;
  }rect;
  
  void rect_ctor(rect* me, uint16_t x, uint16_t y, uint16_t en, uint16_t boy);
  </code></pre>
  <p>rect.c:</p>
  <pre><code>#include &quot;rect.h&quot;
  #include &lt;stdio.h&gt;
  
  void rect_lenght(void* me);
  void rect_area(void* me);
  
  
  void rect_ctor(rect* me,uint16_t x, uint16_t y, uint16_t en, uint16_t boy) {
      struct Vtable vtbl = { &amp;rect_area, &amp;rect_lenght };
      
      shape_ctor(&amp;me-&gt;super, x, y);
      me-&gt;super.vptr = &amp;vtbl;                 //override vptr
      me-&gt;en  = en;
      me-&gt;boy = boy;
  }
  
  
  void rect_lenght(void* me) {
      rect* me_ = (rect*)me;      //downcast işlemi
      printf(&quot;rectangle lenght: %d\n&quot;,(me_-&gt;en + me_-&gt;boy) * 2);
  }
  void rect_area(void* me) {
      rect* me_ = (rect*)me;      //downcast işlemi
      printf(&quot;rectangle area: %d\n&quot;, (me_-&gt;en)*(me_-&gt;boy));
  }
  </code></pre>
  <p>circle.h:</p>
  <pre><code>#include &quot;shape.h&quot;
  
  typedef struct {
      Shape super;
      uint16_t radius;
  }circle;
  
  
  void circle_ctor(circle* me, uint16_t x, uint16_t y, uint16_t r);
  </code></pre>
  <p>circle.c:</p>
  <pre><code>#include &quot;circle.h&quot;
  #include &lt;stdio.h&gt;
  
  void circle_lenght(void* me);
  void circle_area(void* me );
  
  
  void circle_ctor(circle* me, uint16_t x, uint16_t y, uint16_t r) {
      struct Vtable vtbl = {&amp;circle_area,&amp;circle_lenght};
  
      shape_ctor(&amp;me-&gt;super, x, y);
      me-&gt;super.vptr = &amp;vtbl;
      me-&gt;radius = r;
  }
  
  
  void circle_lenght(void* me) {
      circle* me_ = (circle*)me;
      printf(&quot;circle lenght: %f\n&quot;, (me_-&gt;radius) * (2) * (3.14));
  }
  void circle_area(void* me){
      circle* me_ = (circle*)me;
      printf(&quot;circle area: %f\n&quot;, (me_-&gt;radius) * (me_-&gt;radius) * (3.14));
  }
  </code></pre>
  <p>main.c:</p>
  <pre><code>#include &lt;stdio.h&gt;
  #include &quot;rect.h&quot;
  #include &quot;circle.h&quot;
  
  
  int main() {
      rect  k1;
      circle d1;
  
      rect_ctor(&amp;k1, 10, 10, 15, 20);
      circle_ctor(&amp;d1, 10, 10, 5);
      
      
      k1.super.vptr-&gt;lenght(&amp;k1);
      d1.super.vptr-&gt;lenght(&amp;d1);
  }
  </code></pre>