Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The standard <a href="http://linux.die.net/man/2/write" rel="nofollow noreferrer"><code>write</code></a> function accepts a <em>pointer</em> to what you want to write out; you're passing it a <code>char</code> (which is being promoted to an <code>int</code>).</p> <p>The minimal fix would be to change this:</p> <pre><code>write (socket, bmpheader[i], 1); </code></pre> <p>to</p> <pre><code>write (socket, &amp;bmpheader[i], 1); </code></pre> <p>...but there are better ways.</p> <p>You can just let <code>write</code> write out all 54 bytes itself:</p> <pre><code>write (socket, bmpheader, 54); </code></pre> <p>Or if you really want to do it a <code>char</code> at a time, you could use a pointer:</p> <pre><code>int i; char *p; for (i = 0, p = bmpheader; i &lt; 54; ++i, ++p) { printf("%c", *p); write (socket, p, 1); } </code></pre> <p>In either case, you want to avoid that magic number <code>54</code> and take it from the data. Since <code>sizeof(char)</code> is <a href="https://stackoverflow.com/questions/2215445/are-there-machines-where-sizeofchar-1">always one byte in standard C</a>, you can use <code>sizeof</code> without doing the ugly <code>sizeof(array) / sizeof(array[0])</code> thing:</p> <pre><code>int i; char *p; for (i = 0, p = bmpheader; i &lt; sizeof(bmpheader); ++i, ++p) { printf("%c", *p); write (socket, p, 1); } </code></pre> <p>...but I'm sure there's a more elegant solution than that.</p> <p>You can also use <code>putchar</code> (fairly lightweight) instead of <code>printf</code> (fairly heavyweight):</p> <pre><code>int i; char* p; for (i = 0, p = bmpheader; i &lt; sizeof(bmpheader); ++i, ++p) { putchar(*p); write (socket, p, 1); } </code></pre> <p>And I used to do things like this with pointer arithmetic, no idea whether they're bad practice in the modern world:</p> <pre><code>char* p; char* endp; for (p = bmpheader, p = bmpheader + sizeof(bmpheader); p &lt; endp; ++p) { putchar(*p); write (socket, p, 1); } </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