Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Yes, that's exactly what <code>fwrite()</code> is. It uses a <code>const void *</code> which means "pointer to any type of data", and treats it as a pointer to bytes.</p> <p>So, your wrapper isn't really needed, you can just make a correctly-sized call to <code>fwrite()</code> right away to write the array:</p> <pre><code>const int my_ints[3] = { 1, 2, 3 }; const double my_doubles[5] = { 5, 4, 3, 2, 1 }; fwrite(my_ints, sizeof *my_ints, sizeof my_ints / sizeof *my_ints, stdout); fwrite(my_doubles, sizeof *my_doubles, sizeof my_doubles / sizeof *my_doubles, stdout); </code></pre> <p>Note that writing binary data to <code>stdout</code> can be somewhat rude, and also that I/O calls should be checked for errors.</p> <p>If you drop the "x elements of y bytes" model that <code>fwrite()</code>'s API uses, and just treat them as single chunks of bytes, you can simplify the calls:</p> <pre><code>fwrite(my_ints, sizeof my_ints, 1, stdout); fwrite(my_doubles, sizeof my_doubles, 1, stdout); </code></pre> <p>Note that there's no point in looping, the numbers in each array will be adjacent in memory so <code>fwrite()</code> can write them all at once. It's generally best to keep the number of I/O calls done down as much as possible, so if you can do a single write of <code>n * m</code> bytes instead of <code>n</code> writes of <code>m</code> bytes each, that's almost often better. Remember that that many of the underlying devices that you write to are block-oriented, and will benefit a lot from getting whole blocks of data since that's what they have to write, anyway.</p>
    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