Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I would have thought that the compiler would complain about parse taking a <code>char*</code> as its second argument, since you actually provide a <code>const char*</code> (i.e. a string constant). However, you actually try to copy the string, so you could have (and should have) made the prototype <code>const char*</code>.</p> <p>Let's focus on "try to copy". Leaving aside your obfuscated initialization, what you do is:</p> <pre><code> inputstr = malloc (sizeof(str)); strcpy(inputstr, str); </code></pre> <p>Before I go any further, I suggest you look at <code>strdup</code>, which does exactly what you want, only correctly:</p> <pre><code> inputstr = strdup(str); </code></pre> <p>No fuss, no errors. But anyway,</p> <p><code>str</code> is a <code>char*</code>; that is, a pointer to a character. (It should have been <code>const char*</code>, but it would still be a pointer to a character.) So <code>sizeof(str)</code> is the size of a pointer, which is likely to be four bytes on your platform, although it might be eight. Either way, it's not close to long enough to hold the string <code>"add,9898,Wings,Tasty Wings,14.99\n"</code>, so when you copy that into <code>inputstr</code>, you'll end up overwriting random memory. (And probably again with the <code>del</code> string).</p> <p><code>sizeof(*str)</code> would be the size of a character, which is one byte. What I think you might have meant was <code>strlen(str)</code>, which is the length of the string <code>str</code> in bytes, but that wouldn't be right either because it leaves out the terminating NUL byte.</p> <p>In short, what you wanted was the <code>strdup</code> call above, which is equivalent to:</p> <pre><code>char* inputstr = malloc(strlen(str) + 1); strcpy(inputstr, str); </code></pre>
    singulars
    1. This table or related slice is empty.
    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