#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>

typedef struct{
    int a;
    short s[2];
} MSG;


void originalCode() {

MSG *mp, m = {4, 1, 0};
char *fp, *tp;
mp = (MSG *) malloc(sizeof(MSG));
for (fp = (char *)m.s, tp = (char *)mp->s; tp < (char *)(mp+1);)
*tp++ = *fp++;

printf("ORIGINAL: new MSG:  mp.a:%d  mp.s[0]=%d  mp.s[1]=%d  \n",mp->a, mp->s[0], mp->s[1]);

}


int main (void) {
  MSG *mp, m = {4, 1, 8};
  mp = (MSG *) malloc(sizeof(MSG));
  char *fp, *tp;
  unsigned int loopCount=0;

  originalCode();

  mp = malloc (sizeof *mp);   /* allocate mem for one struct */
  for (fp = (char*)m.s, tp = (char*)mp->s;tp < (char*)(mp+1);) 
  {
        loopCount++;
        *tp++ = *fp++;          /* copy m.s to mp->s */
  };
  printf ("This code copies JUST the short array from one MSG to a new MSG -  loops %d times \n",loopCount);
  printf ("m.a   %d, m.s[0]   %hd, m.s[1]   %hd\n", m.a, m.s[0], m.s[1]);
  printf ("mp->a %d, mp->s[0] %hd, mp->s[1] %hd\n", mp->a, mp->s[0], mp->s[1]);

  return 0;
}

