Note that there are some explanatory texts on larger screens.

plurals
  1. POA pointer/array issue in C
    text
    copied!<p>I have followed the following webpage:</p> <p><a href="http://www.1024cores.net/home/lock-free-algorithms/reader-writer-problem/improved-lock-free-seqlock" rel="nofollow">http://www.1024cores.net/home/lock-free-algorithms/reader-writer-problem/improved-lock-free-seqlock</a></p> <p>the source is like following:</p> <pre><code>struct data_t { int seq; // sequence number int data [1024]; // user data }; struct seqlock_t { data_t* current; data_t pool [16]; // 16 user objects are held }; seqlock_t sl; </code></pre> <p>The structure is quite simple, what confuses me is the following:</p> <pre><code>data_t* d0 = sl.current; // load-consume int idx = (sl.current - sl.pool + 1) % 16; data_t* d = &amp;sl.pool[idx]; </code></pre> <p>The <code>sl.current</code> is a pointer, <code>sl.pool</code> is? What current - pool can achieve? in <code>c language</code> view, how should I explain this statement? </p> <pre><code>int idx = (sl.current - sl.pool + 1) % 16; </code></pre> <p>Edit :</p> <p>Thanks for all information , it help a lot !!! in my own coding style would use int idx = (sl.current - &amp;[sl.pool[0]) + 1) % 16; now I know &amp;(sl.pool[0]) is the same with sl.pool !!! I should figure it out , the following example , what I read before , showes pointer/array ....</p> <pre><code>void display(int *p) { int idx ; printf("(%d)(%d)\n",*p,p[1]) ; } int main() { int i,Arr[10] ; for(i=0;i&lt;10;i++) Arr[i] = i + 100; display(Arr) ; } </code></pre> <p>Yes , I can use display(Arr) and display(&amp;Arr[0]) , they are the same ~!!</p>
 

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