title: Sort a array of array of integers lexicographically
body: <p>I'm using qsort() to sort a 2D array of integers lexiographically, each row being one input, but having problems with pointer arithmetic when traversing a row in the array.  </p>
  
  <p>Idea is to concatenate each column in one row into a string, and <code>strcmp</code> with other strings computed similarly from other rows.</p>
  
  <p>Input is restricted to positive integers only. Some sort examples below,<br>
  <code>{1, 1, 10}</code> before <code>{1, 2, 0}</code><br>
  <code>{3, 1, 5}</code> before <code>{3, 11, 5}</code><br>
  <code>{1, 19, 2}</code> before <code>{1, 2, 1}</code></p>
  
  <pre><code>#define COL_SIZE 3  
  
  int cmp_func (const void *a, const void *b) {
      char str1[100], str2[100], temp[10];
      int i;
      const int **ia = (const int **)a;
      const int **ib = (const int **)b;
  
      printf("(a: %p), (b: %p)\n", a, b);
      str1[0] = '\0';
      str2[0] = '\0';
  
      for (i = 0; i &lt; COL_SIZE; i++) {
          printf("(ia+%d: %p), (ib+%d: %p)\n", i, (ia + i), i, (ib + i));
          sprintf(temp, "%d", (int)*(ia + i));
          strcat(str1, temp);
  
          sprintf(temp, "%d", (int)*(ib + i));
          strcat(str2, temp);
      }
  
      printf("a: %s, b:%s\n", str1, str2);
      return(strcmp(str1, str2));
  }
  
  int main (void) {
      int i, len;
      int A[][COL_SIZE] = {{1,2,3},
                           {1,1,5},
                           {1,0,1},
                           {1,2,1},
                           {1,2,0}};
  
      len = sizeof(A)/sizeof(A[0]);
      printf("sizeof(int): %ld, sizeof(int *): %ld\n", sizeof(int), sizeof(int *));
  
      for (i = 0; i &lt; len; i++) {
          printf("Address of A[%d][0]: %p\n", i, A[i]);
      }
  
      qsort(A, len, COL_SIZE * sizeof(int *), cmp_func);
      return 0;
  }
  </code></pre>
  
  <p>Below is the output:  </p>
  
  <pre><code>sizeof(int): 4, sizeof(int *): 8  
  Address of A[0][0]: 0x7fff58e9fb30  
  Address of A[1][0]: 0x7fff58e9fb3c  
  Address of A[2][0]: 0x7fff58e9fb48
  Address of A[3][0]: 0x7fff58e9fb54  
  Address of A[4][0]: 0x7fff58e9fb60  
  (a: 0x7fff58e9fb30), (b: 0x7fff58e9fb48)  
  (ia+0: 0x7fff58e9fb30), (ib+0: 0x7fff58e9fb48)  
  (ia+1: 0x7fff58e9fb38), (ib+1: 0x7fff58e9fb50)  
  (ia+2: 0x7fff58e9fb40), (ib+2: 0x7fff58e9fb58)  
  a: 131, b:112  
  (a: 0x7fff58e9fb48), (b: 0x7fff58e9fb60)  
  (ia+0: 0x7fff58e9fb48), (ib+0: 0x7fff58e9fb60)  
  (ia+1: 0x7fff58e9fb50), (ib+1: 0x7fff58e9fb68)  
  (ia+2: 0x7fff58e9fb58), (ib+2: 0x7fff58e9fb70)  
  Abort trap: 6
  </code></pre>
  
  <p>The pointer arithmetic for <code>*(ia + 1)</code> is causing the address in each iteration to jump by 8 <code>(sizeof(int *))</code> from <code>0x7fff58e9fb30</code> to <code>0x7fff58e9fb38</code>, whereas the next value in A[0] is stored at <code>0x7fff58e9fb34</code> (size of int).  </p>
  
  <p>How do I fix this so that I can get an offset of 4 instead of 8?</p>