title: Finding the square sum of an array with pthreads in C
body: <p>What I need to do is calculate the square sum of an array. The array and the number of threads must be given by the user, and we assume the number of elements and the number of threads are integrally divided.</p>
  
  <p>My code doesn't even compile.. I know I have issues with the pointers, and I should place mutexes but I don't know where exactly. Can you point me to the right direction? I will appreciate it a lot.</p>
  
  <pre><code>#include &lt;pthread.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;unistd.h&gt;
  
  int part = 0;
  int local_elements = 0;
  
  void* square_sum(void* arg)
  {
      int local_sum = 0;
      int *element_array[] = (int *)arg;
  
      //Each thread computes its part
      int thread_part = part++;
  
      for (int i = thread_part * loc_elements; i &lt; (thread_part + 1)*loc_elements; i++) {
          local_sum += element_array[i] * element_array[i];
      }
  
      return (void*)&amp;local_sum;
  }
  
  main()
  {
      int threads, total_elements;
      int i;
      int total_sum = 0;
  
      printf("Give the number of threads: ");
      scanf("%d", &amp;threads);
  
      printf("Give the number of the elements you want: ");
      scanf("%d", &amp;total_elements;)
          int element_array[total_elements];
  
      //How many elements each thread gets
      local_elements = total_elements / threads;
  
      printf("Give the %d elements \n", total_elements);
      for (i = 0; i &lt; total_elements; i++) {
          printf("No. %d:", i);
          scanf("%d", &amp;element_array[i]);
      }
  
      pthread_t newthread[threads];
      //Creating the threads
      for (int i = 0; i &lt; threads; i++) {
          //The start routine gets the whole element_array
          pthread_create(&amp;newthread[i], NULL, square_sum, &amp;element_array);
      }
  
      int loc_sum;
      //Waiting for each thread to finish and creating the total_sum
      for (int i = 0; i &lt; threads; i++) {
          pthread_join(newthread[i], &amp;loc_sum);
          total_sum += loc_sum;
      }
  
      printf("The total sum is: %d", &amp;total_sum);
  }
  </code></pre>