Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><code>memcpy</code> does not allocate any memory. In your <code>memcpy</code> call, the memory for the destination <code>arr</code> was allocated when the variable <code>arr</code> was defined (<code>char arr[400]</code>).</p> <p>There's a problem there, which is that you haven't allocated enough room. You copy <code>sizeof(updata)</code> bytes into <code>arr</code>, which is probably 1+4+7*256=1797 (this may vary depending on <code>sizeof(int)</code> and on whether <code>__packed__</code> actually leaves out all unused bytes on your platform). If you really need <code>arr</code> (you probably don't), make it at least <code>sizeof(updata)</code> large. Defining it with <code>char arr[sizeof(updata)]</code> is fine.</p> <p>If the layout of the structure is defined by some external format, you should use a fixed-size type instead of <code>int</code> (which is 2 or 4 bytes depending on the platform, and could be other sizes but you're unlikely to encounter them).</p> <p>If the layout of the structure is defined by some external binary format and you want to print out the 1797 bytes in this format, use <code>fwrite</code>.</p> <pre><code>fwrite(updata, sizeof(updata), 1, stdout); </code></pre> <p>If you want to have a human representation of the data, use <code>printf</code> with appropriate format specifications.</p> <pre><code>printf("ip='%c' udp=%d\n", updata.ip, updata.ip); for (i = 0; i &lt; sizeof(updata.rules)/sizeof(updata.rules[0]); i++) { puts(updata.rules[i].myname); } </code></pre> <p>Despite the name, <code>char</code> is in fact the type of bytes. There is no separate type for characters in C. A character constant like <code>'a'</code> is in fact an integer value (97 on almost all systems, as per <a href="http://en.wikipedia.org/wiki/ASCII" rel="nofollow">ASCII</a>). It's things like writing it with <code>putchar</code> or <code>printf("%c", …)</code> that make the byte interpreted as a character.</p> <p>If your compiler is not signaling an error when you mix up a <em>pointer</em> (such as <code>char*</code>) with an integer, on the other hand, turn up the warning level. With Gcc, use at least <code>gcc -O -Wall</code>.</p> <hr> <p>After actually compiling your code, I see the main error (you should have copy-pasted the error message from the compiler in your question):</p> <pre><code>udpdata.rules[0].myname = "lalla\0" ; </code></pre> <p><code>udpdata.rules[0].myname</code> is an array of bytes. You can't assign to an array in C. You need to copy the elements one by one. Since this is an array of <code>char</code>, and you want to copy a string into it, you can use <code>strcpy</code> to copy all the bytes of the string. For a bunch of bytes in general, you would use <code>memcpy</code>.</p> <pre><code>strcpy(udpdata.rules[0].myname, "lalla"); </code></pre> <p>(Note that <code>"lalla\0"</code> is equivalent to <code>"lalla"</code>, all string literals are zero-terminated in C.¹) Since <code>strcpy</code> does not perform any size verification, you must make sure that the string (including its final null character) fits in the memory that you've allocated for the targets. You can use other functions such as <code>strncat</code> or <code>strlcpy</code> if you want to specify a maximum size.</p> <p>¹ <sub> There's one exception (and only this exception) where <code>"lalla"</code> won't be zero-terminated: when initializing an array of 5 bytes, e.g. <code>char bytes[5] = "lalla"</code>. If the array size is at least 6 or unspecified, there will be a terminating zero byte. </sub> </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