title: C pointers : pointing to an array of fixed size
body: <p>This question goes out to the C gurus out there:</p>
  
  <p>In C, it is possible to declare a pointer as follows:</p>
  
  <pre><code>char (* p)[10];
  </code></pre>
  
  <p>.. which basically states that this pointer points to an array of 10 chars. The neat thing about declaring a pointer like this is that you will get a compile time error if you try to assign a pointer of an array of different size to p. It will also give you a compile time error if you try to assign the value of a simple char pointer to p. I tried this with gcc and it seems to work with ANSI, C89 and C99.</p>
  
  <p>It looks to me like declaring a pointer like this would be very useful - particularly, when passing a pointer to a function. Usually, people would write the prototype of such a function like this:</p>
  
  <pre><code>void foo(char * p, int plen);
  </code></pre>
  
  <p>If you were expecting a buffer of an specific size, you would simply test the value of plen. However, you cannot be guaranteed that the person who passes p to you will really give you plen valid memory locations in that buffer. You have to trust that the person who called this function is doing the right thing. On the other hand:</p>
  
  <pre><code>void foo(char (*p)[10]);
  </code></pre>
  
  <p>..would force the caller to give you a buffer of the specified size.</p>
  
  <p>This seems very useful but I have never seen a pointer declared like this in any code I have ever ran across. </p>
  
  <p>My question is: Is there any reason why people do not declare pointers like this? Am I not seeing some obvious pitfall?</p>