Hi,

My job is to read some image data, and add some appropriate hex data. I stripped this code to this form because much of the code is irrelevant to my problem. I have multiple structures, and behind each one is a variable length string. Also, the output may contain two or three of these structures. I malloc the area where this data is constructed. Then I try to assign the first structure to point to this area. Then I add the variable length data via memcpy. Then I want to make the next structure point to the adjacent byte to the previous, add its fixed length part, then variable, then third structure if applicable. I need to know the total length (always less than 256) so it can be written.

Here is the "reduced version" where I have attempted to illustrate my problem while deleting lots of nasty details. This is very convoluted because the data will be sent to another platform. The structures were created because I had a difficult time defining all the hex constants.

I do realize this code will not compile. I just need to know how to address my data.

Code:
   #include <stdio.h>
   #include <string.h>   
   
   main(argc, argv)
   int argc;
   char *argv[];

   {    
   
     struct TLE_hdr {          /* hdr  */
         char x5A;
         char len1;
         char len2;
         char xD3;
         char xA0;
         char x90;
         char h10;
         char h20;
         char h30;       
     };
   
     struct TLE_search {         /* variable name */
         char slen;       
         char styp1;
         char styp2;
         char h40;
         char sname[10];
     };
     
     struct TLE_data {         /* data name */
         char dlen;       
         char dtyp;
         char h50;  
         char h60; 
         char dname[10];
     };  

       struct TLE_hdr *ptrh1, hdr1 =
           { 0x5A,0x00,0x00,0xD3,0xA0,0x90,0x00,0x00,0x00 };

       struct TLE_search *ptrs1, search1 =
           { 0x08,0x02,0x0B,0x00, "NAME" };        

       struct TLE_search *ptrs2, search2 =
           { 0x0B,0x02,0x0B,0x00, "ACCTNUM" };

       struct TLE_data *ptrc1, custdata =
           { 0x00,0x36,0x00,0x00 };  /* no initial account number or name*/

       struct TLE_data  *ptra1, acctdata =
           { 0x12,0x36,0x00,0x00 };  /* 14 byte acctno + 4 = 18 */

       char *cname[10];
       char *cacct[10];            

       char aname[5] = "NAME"; 
       char ename[5];
       char aacct[8] = "ACCTNUM";
       char eacct[8];

       char xlen;      
  
       FILE *fpout;       
      
       int tot_len = 0;
       int rc = 0;            

       char *buffer[255];
       
       char david[] = "David";
       char goliath[] = "Goliath";       
      
       printf ("Demo Version 1.0\n");
       printf ("%s Parm 0\n", argv[0]);
       printf ("%s Parm 1\n", argv[1]);
       
       if ((fpout = fopen(argv[1], "wb")) == NULL)
           {
           printf("Can not open output file!\n");
           return 4;
       }

       /* -------------------------------------------------*/
          
       *buffer =  malloc(256);
       
       ptrh1 = (struct TLE_hdr *) buffer;     

       memcpy(ptrh1, &hdr1, 9);               
       
       ptrs1 = ptrh1 + sizeof(hdr1);
       memcpy(ptrs1, &search1, sizeof(search1));

       ptrc1 = ptrs1 + sizeof(search1);               
       memcpy(ptrc1, &custdata, sizeof(custdata));
      
       cname = ptrc1 + sizeof(custdata);                
       memcpy(cname, &david, strlen(david) - 1);
               
       memcpy((ptrc1).dlen, &xlen, 1);  /* length */             

       ptra1 = ptrc1 + strlen(david) - 1;       

       tot_len = cacct + 13 - buffer;
                                                         
       memcpy((ptra1).name, &goliath, strlen(goliath) - 1);  /* length */

       tot_len = ptra1 + strlen(goliath) - 1 - buffer;

       /* ----------------------------------------------------- */
            
       printf("Total length is: %d \n", tot_len);
                                       
       fwrite(*buffer, tot_len + 1, 1, fpout);

       free(buffer);

       close(fpout);

       return 0;
  )
I know part of my problem is knowing when to use *var versus var, and using cast to make the compiler know I understand the bytes.