<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	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/"
		>
<channel>
	<title>Comments on: Pointers, arrays, and string literals</title>
	<atom:link href="http://www.artfulcode.net/articles/pointers-arrays-and-string-literals/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.artfulcode.net/articles/pointers-arrays-and-string-literals/</link>
	<description>Resources and tips for dynamic, interactive languages.</description>
	<lastBuildDate>Tue, 07 Sep 2010 20:27:19 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
	<item>
		<title>By: hitechnical</title>
		<link>http://www.artfulcode.net/articles/pointers-arrays-and-string-literals/comment-page-1/#comment-1928</link>
		<dc:creator>hitechnical</dc:creator>
		<pubDate>Mon, 05 Oct 2009 12:16:48 +0000</pubDate>
		<guid isPermaLink="false">http://www.artfulcode.net/?p=698#comment-1928</guid>
		<description>It will be great if you elaborate your article with pointer declarations like this,

char *string = { &quot;This is a string&quot; }

I was not sure how to pass-by-reference a string like this and also the const-correctness issue.</description>
		<content:encoded><![CDATA[<p>It will be great if you elaborate your article with pointer declarations like this,</p>
<p>char *string = { &#8220;This is a string&#8221; }</p>
<p>I was not sure how to pass-by-reference a string like this and also the const-correctness issue.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jeff</title>
		<link>http://www.artfulcode.net/articles/pointers-arrays-and-string-literals/comment-page-1/#comment-1700</link>
		<dc:creator>Jeff</dc:creator>
		<pubDate>Tue, 28 Jul 2009 16:29:42 +0000</pubDate>
		<guid isPermaLink="false">http://www.artfulcode.net/?p=698#comment-1700</guid>
		<description>You too, Ian. Have you looked at the &lt;a href=&quot;http://sourceforge.net/projects/safeclib/&quot; rel=&quot;nofollow&quot;&gt;safe c library&lt;/a&gt;? It contains some very well-written string functions. The code is extremely clean and readable, too.</description>
		<content:encoded><![CDATA[<p>You too, Ian. Have you looked at the <a href="http://sourceforge.net/projects/safeclib/" rel="nofollow">safe c library</a>? It contains some very well-written string functions. The code is extremely clean and readable, too.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Ian</title>
		<link>http://www.artfulcode.net/articles/pointers-arrays-and-string-literals/comment-page-1/#comment-1698</link>
		<dc:creator>Ian</dc:creator>
		<pubDate>Mon, 27 Jul 2009 04:13:24 +0000</pubDate>
		<guid isPermaLink="false">http://www.artfulcode.net/?p=698#comment-1698</guid>
		<description>It can be even more insidious than that, in fact.  There are really 4 possibilities, and 3 look quite similar.

&lt;code&gt;
        char a[] = &quot;This is a test&quot;;    // immutable pointer to mutable memory
        char *b = &quot;this is another test&quot;;  // mutable pointer to immutable memory
        char *c = malloc(256); // mutable pointer, mutable memory
        const char *const d = &quot;this is the fourth test&quot;;  // immutable pointer, immutable memory  -- really, really constant
        strcpy(c, &quot;this is yet another test&quot;);

        a[2] = &#039;I&#039;;    // compiles, works
        b[2] = &#039;I&#039;;    // compiles, Fails!
        c[2] = &#039;I&#039;;    // compiles, works
        d[2] = &#039;I&#039;;    // will not compile
        
        a = b;  // compile error
        c = a;  // Legal, works
        d = c;  // compile error        

&lt;/code&gt;

This also touches on the fact that saying const char *foo gives you a constant &lt;strong&gt;value.&lt;/strong&gt;  You can&#039;t change the thing it points to, but the pointer itself is fair game.  Strings in C are a minefield of trickiness.

By the way, it was nice meeting you at PyOhio this weekend.</description>
		<content:encoded><![CDATA[<p>It can be even more insidious than that, in fact.  There are really 4 possibilities, and 3 look quite similar.</p>
<p><code><br />
        char a[] = "This is a test";    // immutable pointer to mutable memory<br />
        char *b = "this is another test";  // mutable pointer to immutable memory<br />
        char *c = malloc(256); // mutable pointer, mutable memory<br />
        const char *const d = "this is the fourth test";  // immutable pointer, immutable memory  -- really, really constant<br />
        strcpy(c, "this is yet another test");</p>
<p>        a[2] = 'I';    // compiles, works<br />
        b[2] = 'I';    // compiles, Fails!<br />
        c[2] = 'I';    // compiles, works<br />
        d[2] = 'I';    // will not compile</p>
<p>        a = b;  // compile error<br />
        c = a;  // Legal, works<br />
        d = c;  // compile error        </p>
<p></code></p>
<p>This also touches on the fact that saying const char *foo gives you a constant <strong>value.</strong>  You can&#8217;t change the thing it points to, but the pointer itself is fair game.  Strings in C are a minefield of trickiness.</p>
<p>By the way, it was nice meeting you at PyOhio this weekend.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jeff</title>
		<link>http://www.artfulcode.net/articles/pointers-arrays-and-string-literals/comment-page-1/#comment-1644</link>
		<dc:creator>Jeff</dc:creator>
		<pubDate>Fri, 26 Jun 2009 11:33:07 +0000</pubDate>
		<guid isPermaLink="false">http://www.artfulcode.net/?p=698#comment-1644</guid>
		<description>Often, it&#039;s because a language is developed organically rather than top-down. That is especially true for lisp, which was conceived of more than half a century ago when the available alternatives were fortran, assembly, and punched cards. CAR and CDR are much simpler than their equivalent forms in assembly. From http://www.statemaster.com/encyclopedia/Car-and-cdr:

  The 704 assembler macro for cdr was
  LXD JLOC,4
  CLA 0,4
  PDX 0,4
  PXD 0,4[1]</description>
		<content:encoded><![CDATA[<p>Often, it&#8217;s because a language is developed organically rather than top-down. That is especially true for lisp, which was conceived of more than half a century ago when the available alternatives were fortran, assembly, and punched cards. CAR and CDR are much simpler than their equivalent forms in assembly. From <a href="http://www.statemaster.com/encyclopedia/Car-and-cdr" rel="nofollow">http://www.statemaster.com/encyclopedia/Car-and-cdr</a>:</p>
<p>  The 704 assembler macro for cdr was<br />
  LXD JLOC,4<br />
  CLA 0,4<br />
  PDX 0,4<br />
  PXD 0,4[1]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Andrew Pennebaker</title>
		<link>http://www.artfulcode.net/articles/pointers-arrays-and-string-literals/comment-page-1/#comment-1627</link>
		<dc:creator>Andrew Pennebaker</dc:creator>
		<pubDate>Thu, 25 Jun 2009 20:18:08 +0000</pubDate>
		<guid isPermaLink="false">http://www.artfulcode.net/?p=698#comment-1627</guid>
		<description>Oh wow. I know about these kinds of things generally, which makes reading/writing C easier. But I thought *char vs char[] was simply a syntax nicety. Thanks for clearing that up. Explicit pointer syntax is stupid. Do language developers ever think about these kinds of easily solved syntax problems? I think it&#039;s disastrous that Lisp uses CAR/CDR instead of FIRST/REST. These are issues that newbies discover every time they are introduced to a language. How come engineers with PhDs can&#039;t imagine them?</description>
		<content:encoded><![CDATA[<p>Oh wow. I know about these kinds of things generally, which makes reading/writing C easier. But I thought *char vs char[] was simply a syntax nicety. Thanks for clearing that up. Explicit pointer syntax is stupid. Do language developers ever think about these kinds of easily solved syntax problems? I think it&#8217;s disastrous that Lisp uses CAR/CDR instead of FIRST/REST. These are issues that newbies discover every time they are introduced to a language. How come engineers with PhDs can&#8217;t imagine them?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Sara</title>
		<link>http://www.artfulcode.net/articles/pointers-arrays-and-string-literals/comment-page-1/#comment-1594</link>
		<dc:creator>Sara</dc:creator>
		<pubDate>Wed, 24 Jun 2009 03:14:46 +0000</pubDate>
		<guid isPermaLink="false">http://www.artfulcode.net/?p=698#comment-1594</guid>
		<description>Pretty nice post. I just came across your site and wanted to say 
that I have really enjoyed reading your blog posts. Any way 
I&#039;ll be subscribing to your feed and I hope you write again soon!</description>
		<content:encoded><![CDATA[<p>Pretty nice post. I just came across your site and wanted to say<br />
that I have really enjoyed reading your blog posts. Any way<br />
I&#8217;ll be subscribing to your feed and I hope you write again soon!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jeff</title>
		<link>http://www.artfulcode.net/articles/pointers-arrays-and-string-literals/comment-page-1/#comment-1559</link>
		<dc:creator>Jeff</dc:creator>
		<pubDate>Mon, 22 Jun 2009 12:33:45 +0000</pubDate>
		<guid isPermaLink="false">http://www.artfulcode.net/?p=698#comment-1559</guid>
		<description>I&#039;ve updated the function, putting the null terminator test at the beginning of the loop.</description>
		<content:encoded><![CDATA[<p>I&#8217;ve updated the function, putting the null terminator test at the beginning of the loop.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Tordek</title>
		<link>http://www.artfulcode.net/articles/pointers-arrays-and-string-literals/comment-page-1/#comment-1513</link>
		<dc:creator>Tordek</dc:creator>
		<pubDate>Fri, 19 Jun 2009 21:33:33 +0000</pubDate>
		<guid isPermaLink="false">http://www.artfulcode.net/?p=698#comment-1513</guid>
		<description>There&#039;s another subtle bug on strtoupper:

int a[] = &quot;&quot;;
a = strtoupper(a);

will break. The pointer is not null (so, it passes the null check), but then inside the do, the first () char gets toupper&#039;ed, which is not a problem. On the while check, the pointer is advanced firs, and checked second, so the (only)  of the string isn&#039;t seen.

A correct implementation would be as Michael wrote, checking the content first, and stepping forward last.

Also, a better (IMHO) way to handle errors is to return as early as possible, like so:

char* strtoupper(char *str)
{

if(NULL == str) {
  return NULL;
}

char *start = str;

while (*str) {
*str = toupper(*str);

str++;
}

return start;
}

(I&#039;ve separated the assigning and the increasing, even though it&#039;s not necessary, to make it an itty bit cleaner.)</description>
		<content:encoded><![CDATA[<p>There&#8217;s another subtle bug on strtoupper:</p>
<p>int a[] = &#8220;&#8221;;<br />
a = strtoupper(a);</p>
<p>will break. The pointer is not null (so, it passes the null check), but then inside the do, the first () char gets toupper&#8217;ed, which is not a problem. On the while check, the pointer is advanced firs, and checked second, so the (only)  of the string isn&#8217;t seen.</p>
<p>A correct implementation would be as Michael wrote, checking the content first, and stepping forward last.</p>
<p>Also, a better (IMHO) way to handle errors is to return as early as possible, like so:</p>
<p>char* strtoupper(char *str)<br />
{</p>
<p>if(NULL == str) {<br />
  return NULL;<br />
}</p>
<p>char *start = str;</p>
<p>while (*str) {<br />
*str = toupper(*str);</p>
<p>str++;<br />
}</p>
<p>return start;<br />
}</p>
<p>(I&#8217;ve separated the assigning and the increasing, even though it&#8217;s not necessary, to make it an itty bit cleaner.)</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jeff</title>
		<link>http://www.artfulcode.net/articles/pointers-arrays-and-string-literals/comment-page-1/#comment-1510</link>
		<dc:creator>Jeff</dc:creator>
		<pubDate>Fri, 19 Jun 2009 14:35:22 +0000</pubDate>
		<guid isPermaLink="false">http://www.artfulcode.net/?p=698#comment-1510</guid>
		<description>Thanks. I added a check for null pointers.</description>
		<content:encoded><![CDATA[<p>Thanks. I added a check for null pointers.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Michael</title>
		<link>http://www.artfulcode.net/articles/pointers-arrays-and-string-literals/comment-page-1/#comment-1509</link>
		<dc:creator>Michael</dc:creator>
		<pubDate>Fri, 19 Jun 2009 14:15:24 +0000</pubDate>
		<guid isPermaLink="false">http://www.artfulcode.net/?p=698#comment-1509</guid>
		<description>You have a bug in strtoupper() -- it will crash if NULL is passed in. i.e. strtoupper(NULL);

This is a more type safe version:
void strtoupper(char *str)
{
 if( str )
  while (*str)
  {
   *str++ = toupper(*str);
  }
}</description>
		<content:encoded><![CDATA[<p>You have a bug in strtoupper() &#8212; it will crash if NULL is passed in. i.e. strtoupper(NULL);</p>
<p>This is a more type safe version:<br />
void strtoupper(char *str)<br />
{<br />
 if( str )<br />
  while (*str)<br />
  {<br />
   *str++ = toupper(*str);<br />
  }<br />
}</p>
]]></content:encoded>
	</item>
</channel>
</rss>
