<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Alexander Kruel &#187; Alexander Kruel &#187; Programming</title>
	<atom:link href="http://kruel.co/category/programming-2/feed/" rel="self" type="application/rss+xml" />
	<link>http://kruel.co</link>
	<description>Thoughts and news on transhumanism, vegetarianism, science fiction, science, philosophy, math, programming, language, consciousness and the nature of reality.</description>
	<lastBuildDate>Mon, 25 Aug 2014 10:43:11 +0000</lastBuildDate>
	<language>en-US</language>
		<sy:updatePeriod>hourly</sy:updatePeriod>
		<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=4.0</generator>
	<item>
		<title>Learning C: fgets() and strstr()</title>
		<link>http://kruel.co/2013/01/16/learning-c-fgets-and-strstr/</link>
		<comments>http://kruel.co/2013/01/16/learning-c-fgets-and-strstr/#comments</comments>
		<pubDate>Wed, 16 Jan 2013 12:18:07 +0000</pubDate>
		<dc:creator><![CDATA[Alexander Kruel]]></dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[C]]></category>
		<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://kruel.co/?p=3852</guid>
		<description><![CDATA[I am currently reading Head First C and noticed that the following (slightly modified) example code is broken: #include &#60;stdio.h&#62; #include &#60;string.h&#62; char names[][80] = { &#34;David Bowie&#34;, &#34;Julia Roberts&#34;, &#34;George Bush&#34;, &#34;Robin Hanson&#34;, }; void find_name(char search_for[]) { int i; for (i = 0; i &#60; 4; i++) { if (strstr(names[i], search_for)) { printf(&#34;Name %i: &#039;%s&#039;\n&#34;, [&#8230;]]]></description>
				<content:encoded><![CDATA[<p>I am currently reading <a href="http://www.amazon.com/Head-First-C-David-Griffiths/dp/1449399916">Head First C</a> and noticed that the following (slightly modified) example code is broken:</p>
<pre class="brush: c; gutter: true">#include &lt;stdio.h&gt;
#include &lt;string.h&gt;

char names[][80] = {
    &quot;David Bowie&quot;,
    &quot;Julia Roberts&quot;,
    &quot;George Bush&quot;,
    &quot;Robin Hanson&quot;,
};

void find_name(char search_for[])
{
    int i;
    for (i = 0; i &lt; 4; i++) {
        if (strstr(names[i], search_for)) {
            printf(&quot;Name %i: &#039;%s&#039;\n&quot;, i, names[i]);
        }
    }

}

int main()
{
    char search_for[80];
    printf(&quot;Enter search term: &quot;);
    fgets(search_for, 80, stdin);

    find_name(search_for);

    return 0;
}</pre>
<p>So what&#8217;s wrong? Well,</p>
<blockquote><p>The <strong>fgets</strong> function reads characters from the stream up to and <span style="text-decoration: underline;" data-mce-mark="1">including a newline character</span> and stores them in the string s, adding a null character to mark the end of the string.</p></blockquote>
<p>A newline character makes fgets() stop reading and is included in the string. Which means that when you type &#8220;David Bowie&#8221; the search string will contain &#8220;David Bowie\n&#8221;, which does not equal names[0]:</p>
<p>{&#8216;D&#8217;, &#8216;a&#8217;, &#8216;v&#8217;, &#8216;i&#8217;, &#8216;d&#8217;, &#8216; &#8216;, &#8216;B&#8217;, &#8216;o&#8217;, &#8216;w&#8217;, &#8216;i&#8217;, &#8216;e&#8217;, &#8216;<strong>\n</strong>&#8216;, &#8216;\0&#8242;} does <em>not</em> equal {&#8216;D&#8217;, &#8216;a&#8217;, &#8216;v&#8217;, &#8216;i&#8217;, &#8216;d&#8217;, &#8216; &#8216;, &#8216;B&#8217;, &#8216;o&#8217;, &#8216;w&#8217;, &#8216;i&#8217;, &#8216;e&#8217;, &#8216;\0&#8242;}</p>
<p>The solution? Replace the newline character by a NULL character:</p>
<pre class="brush: c; gutter: true">size_t len = strlen(search_for) - 1;

if (search_for[len] == &#039;\n&#039;);
    search_for[len] = &#039;\0&#039;;</pre>
<p>This works because the NULL character marks the end of any character array that is a string. In other words, the string ends at the first null character.</p>
]]></content:encoded>
			<wfw:commentRss>http://kruel.co/2013/01/16/learning-c-fgets-and-strstr/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
