Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I presume you know what the code is supposed to do. The function has a very <a href="http://en.wikipedia.org/wiki/Boyer%E2%80%93Moore_string_search_algorithm" rel="nofollow">evocative</a> <a href="http://en.wikipedia.org/wiki/Boyer-Moore-Horspool_algorithm" rel="nofollow">name</a>.</p> <p>So, addressing the queries in code directly, without analysing the code too much (it's incomplete):</p> <p><code>search</code> is a pointer. Not the string itself, but a pointer to that string. One might expect <code>string</code> to be pointing the start of some string, and <code>search</code> is set to point to the same value.</p> <p><code>skipTable[*search++] = i;</code> <em>dereferences</em> the <em>pointer</em>, obtaining the value at whatever <code>search</code> was pointing at, and then increments the pointer. Since <code>skipTable</code> is an array of 255 ints and <code>search</code> is a char pointer, this will never fall out of bounds on x86 if a char is unsigned (compiler dependent). Really, there should be a cast; <code>skipTable[(unsigned char) *search++]</code>. (strictly speaking, this still isn't portable).</p> <p>So <code>i</code> is set to the value at <code>skipTable[*search]</code> and <code>search</code> (which is a pointer) is incremented (presumably to point to the next value in some array).</p> <p><code>lastChar = *--search;</code> is similar to before. <code>search</code> is decremented and then dereferenced. So this gets the <em>previous</em> value in the string.</p> <p><code>search += (strLength - i + 1);</code> again, a pointer is being increased. If <code>strLength</code> represents the length of a string</p> <p>As has been pointed in out in comments, the following is not a good idea: <code>char *data ="nice bad good worst";</code> The string you've defined in code cannot be modified, but because you have a pointer to it, you're allowed to try. The result of trying to modify the string pointed to be <code>data</code> is undefined, but will almost invariably result in a crash.</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. This table or related slice is empty.
    1. 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