Note that there are some explanatory texts on larger screens.

plurals
  1. PO2 Dimensional Array Pointer
    text
    copied!<p>I am following an example to implement threads in C: <a href="http://ramcdougal.com/threads.html" rel="nofollow">http://ramcdougal.com/threads.html</a>. This example is using a 1-dimensional array. I need a dynamic 2 dimensional array.</p> <p>What would it look like if in the <code>main()</code> it was <code>int **array</code> instead of <code>int array[ARRAYSIZE]</code>?</p> <p>My problem is how to pass a pointer to a 2-dimensional array to the struct. The idea is, that I have a big array, and each thread should only fill a certain area of that array.</p> <p>Thanks a lot !</p> <p>Here's the code from the example:</p> <pre><code>struct ThreadData { int start, stop; int* array; }; void* squarer(struct ThreadData* td) { struct ThreadData* data=(struct ThreadData*) td; int start=data-&gt;start; int stop=data-&gt;stop; int* array=data-&gt;array; int i; for (i=start; i&lt;stop; i++) { array[i]=i*i; } return NULL; } int main(void) { int array[ARRAYSIZE]; pthread_t thread[NUMTHREADS]; struct ThreadData data[NUMTHREADS]; int i; int tasksPerThread=(ARRAYSIZE+NUMTHREADS-1)/NUMTHREADS; for (i=0; i&lt;NUMTHREADS; i++) { data[i].start=i*tasksPerThread; data[i].stop=(i+1)*tasksPerThread; data[i].array=array; } /* the last thread must not go past the end of the array */ data[NUMTHREADS-1].stop=ARRAYSIZE; /* Launch Threads */ for (i=0; i&lt;NUMTHREADS; i++) { pthread_create(&amp;thread[i], NULL, squarer, &amp;data[i]); } /* Wait for Threads to Finish */ for (i=0; i&lt;NUMTHREADS; i++) { pthread_join(thread[i], NULL); } /* Display Result */ for (i=0; i&lt;ARRAYSIZE; i++) { printf("%d ", array[i]); } printf("\n"); return 0; } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload