title: sizeof () returning weird values in c
body: <p>Hi guys I'm implementing a multi protocol server for an assignment, the problem is that when I use a function (which works very well with TCP) with UDP it returns some weird values for sizeof (rslt)
  this is the result of printing sizeof (rslt), </p>
  
  <blockquote>
    <p>227247297301947350235748709778135745577468557658424998002441906536119128023452627558110097185387624219639637941804882228932644995956639137025857651046253325633445227449505007892388016152332207357456599282029784441594833774964182706535976953843609432439401651880375483842720203925484233013596689866744018141665458868649317570117104675934020967854264479860821412689862026122703619698561786472189815291976898928623462131088247227757807721072403795459334826914847683531968239236940500199813269288827276477929188384103679545017307548348628331645669098568909370657065971531337341841662603278594221673762642070120745706004269203411380905823024264516528460218266891034505741227610635108566850102547194244234491595802548473820585414612221747352614218601769292070105886498675038800726670654977989907519633820118543794748179420330708894380512935922929197901192958685020453701483477243638441444607644584045103566854732472043437182834837031151276986466924005056107273112615576154305580818456831896986707479921666804850676978009175139482011600872247781696198151360337444421986267162518809749962279541373290140958667251725787077177421917491032187282871406009682186676021616280669304703976683626337533413464759379774970193907347344224349646126910484652263652582861429537622470060950001898433056178384737790185034727054100655408078501650100419653460914613312003325114514976904977903673785434865519242218932927846724749932914680623078230459473390721631571879357028192620799780670249628404890444625081372459032351139442349193023231648953178892393215225869560487387734447607698987643759581917130647967805017677771860565983455868285091963816554539133509004989767441167607027792254042536381477844753039244848391452325130886154506629373015159028410740477270477681641090045720626668817450051271286643920235139888460288674438576178355066172484843620406990305664863352348519178027532105785588869464103655179641271672762329909252285829060459009910768529658550406700160376994566456247010717483703953366527358263048640804889600709363152032432252410085951706712799052430344895928694442582957645431633369053034305667463823090663464760738164090556152525774466164744037686011101663800170078366921209056500303914474056087892235358298164024357215391588641194707115978140407226154118906542449763834465166058632126443504894420499741513006310822583552919768761548462977412311647236868595822220663005491511561850746178480104681354464439867884163742890098946294243688211351484163203709554184403155848611709506733882593468169901350306884677995915932010321114285505973740406847370286120635158347518247510277661323300043873047950392164942431645418302947950413506210734540437852746639625375347610012995678139901099918378380261823821336209645745980214130228811271994044550978448678801380815142903470540468943168894917972070887965091844883478597646129241181927386702586436228869778709864490945388199788157229336144495868928370130428522581684988316175567123887288601709737949842018996477785851910159268866322819839489792236073965243444078966709362490743754122694159124524052020792484412812868943800034992477766811147183724209135415987638202158028537905514471899977100572198298304042878318729460950115591276417376811003545986574793531075323131684626381339291509853454358163265714861331384777107194532363959316468225193409347344254519577720009422713289644543760402612121969692919169780911452088487833412590590051242129109845157720008193666133253502916786223810520703777964003329058076952385412988850950651536066213022496822713600805867150527938872166308595393085376986603206932959009695449149501563093375374780821185997456057835811105292775253063772002382719467857393447371079222454636084693485484877479484598463692899799144769464635549655732415843871907114705042415521903704488250326193374402414538809696743414494509229818516037189690520555940494775487076957344200010781415703176415728240860895233439634632912797526990278731739984780391745714390037327203315505427496375837124960562284419088384.000000</p>
  </blockquote>
  
  <p>The server</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;sys/socket.h&gt;
  #include &lt;arpa/inet.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;string.h&gt;
  #include &lt;unistd.h&gt;
  #include &lt;netinet/in.h&gt;
  #include &lt;sys/types.h&gt;
  #include &lt;netinet/tcp.h&gt;
  #include &lt;errno.h&gt;
  
  #define MAXLINE 100
  
  
  struct equation {
      float a;
      char c;
      float b;
  };
  
  struct result {
      long double res;
  };
  
  int MAX (int a,int b){
      if (a&gt;b) return a;
      return b;
  }
  
  
  int readn(int fd, void *vptr, size_t n)
  {
      size_t  nleft;
      ssize_t nread;
      char   *ptr;
      ptr = vptr;
      nleft = n;
      while (nleft &gt; 0) {
          if ( (nread = read(fd, ptr, nleft)) &lt; 0) {
              if (errno == EINTR)
                  nread = 0;      /* and call read() again */
              else
                  return (-1);
          } else if (nread == 0)
              break;              /* EOF */
  
          nleft -= nread;
          ptr += nread;
      }
      return (n - nleft);         /* return &gt;= 0 */
  }
  
  
  int writen(int fd, const void *vptr, size_t n)
  {
      size_t nleft;
      ssize_t nwritten;
      const char *ptr;
      printf ("\nI'm In %Lf\n",n);
      ptr = vptr;
      nleft = n;
      printf ("%Lf NNNNN  %Lf SIZE %Lf \n",nleft,n,sizeof(*vptr));
      while (nleft &gt; 0) {
          printf ("%Lf\n",nleft);
          fflush (stdout);
          if ( (nwritten = write(fd, ptr, nleft)) &lt;= 0) {
              if (nwritten &lt; 0 &amp;&amp; errno == EINTR)
                  nwritten = 0;   /* and call write() again */
              else
                  return (-1);    /* error */
          }
  
          nleft -= nwritten;
          ptr += nwritten;
      }
      printf ("\n 5alawees \n");
  // printf ("\n%s\n",vptr);
  //    ffulsh (stdout);
      return (n);
  }
  
  void HandleClient(int comm_fd);
  void Die (const char * msg)
  {
      perror(msg);
      exit(1);
  }
  
  int passiveUDP (short port){
      struct sockaddr_in servaddr;
      int listen_fd;
      if ((listen_fd = socket(AF_INET, SOCK_DGRAM, 0)) &lt; 0)
      {
          Die("Falied to create socket");
      };
  
      //printf ("%d" ,listen_fd);
      memset( &amp;servaddr,0, sizeof(servaddr));
  
      servaddr.sin_family = AF_INET;
      servaddr.sin_addr.s_addr = htons(INADDR_ANY);
      servaddr.sin_port = htons(port);
  
      if (bind(listen_fd, (struct sockaddr *) &amp;servaddr, sizeof(servaddr))&lt;0)
      {
          Die("Failed to bind socket to address");
      }
      return listen_fd;
  }
  
  int passiveTCP (short port){
      struct sockaddr_in servaddr;
      int listen_fd;
      if ((listen_fd = socket(AF_INET, SOCK_STREAM, 0)) &lt; 0)
      {
          Die("Falied to create socket");
      };
  
      memset( &amp;servaddr,0, sizeof(servaddr));
  
      servaddr.sin_family = AF_INET;
      servaddr.sin_addr.s_addr = htons(INADDR_ANY);
      servaddr.sin_port = htons(port);
  
      if (bind(listen_fd, (struct sockaddr *) &amp;servaddr, sizeof(servaddr))&lt;0)
      {
          Die("Failed to bind socket to address");
      }
  
      if (listen(listen_fd, 10) &lt; 0)
      {
          Die("Failed to listen on server socket");
      }
      return listen_fd;
  }
  
  int main(int argc, char * argv[])
  {
      if (argc != 2) {
          fprintf(stderr, "USAGE: ./HelloITServer &lt;port&gt;\n");
          exit(1);
      }
      char str[100];
      int listen_fd, comm_fd;
      int    usock = passiveUDP (atoi (argv[1]));               /* UDP socket  */
  
      int    tsock = passiveTCP (atoi (argv[1]));           /* TCP master socket  */
      int    nfds;
      fd_set rfds;                 /* readable file descriptors */
      struct sockaddr_in fsin;     /* the request from address */
  
      nfds = MAX(tsock, usock) + 1;
      FD_ZERO(&amp;rfds);
      while (1) {
          FD_SET(tsock, &amp;rfds);
          FD_SET(usock, &amp;rfds);
          printf ("HELLO");
          if(select(nfds, &amp;rfds, NULL, NULL, NULL) &lt; 0){
              printf("select error: %d \n",errno);
              exit (1);
          }
          if(FD_ISSET(tsock, &amp;rfds))
          {
              /* TCP slave socket */
              printf ("Hello TCP");
              int ssock;
              //int alen = sizeof(fsin);
              ssock = accept(tsock, (struct sockaddr *) NULL, NULL);
              if(ssock &lt; 0)
                  Die("accept failed: jkjkjkjkjkj \n");
              HandleClient (ssock);
              close (ssock);
          }
          if(FD_ISSET(usock, &amp;rfds))
          {
              printf ("Hello UDP");
              HandleClient (usock);
          }
      }
  
  }
  void HandleClient(int comm_fd)
  {
      struct equation eq;
      struct result rslt;
      bzero (&amp;eq,sizeof (eq));
      bzero (&amp;rslt, sizeof (rslt));
      if ((readn (comm_fd, &amp;eq, sizeof(eq))) == 0){
          Die("Failed to receive from client");
      }
      printf ("\n%Lf  %Lf\n",sizeof (struct result),sizeof (rslt));
      printf ("reciveed %f %c %f",eq.a,eq.c,eq.b);
      switch (eq.c) {
          case '+':
              rslt.res = eq.a+eq.b;
              break;
          case '-':
              rslt.res = eq.a-eq.b;
              break;
          case '*':
              rslt.res = eq.a*eq.b;
              break;
          case '/':
              rslt.res = eq.a/eq.b;
              break;
          case '%':
              rslt.res = (int)eq.a% (int)eq.b;
              break;
          default:
              break;
      }
      printf ("\n%Lf\n",sizeof(rslt));
      printf ("\n%Lf\n",sizeof(rslt));
      printf ("\n%Lf\n",sizeof(rslt));
  
      writen (comm_fd, &amp;rslt, sizeof (rslt));
      //close (comm_fd);
  }
  </code></pre>
  
  <p>The client (just in case u guys need it)</p>
  
  <pre><code>#include &lt;sys/types.h&gt;
  #include &lt;sys/socket.h&gt;
  #include &lt;netdb.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;string.h&gt;
  #include &lt;stdlib.h&gt;
  #include&lt;unistd.h&gt;
  #include &lt;float.h&gt;
  #include &lt;errno.h&gt;
  
  struct equation {
      float a;
      char c;
      float b;
  };
  
  struct result {
      long double res;
  };
  
  int readn(int fd, void *vptr, size_t n)
  {
      size_t  nleft;
      ssize_t nread;
      char   *ptr;
      ptr = vptr;
      nleft = n;
      while (nleft &gt; 0) {
          if ( (nread = read(fd, ptr, nleft)) &lt; 0) {
              if (errno == EINTR)
                  nread = 0;      /* and call read() again */
              else
                  return (-1);
          } else if (nread == 0)
              break;              /* EOF */
  
          nleft -= nread;
          ptr += nread;
      }
      return (n - nleft);         /* return &gt;= 0 */
  }
  
  int writen(int fd, const void *vptr, size_t n)
  {
      size_t nleft;
      ssize_t nwritten;
      const char *ptr;
  
      ptr = vptr;
      nleft = n;
      while (nleft &gt; 0) {
          if ( (nwritten = write(fd, ptr, nleft)) &lt;= 0) {
              if (nwritten &lt; 0 &amp;&amp; errno == EINTR)
                  nwritten = 0;   /* and call write() again */
              else
                  return (-1);    /* error */
          }
  
          nleft -= nwritten;
          ptr += nwritten;
      }
      return (n);
  }
  
  int main(int argc,char *argv[])
  {
      int sockfd,n;
      char sendline[100];
      char recvline[100];
      struct sockaddr_in servaddr;
      if (argc != 3) {
          fprintf(stderr, "USAGE: ./HelloClient &lt;server_ip&gt; &lt;port&gt;\n");
          exit(1);
      }
  
      sockfd=socket(AF_INET,SOCK_DGRAM,0);
      bzero(&amp;servaddr,sizeof servaddr);
  
      servaddr.sin_family=AF_INET;
      servaddr.sin_port= htons(atoi(argv[2]));
  
      inet_pton(AF_INET,argv[1],&amp;(servaddr.sin_addr));
  
      connect(sockfd,(struct sockaddr *)&amp;servaddr,sizeof(servaddr));
      struct result rslt;
      struct equation eq;
      //while(1)
      //{
      printf ("Accepted values by the program, from %f to %f\n", FLT_MIN, FLT_MAX);
      printf ("\n possible operations are addition, substraction, division, multiplication and modulo with operators : +,-,*,/,% respectivly\n");
      printf ("\nPlease enter the equation in this form only : \"5.0 + 2.0\" with a single space\n");
      scanf ("%f %c %f", &amp;eq.a,&amp;eq.c,&amp;eq.b);
      if (eq.c!='+' &amp;&amp; eq.c!='-' &amp;&amp; eq.c!='*' &amp;&amp; eq.c!='/' &amp;&amp; eq.c!='%'){
          printf ("\n possible operations are addition, substraction, division, multiplication and modulo with operators : +,-,*,/,% respectivly\n");
          exit (1);
      }
      if (eq.c== '%'){
          if (!(eq.a == (float) ((int) eq.a) &amp;&amp; eq.b == (float) ((int) eq.b))){
              printf ("Only integer values are accepted with the % operation, please rerun the program\n");
              exit (1);
          }
      }
      //bzero( &amp;eq, sizeof(eq));
      bzero( &amp;rslt, sizeof(rslt) );
      //fgets(sendline,100,stdin); /*stdin = 0 , for standard input */
      writen (sockfd, &amp;eq, sizeof(eq));
  
      readn (sockfd, &amp;rslt, sizeof(rslt));
      printf("%Lf\n thank you for using this marvelous calculator!\n",rslt.res);
      bzero( &amp;eq, sizeof(eq));
      exit (1);
      //}
  }
  </code></pre>