<?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: Are pointers and arrays equivalent in C?</title>
	<atom:link href="http://eli.thegreenplace.net/2009/10/21/are-pointers-and-arrays-equivalent-in-c/feed/" rel="self" type="application/rss+xml" />
	<link>http://eli.thegreenplace.net/2009/10/21/are-pointers-and-arrays-equivalent-in-c/</link>
	<description>Eli Bendersky's personal website</description>
	<lastBuildDate>Wed, 10 Mar 2010 17:32:25 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>By: Sachin</title>
		<link>http://eli.thegreenplace.net/2009/10/21/are-pointers-and-arrays-equivalent-in-c/comment-page-1/#comment-219635</link>
		<dc:creator>Sachin</dc:creator>
		<pubDate>Tue, 27 Oct 2009 05:18:34 +0000</pubDate>
		<guid isPermaLink="false">http://eli.thegreenplace.net/?p=1963#comment-219635</guid>
		<description>Avery neat and clean and nice explanation....clearly explains the difference..thanks</description>
		<content:encoded><![CDATA[<p>Avery neat and clean and nice explanation&#8230;.clearly explains the difference..thanks</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Experiment Garden</title>
		<link>http://eli.thegreenplace.net/2009/10/21/are-pointers-and-arrays-equivalent-in-c/comment-page-1/#comment-219609</link>
		<dc:creator>Experiment Garden</dc:creator>
		<pubDate>Tue, 27 Oct 2009 03:21:10 +0000</pubDate>
		<guid isPermaLink="false">http://eli.thegreenplace.net/?p=1963#comment-219609</guid>
		<description>This is a very nice tutorial that is bound to help many programming newbies.  I remember when I was taking beginning C++ in college.  I had already been working with C++ for years but there were many in the class who had no prior programming experience.  They didn&#039;t understand the difference between pointers and arrays and also didn&#039;t understand why anyone would use a pointer at all!

I tried to explain the power of the pointer to them but I fear that most of them never got it.  Your succinct explanation here, though, probably would have helped them understand.</description>
		<content:encoded><![CDATA[<p>This is a very nice tutorial that is bound to help many programming newbies.  I remember when I was taking beginning C++ in college.  I had already been working with C++ for years but there were many in the class who had no prior programming experience.  They didn&#8217;t understand the difference between pointers and arrays and also didn&#8217;t understand why anyone would use a pointer at all!</p>
<p>I tried to explain the power of the pointer to them but I fear that most of them never got it.  Your succinct explanation here, though, probably would have helped them understand.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Isaac</title>
		<link>http://eli.thegreenplace.net/2009/10/21/are-pointers-and-arrays-equivalent-in-c/comment-page-1/#comment-219582</link>
		<dc:creator>Isaac</dc:creator>
		<pubDate>Tue, 27 Oct 2009 01:25:57 +0000</pubDate>
		<guid isPermaLink="false">http://eli.thegreenplace.net/?p=1963#comment-219582</guid>
		<description>Excellent article! That really clarifies my understanding of pointers and array in C. I&#039;ve been learning it through P&amp;H&#039;s MIPS Architecture book, so it&#039;s also interesting to see if presented in more &quot;modern&quot; x86 assembly. 

Cheers, coming from HN.</description>
		<content:encoded><![CDATA[<p>Excellent article! That really clarifies my understanding of pointers and array in C. I&#8217;ve been learning it through P&amp;H&#8217;s MIPS Architecture book, so it&#8217;s also interesting to see if presented in more &#8220;modern&#8221; x86 assembly. </p>
<p>Cheers, coming from HN.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Peter</title>
		<link>http://eli.thegreenplace.net/2009/10/21/are-pointers-and-arrays-equivalent-in-c/comment-page-1/#comment-219451</link>
		<dc:creator>Peter</dc:creator>
		<pubDate>Mon, 26 Oct 2009 20:08:57 +0000</pubDate>
		<guid isPermaLink="false">http://eli.thegreenplace.net/?p=1963#comment-219451</guid>
		<description>I cant be bothered to read the whole article. I just want to know if a is the same as b in the 2nd example. Please state the answer before you dive into the details. Otherwise, thanks</description>
		<content:encoded><![CDATA[<p>I cant be bothered to read the whole article. I just want to know if a is the same as b in the 2nd example. Please state the answer before you dive into the details. Otherwise, thanks</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: gwenhwyfaer</title>
		<link>http://eli.thegreenplace.net/2009/10/21/are-pointers-and-arrays-equivalent-in-c/comment-page-1/#comment-219409</link>
		<dc:creator>gwenhwyfaer</dc:creator>
		<pubDate>Mon, 26 Oct 2009 18:16:15 +0000</pubDate>
		<guid isPermaLink="false">http://eli.thegreenplace.net/?p=1963#comment-219409</guid>
		<description>Funnily enough, in BCPL arrays and pointers were exactly equivalent. If you declared an array, you got a variable with that name, initialised with a pointer to the number of items in that array. Likewise, functions were variables initialised to the address of the function. Dennis Ritchie changed this in the transition from B to C when he added types, because not having to chase the hidden pointer variables around made life easier (as I recall). Indeed, arguably the difference you&#039;re going all around the houses to explain above is simply the difference between pointers (which *is* how C treats arrays) and pointer variables (exactly the same as the difference between a char and a char variable). And looked at that way, it all gets quite straightforward.</description>
		<content:encoded><![CDATA[<p>Funnily enough, in BCPL arrays and pointers were exactly equivalent. If you declared an array, you got a variable with that name, initialised with a pointer to the number of items in that array. Likewise, functions were variables initialised to the address of the function. Dennis Ritchie changed this in the transition from B to C when he added types, because not having to chase the hidden pointer variables around made life easier (as I recall). Indeed, arguably the difference you&#8217;re going all around the houses to explain above is simply the difference between pointers (which *is* how C treats arrays) and pointer variables (exactly the same as the difference between a char and a char variable). And looked at that way, it all gets quite straightforward.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Keith Thompson</title>
		<link>http://eli.thegreenplace.net/2009/10/21/are-pointers-and-arrays-equivalent-in-c/comment-page-1/#comment-218456</link>
		<dc:creator>Keith Thompson</dc:creator>
		<pubDate>Fri, 23 Oct 2009 22:19:48 +0000</pubDate>
		<guid isPermaLink="false">http://eli.thegreenplace.net/?p=1963#comment-218456</guid>
		<description>turly: Since the program&#039;s behavior is undefined, one of the infinitely many (but vanishingly unlikely) possibilities is that will print
`&lt;code&gt;str is __abENT__apos;Hello, world__abENT__apos;&lt;/code&gt;`

(Oh, and the &quot;const&quot; shouldn&#039;t be there.  The standard specifies two forms for the definition of main; neither of them uses &quot;const&quot;.  It&#039;s unlikely to cause any problems.)

I once had an interview question involving the output of a program that involved something along the lines of &quot;i = i++;&quot;.  I said the behavior was undefined.  The interviewer insisted that it&#039;s well defined.  Another answer, of course, is that the line will never survive a code review; whatever it&#039;s supposed to mean, there&#039;s a better way to say it.  (I got the job.)</description>
		<content:encoded><![CDATA[<p>turly: Since the program&#8217;s behavior is undefined, one of the infinitely many (but vanishingly unlikely) possibilities is that will print<br />
<code class="backtick">str is &#039;Hello, world&#039;</code></p>
<p>(Oh, and the &#8220;const&#8221; shouldn&#8217;t be there.  The standard specifies two forms for the definition of main; neither of them uses &#8220;const&#8221;.  It&#8217;s unlikely to cause any problems.)</p>
<p>I once had an interview question involving the output of a program that involved something along the lines of &#8220;i = i++;&#8221;.  I said the behavior was undefined.  The interviewer insisted that it&#8217;s well defined.  Another answer, of course, is that the line will never survive a code review; whatever it&#8217;s supposed to mean, there&#8217;s a better way to say it.  (I got the job.)</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: turly</title>
		<link>http://eli.thegreenplace.net/2009/10/21/are-pointers-and-arrays-equivalent-in-c/comment-page-1/#comment-218367</link>
		<dc:creator>turly</dc:creator>
		<pubDate>Fri, 23 Oct 2009 16:42:56 +0000</pubDate>
		<guid isPermaLink="false">http://eli.thegreenplace.net/?p=1963#comment-218367</guid>
		<description>We used to have an interview question as follows:
In str.c:

`&lt;code&gt;char str [] = __abENT__quot;Hello, world__abENT__quot;;&lt;/code&gt;`

and in main.c:

`&lt;code&gt;extern char *str;

int main (int argc, const char *argv [])
{
     printf (__abENT__quot;str is __abENT__apos;%s__abENT__apos;__abENT__#92;n__abENT__quot;, str);
     return 0;
}&lt;/code&gt;`

Q: what will happen when the resulting program is run, and why?</description>
		<content:encoded><![CDATA[<p>We used to have an interview question as follows:<br />
In str.c:</p>
<div class="backtick"><pre><code>char str [] = &quot;Hello, world&quot;;</code></pre></div>
<p>and in main.c:</p>
<div class="backtick"><pre><code>extern char *str;

int main (int argc, const char *argv [])
{
     printf (&quot;str is &#039;%s&#039;&#92;n&quot;, str);
     return 0;
}</code></pre></div>
<p>Q: what will happen when the resulting program is run, and why?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: dave</title>
		<link>http://eli.thegreenplace.net/2009/10/21/are-pointers-and-arrays-equivalent-in-c/comment-page-1/#comment-218111</link>
		<dc:creator>dave</dc:creator>
		<pubDate>Fri, 23 Oct 2009 03:20:07 +0000</pubDate>
		<guid isPermaLink="false">http://eli.thegreenplace.net/?p=1963#comment-218111</guid>
		<description>Keith,

You&#039;re referring to defined limits of precision in the standard.  

You cite a rather interesting case where short is padded but int is not.  

There are ranks for precision of conversion that must be met in the standard as well requiring that the highest rank is long long, followed by long, followed by int, followed by short, and lastly char.

That said, you have to be able to represent the numerical value of a short in an int... your example of padding digits is one I&#039;ve never seen happen, but I suppose it could, and thusly could increase the size of a short, but the size would then not be an indication of precision :-)

My main point is, knowing the LP32, LP64, ILP64 or whatever model of the system in question, is pretty much a prerequisite to doing useful things with integer types, and stdint.h is a nice portable way to get some of the behavior you want, when portability matters.</description>
		<content:encoded><![CDATA[<p>Keith,</p>
<p>You&#8217;re referring to defined limits of precision in the standard.  </p>
<p>You cite a rather interesting case where short is padded but int is not.  </p>
<p>There are ranks for precision of conversion that must be met in the standard as well requiring that the highest rank is long long, followed by long, followed by int, followed by short, and lastly char.</p>
<p>That said, you have to be able to represent the numerical value of a short in an int&#8230; your example of padding digits is one I&#8217;ve never seen happen, but I suppose it could, and thusly could increase the size of a short, but the size would then not be an indication of precision <img src='http://eli.thegreenplace.net/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>
<p>My main point is, knowing the LP32, LP64, ILP64 or whatever model of the system in question, is pretty much a prerequisite to doing useful things with integer types, and stdint.h is a nice portable way to get some of the behavior you want, when portability matters.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Keith Thompson</title>
		<link>http://eli.thegreenplace.net/2009/10/21/are-pointers-and-arrays-equivalent-in-c/comment-page-1/#comment-218024</link>
		<dc:creator>Keith Thompson</dc:creator>
		<pubDate>Thu, 22 Oct 2009 21:53:38 +0000</pubDate>
		<guid isPermaLink="false">http://eli.thegreenplace.net/?p=1963#comment-218024</guid>
		<description>dave:
You wrote:
==========
`&lt;code&gt;The ONLY relationship the C standards guarantee are the following:

    sizeof(char) __abENT__lt;= sizeof(short) __abENT__lt;= sizeof(int) __abENT__lt;= sizeof(long) __abENT__lt;= sizeof(long long)&lt;/code&gt;`
==========

That&#039;s not *quite* true.  The standard makes certain guarantees about the *ranges* of the predefined types.  It guarantees that sizeof(char)==1, but you could conceivably have sizeof(short) &gt; sizeof(int) (this would require short to have padding bits, i.e., bits that don&#039;t contribute to the value).  In practice, the above size relationships will almost certainly hold.

Some more guarantees:
sizeof(char)==1
The range of each type in the following list is a subset of the range of the following type:
signed char, short, int, long, long long
Likewise for the corresponding unsigned types.  (Plain char has the same range and representation as either signed char or unsigned char, but is a distinct type.)
char is at least 8 bits
short and int are at least 16 bits
long is at least 32 bits
long long is at least 64 bits
(Those guarantees are actually stated in terms of ranges.)</description>
		<content:encoded><![CDATA[<p>dave:<br />
You wrote:<br />
==========<br />
<code class="backtick">The ONLY relationship the C standards guarantee are the following:</p>
<p>    sizeof(char) &lt;= sizeof(short) &lt;= sizeof(int) &lt;= sizeof(long) &lt;= sizeof(long long)</code><br />
==========</p>
<p>That&#8217;s not *quite* true.  The standard makes certain guarantees about the *ranges* of the predefined types.  It guarantees that sizeof(char)==1, but you could conceivably have sizeof(short) &gt; sizeof(int) (this would require short to have padding bits, i.e., bits that don&#8217;t contribute to the value).  In practice, the above size relationships will almost certainly hold.</p>
<p>Some more guarantees:<br />
sizeof(char)==1<br />
The range of each type in the following list is a subset of the range of the following type:<br />
signed char, short, int, long, long long<br />
Likewise for the corresponding unsigned types.  (Plain char has the same range and representation as either signed char or unsigned char, but is a distinct type.)<br />
char is at least 8 bits<br />
short and int are at least 16 bits<br />
long is at least 32 bits<br />
long long is at least 64 bits<br />
(Those guarantees are actually stated in terms of ranges.)</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Keith Thompson</title>
		<link>http://eli.thegreenplace.net/2009/10/21/are-pointers-and-arrays-equivalent-in-c/comment-page-1/#comment-218008</link>
		<dc:creator>Keith Thompson</dc:creator>
		<pubDate>Thu, 22 Oct 2009 21:35:51 +0000</pubDate>
		<guid isPermaLink="false">http://eli.thegreenplace.net/?p=1963#comment-218008</guid>
		<description>Corby: Um, no.

Pointer objects are allocated wherever you happen to allocate them.  The memory that a pointer *points to* is typically allocated on the heap, but you can easily have a pointer point to an object that&#039;s allocated anywhere you like.

The C++ &quot;new&quot; operator very likely is implemented using malloc(), but that&#039;s not specified by the language.  This is an important distinction; allocating with &quot;new&quot; and deallocating with free(), or allocating with malloc() and deallocating with &quot;delete&quot;, might happen to work, or it might go badly wrong.

calloc() does not allocate on the stack; it&#039;s like malloc() except that the allocated memory is initialized to all-bits-zero.  You&#039;re probably thinking of alloca() (which is non-standard and generally not recommended).

Any object can be allocated anywhere (or, as the C standard puts it, with any storage duration: static, automatic, or allocated).  Pointers and arrays are just two particular kinds of objects; others are integers, floating-point objects, structs, and unions.  The way a particular object is allocated depends entirely on the code used to create it.

Incidentally, the C language standard doesn&#039;t use the terms &quot;stack&quot; and &quot;heap&quot;.</description>
		<content:encoded><![CDATA[<p>Corby: Um, no.</p>
<p>Pointer objects are allocated wherever you happen to allocate them.  The memory that a pointer *points to* is typically allocated on the heap, but you can easily have a pointer point to an object that&#8217;s allocated anywhere you like.</p>
<p>The C++ &#8220;new&#8221; operator very likely is implemented using malloc(), but that&#8217;s not specified by the language.  This is an important distinction; allocating with &#8220;new&#8221; and deallocating with free(), or allocating with malloc() and deallocating with &#8220;delete&#8221;, might happen to work, or it might go badly wrong.</p>
<p>calloc() does not allocate on the stack; it&#8217;s like malloc() except that the allocated memory is initialized to all-bits-zero.  You&#8217;re probably thinking of alloca() (which is non-standard and generally not recommended).</p>
<p>Any object can be allocated anywhere (or, as the C standard puts it, with any storage duration: static, automatic, or allocated).  Pointers and arrays are just two particular kinds of objects; others are integers, floating-point objects, structs, and unions.  The way a particular object is allocated depends entirely on the code used to create it.</p>
<p>Incidentally, the C language standard doesn&#8217;t use the terms &#8220;stack&#8221; and &#8220;heap&#8221;.</p>
]]></content:encoded>
	</item>
</channel>
</rss>
