title: Bubble sort of structures using pointers in C
body: <p>I want to sort an array of structures using the bubble sort algorithm and pointers in C. 
  I have a cars structure:</p>
  
  <pre><code>typedef struct{
      char model[30];
      int hp;
      int price;
  }cars;
  </code></pre>
  
  <p>and I allocate memory for 12 items:</p>
  
  <pre><code>cars *pointer = (cars*)malloc(12*sizeof(cars));
  </code></pre>
  
  <p>and read data from file:</p>
  
  <pre><code>for (i = 0; i &lt;number ; i++) {
      fscanf(file, "%s %i %i\n", (pointer+i)-&gt;model, &amp;(pointer+i)-&gt;hp, &amp;(pointer+i)-&gt;price);
  }
  </code></pre>
  
  <p>I pass pointer <code>ptr</code> to <code>bubbleSort</code> function:</p>
  
  <pre><code>bubbleSort(pointer, number);
  </code></pre>
  
  <p>Here is my <code>bubbleSort</code> function:</p>
  
  <pre><code>void bubbleSort(cars *x, int size) {
      int i, j;
      for (i=0;i&lt;size-1;i++) {
      int swapped = 0;
      for (j = 0; j &lt; size - 1 - i; j++) {
          if ( (x+i)-&gt;hp &gt; (x+j+1)-&gt;hp ) {
              cars *temp = (x+j+1);
              x[j+1] = x[j];
              x[j] = *temp;
              swapped = 1;
          }
      }
          if (!swapped) {
          //return;
          }
      }
  }
  </code></pre>
  
  <p>The problem is that I don't know how to swap items using pointers.</p>