title: Report duplicates in array of structures using qsort?
body: <p>What I'm trying to accomplish:</p>
  
  <p>I am making 3000 requests and capturing 8 bytes of that request.  I stick the response in:</p>
  
  <pre><code>struct negs
  {
      int neg_num;
      char neg_key[9];
  };
  </code></pre>
  
  <p>where <code>neg_num == i</code> (the request number)
  and 
  <code>memcpy(nego[neg_count].neg_key, recv_data+73,8);</code></p>
  
  <p>I need to find any duplicate neg_keys in the nego struct.  I am trying to use qsort to accomplish this... this is my compare function:</p>
  
  <pre><code>int compare( const void* a, const void* b ){
      negs *ia = *(negs **)a;
      negs *ib = *(negs **)b;
      return memcmp(ia-&gt;neg_key, ia-&gt;neg_key, 8);
  }
  </code></pre>
  
  <p>and my call to qsort:</p>
  
  <pre><code>    printf("Sizeof = %d"), sizeof(*nego);
      qsort(nego,4,sizeof(*nego),compare);
      printf("Sizeof2 = %d"), sizeof(*nego);
  </code></pre>
  
  <p>I was planning on just seeing if there is a diff in sizeof after the call but this always returns:</p>
  
  <pre><code>Sizeof = 159218900
  Sizeof2 = 4
  </code></pre>
  
  <p>Am I approaching this the wrong way? any suggestions?</p>
  
  <hr>
  
  <h2>Here is the relevant code that is segfaulting:</h2>
  
  <pre><code>char current_key[9];
  int key_index = 0;
  int dupe_count = 0;
  
  typedef struct {
      int neg_num;
      char neg_key[9];
  } negs;
  
  struct negs
  {
      int neg_num;
      char neg_key[9];
  };
  
  
  
  ...
  
  int compare( const void* a, const void* b ){
      negs *ia = *(negs **)a;
      negs *ib = *(negs **)b;
      return memcmp(ia-&gt;neg_key, ia-&gt;neg_key, 8);
  }
  
  
  ...
  
  
  main(int argc, char **argv) {
      char send_smbNego[] = {
  0x00,0x00,0x00,0x54,0xFf,0x53
          };
  
      int neg_count = 0;
      int neg_max = 3000;
  
      struct negs *nego;
  
  
  
      nego = malloc(neg_max * sizeof(struct negs));
      for (neg_count = 0 ; neg_count &lt; neg_max ; neg_count++){
                  if ((sock = open_socket(target, TCP, 445))&gt;=0) {
                          send(sock, send_smbNego, sizeof(send_smbNego), 0);
                          len = recv(sock, recv_data, OUTBUF_LEN, 0);
              if (len &gt; 81) { // This should be changed to look for SMB header: ff534d42 followed by 72 which is SMB Command Negotiate Protocol, followed by 00000000 which indicates success.  The encryption key or challenge token is at offset 73 and is 8 bytes long;
                  nego[neg_count].neg_num = neg_count;
                  memcpy(current_key, recv_data+73,8);                   
                  memcpy(nego[neg_count].neg_key, recv_data+73,8);
                  //print_hex("Key 1 = ",nego[neg_count].neg_key,8);
                  //printf("\n");
              }
  
      close(sock);
  
      }
      print_hex("Key number 0",nego[0].neg_key,8);
      printf("Sizeof = %d"), sizeof(*nego);
      qsort(nego,4,sizeof(*nego),compare);
      printf("Sizeof2 = %d"), sizeof(*nego);
  
  
  
  }
  }
  </code></pre>