Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>This is probably a good example of why (in my opinion) it's a bad idea to <code>typedef</code> array types.</p> <p>Unlike in other contexts, in function declarations a parameter of array type is always adjusted to an equivalent pointer type. When an array is passed to the function it always decays into a pointer to the first element.</p> <p>These two snippets are equivalent:</p> <pre><code>typedef unsigned char Str[257]; Str src = "blah"; Str dst; memcpy( &amp;dst, &amp;src, sizeof(Str) ); // unconventional </code></pre> <hr> <pre><code>unsigned char src[257] = "blah"; unsigned char dst[257]; memcpy(&amp;dst, &amp;src, sizeof(unsigned char[257])); // unconventional </code></pre> <p>In this latter case <code>&amp;dst</code> and <code>&amp;src</code> are both of type <code>unsigned char (*)[257]</code> but the value of these pointers are the same as the value of pointers to the first element of each array, which is what <code>dst</code> and <code>src</code> would decay into if passed directly into <code>memcpy</code> like this.</p> <pre><code>memcpy(dst, src, sizeof(unsigned char[257])); // more usual </code></pre> <p><code>memcpy</code> takes <code>void*</code> arguments so the types of the original pointers don't matter, only their values.</p> <p>Because of the rule for parameter declarations (an array type of any or unspecified size is adjusted to the equivalent pointer type), these declarations for <code>fn</code> are all equivalent:</p> <pre><code>typedef unsigned char Str[257]; void fn( Str dst, Str src ); </code></pre> <hr> <pre><code>void fn( unsigned char dst[257], unsigned char src[257] ); </code></pre> <hr> <pre><code>void fn( unsigned char dst[], unsigned char src[] ); </code></pre> <hr> <pre><code>void fn( unsigned char* dst, unsigned char* src ); </code></pre> <p>Looking at this code, it is more obvious that the values being passed into <code>memcpy</code> in this case are pointers to the passed pointers, and not pointers to the actual <code>unsigned char</code> arrays.</p> <pre><code>// Incorrect void fn( unsigned char* dst, unsigned char* src ) { memcpy(&amp;dst, &amp;src, sizeof(unsigned char[257])); } </code></pre> <p>With a typedef, the error is not so obvious, but still present.</p> <pre><code>// Still incorrect typedef unsigned char Str[257]; void fn( Str dst, Str src ) { memcpy(&amp;dst, &amp;src, sizeof(Str)); } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

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