title: memcpy in Abstract Data Type List Array based in C
body: <p>I'm currently struggling to understand how an array of structures works. For reference, here is the complete code. </p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;string.h&gt;
  #define SIZE 14
  /******************************************************************
   * Data Structure Definition                                      *
   ******************************************************************/
  typedef struct {
    unsigned int prodID;    /*  product ID, uniquely identifies an element */ 
    char prodDesc[50];      /*  product description*/  
    float prodPrice;        /*  product price*/  
    int prodQty;            /* product count or quantity  */
  }product, products[SIZE];                 /* product record */
  
  typedef struct cell {
     products item;             /* array of products */
     int prodCtr;           /* number of products in the list */
  }*chocoList;              /*  Definition of the ADT List ver 2 */  
  
  typedef enum {
      TRUE, FALSE
  }boolean;
  
  /******************************************************************
   * Function Prototypes                                            *
   ******************************************************************/
  void initializeList(chocoList *L);             
  void populateSortedList(chocoList L);         
  void displayList(chocoList L, char * listName);
  void displayProduct(product X);                 
  int insertSorted(chocoList L, product X);    
  product deleteFirstOccurence(chocoList L, float prodPrice);
  
  int main(void) 
  {
      chocoList  L;      /* sorted List  */
      product deleted;     /* container for the deleted record */
  
      /*---------------------------------------------------------------------------------
      * Problem #1 :: Initialize the choco list. Display the products in the list after* 
      * calling populateSortedList().                                                  *
      * printf("\n\n\nProblem #1:: ");                                                 *
      *--------------------------------------------------------------------------------*/
      printf("\n\n\nProblem #1:: ");
      initializeList(&amp;L); 
      populateSortedList(L);
      displayList(L, "Problem 1");
  
      /*---------------------------------------------------------------------------------
      * Problem #2 :: Delete 1 product from the list by calling deleteFirstOccurence(). * 
      * Display the returned product record by calling displayProduct().               *
      * printf("\n\n\nProblem #2:: ");                                                 *
      *--------------------------------------------------------------------------------*/
      printf("\n\n\nProblem #2:: ");     
      deleted = deleteFirstOccurence(L, 75.50);
      displayProduct(deleted);
  
      /*---------------------------------------------------------------------------------
      * CHALLENGE :: Redo Problem #s 1 &amp; 2 using either versions 3 or 4                       *
      * Keep in mind the changes when converting the ADT List Versions                 *
      *--------------------------------------------------------------------------------*/
  
      getch();
      return 0;
  }
  
  /****************************************************************************************
   * This function initializes the list for its first use.                                     *
   ****************************************************************************************/
  void initializeList(chocoList *L)
  {
      *L = (chocoList)malloc(sizeof(struct cell));
      (*L)-&gt;prodCtr = 0;
  }
  
  /****************************************************************************************
   * This function populates the list by calling insertSorted().                          *
   ****************************************************************************************/
  void populateSortedList(chocoList L)
  {
      int x, result = 1;
  
      product data[] = {  {1501, "Hersheys", 100.50, 10},
                                  {1502, "Hersheys Almond", 100.50, 15},
                                  {1503, "Hersheys Krackel", 100.50, 15},
                                  {1701, "Toblerone", 150.75, 20},
                                  {1702, "Toblerone Milk", 150.75, 40},
                                  {1703, "Toblerone Honey", 150.75, 10},
                                  {1550, "Cadbury", 200.00, 30},
                                  {1201, "Kitkat", 97.75, 40},
                                  {1450, "Ferrero", 150.50, 50},
                                  {1601, "Meiji", 75.50, 60},
                                  {1301, "Nestle", 124.50, 70},
                                  {1525, "Lindt", 175.50, 80},
                                  {1545, "Valor", 100.50, 90},
                                  {1455, "Tango", 49.50, 100}
                      };
      for(x = 0; x &lt; 10 &amp;&amp; result == 1; x++){
          result = insertSorted(L, data[x]);
      }                 
  }
  
  /****************************************************************************************
   * This function display the details of all products in the list.                       *
   ****************************************************************************************/            
  void displayList(chocoList L, char * listName) 
  {
      int x;
      system("CLS");       /* clears the screen before displaying succeeding lines */
  
      if(L-&gt;prodCtr != 0){
          printf("\nElements of the Product List %s:", listName);
          printf("\n\n%-10s%-15s%10s%10s","ID","Description","Price","Quantity");
          printf("\n%-10s%-15s%10s%10s","--","-----------","-----","--------");
  
          for(x = 0 ; x &lt; L-&gt;prodCtr; x++){
              printf("\n%-10d", L-&gt;item[x].prodID);
              printf("%-15s", L-&gt;item[x].prodDesc);   
              printf("%10.2f", L-&gt;item[x].prodPrice);
              printf("%10d", L-&gt;item[x].prodQty);     
          }
      }else{
          printf("\n\tChoco list is currently empty!\n");
      }
  
      printf("\n\n Press any key to continue . . . ");
      getch();
  }
  
  /****************************************************************************************
   * This function inserts a product X in an alphabetically sorted list according to its  *
   * product description. It returns a value of 1 for successful insertion. Otherwise, 0. *
   * Use memcpy() in shifting the elements downward to provide space for the new product. *
   ***************************************************************************************/
  int insertSorted(chocoList L, product X)
  {
      int ctr;
      if(L-&gt;prodCtr&lt;14){
          for(ctr=0; ctr&lt;L-&gt;prodCtr &amp;&amp; strcmp(L-&gt;item[ctr].prodDesc, X.prodDesc)&lt;0;ctr++){    }
          memcpy(L-&gt;item + ctr + 1, L-&gt;item + ctr, sizeof(product) * (L-&gt;prodCtr - ctr));
          L-&gt;item[ctr] = X;
          L-&gt;prodCtr++;
          ctr = 1;
      }
      else{
          ctr = 0;
      }
      return ctr;
  }
  
  /****************************************************************************************
   * This function deletes the first occurence of a product given the prodPrice.It returns* 
   * the product record ones it is found. Otherwise, it returns a dummy record containing *
   * "XXX" for string values and 0 for integer and float values.  Use memcpy() in shifting *
   * the elements upward to avoid having empty indices within the array.                       *
   ***************************************************************************************/
  product deleteFirstOccurence(chocoList L, float prodPrice)
  {
      int ctr;
      product dummy;
      strcpy(dummy.prodDesc , "XXX");
      dummy.prodID = 0;
      dummy.prodPrice = 0;
      dummy.prodQty = 0;
      for(ctr = 0; ctr&lt;L-&gt;prodCtr &amp;&amp; L-&gt;item[ctr].prodPrice != prodPrice; ctr++){ }
      if(ctr!=L-&gt;prodCtr){
          dummy = L-&gt;item[ctr];
          memcpy(L-&gt;item + ctr, L-&gt;item + ctr + 1, sizeof(product) * (L-&gt;prodCtr - ctr));
          L-&gt;prodCtr--;
      }
      return dummy;
  }
  
  /****************************************************************************************
   * This function display the details of 1 product.                                       *
   ***************************************************************************************/
  void displayProduct(product X)
  {
      //system("CLS");       /* clears the screen before displaying succeeding lines */
      printf("\n\nElements of Product %d:", X.prodID);
      printf("\n\n%-10s%-15s%10s%10s","ID","Description","Price","Quantity");
      printf("\n%-10s%-15s%10s%10s","--","-----------","-----","--------");
  
      printf("\n%-10d", X.prodID);
      printf("%-15s", X.prodDesc);    
      printf("%10.2f", X.prodPrice);
      printf("%10d", X.prodQty);      
  
      printf("\n\n Press any key to continue . . . ");
      getch();    
  }
  </code></pre>
  
  <p>I have a couple of questions with this part in particular:</p>
  
  <pre><code>int insertSorted(chocoList L, product X)
  {
      int ctr;
      if(L-&gt;prodCtr&lt;14){
          for(ctr=0; ctr&lt;L-&gt;prodCtr &amp;&amp; strcmp(L-&gt;item[ctr].prodDesc, X.prodDesc)&lt;0;ctr++){    }
          memcpy(L-&gt;item + ctr + 1, L-&gt;item + ctr, sizeof(product) * (L-&gt;prodCtr - ctr));
          L-&gt;item[ctr] = X;
          L-&gt;prodCtr++;
          ctr = 1;
      }
      else{
          ctr = 0;
      }
      return ctr;
  }
  </code></pre>
  
  <p>First of all, can someone explain to me this statement?</p>
  
  <pre><code>memcpy(L-&gt;item + ctr + 1, L-&gt;item + ctr, sizeof(product) * (L-&gt;prodCtr - ctr));
  </code></pre>
  
  <p>Second, is it more efficient to use <code>memcpy</code> or <code>memmove</code> in this scenario?</p>