title: C function for converting arbitrary structure from Host to Network byte order?
body: <p>I have created a function <code>my_hton()</code> to convert data in host byte order to network byte order. It converts object of any arbitrary structure. If it is correct, why does C has functions like <code>htons()</code> and <code>htonl()</code> which converts only 2 and 4 bytes respectively, instead of having one function for all data types, or am I missing something?</p>
  
  <pre><code>void my_hton(char *buffer, char *p, size_t i){
      if( is_little_endian() ){
          while(i &gt; 0){
              *buffer = p[i-1];
              buffer++;
              i--;
          }
      }
  }
  
  struct demo{
      int a;
      char b;
      char str[100];
      float f;
      double d;
      int *ptr;
  };
  
  int main() {
      char buffer[10000] = "";
      struct demo object;
  
      /*
      * object initialization
      */
  
      my_hton(buffer, (char*) &amp;object, sizeof(object));
  
      return 0;
  }
  </code></pre>