<?xml version="1.0"?>
<rss version="2.0">
	<channel>
		<title>newb confusion about pointers</title>
		<link>http://www.allegro.cc/forums/view/587355</link>
		<description>Allegro.cc Forum Thread</description>
		<webMaster>matthew@allegro.cc (Matthew Leverton)</webMaster>
		<lastBuildDate>Fri, 01 Sep 2006 23:53:28 +0000</lastBuildDate>
	</channel>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>when using set_color(int index, const RGB *p)</p><p>does a RGB pointer have to be declared ?</p><p>ie</p><p>RGB *black;</p><p>and then</p><p>set_color(255, black);</p><p>i think this must be wrong since it&#39;s not working in my code.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (William Labbett)</author>
		<pubDate>Fri, 01 Sep 2006 05:57:39 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Try just &amp;black, without making it a pointer. Can you better describe what is not working?
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Elverion)</author>
		<pubDate>Fri, 01 Sep 2006 06:09:31 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>i think between &quot;RGB *black;&quot; and &quot;set_color(255, black);&quot;,<br />u should make sure that what black pointed is a reasonable value ,then u can use it in &quot;set_color(255, black)&quot;.</p><p>this is the syntax of set_clor function:</p><p>void set_color(int index, const RGB *p);</p><p>Sets the specified palette entry to the specified RGB triplet. Unlike the other palette functions this doesn&#39;t do any retrace synchronisation, so you should call vsync() before it to prevent snow problems. Example: <br />      RGB rgb;<br />      ...<br />      vsync();<br />      set_color(192, &amp;rgb);
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (sj971059)</author>
		<pubDate>Fri, 01 Sep 2006 06:11:17 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Well, I&#39;m afraid I can&#39;t tell you more than to grab a book or find a tutorial covering pointer basics. There are already plenty of good resources out there, so it won&#39;t make much sense if we explained it all over again ;-)</p><p>Syntactically, your code is correct. The fact that <tt>p</tt> is passed as <tt>const</tt> however suggests that it is used as in-parameter (pass-by-reference instead of pass-by-value). The pointer <tt>black</tt> in your example points to anything - you didn&#39;t initialize it, so you must not dereference <tt>black</tt>, which I suppose <tt>set_color</tt> does.</p><p>So, quick fix (be sure to read up on the topic, though!):</p><div class="source-code snippet"><div class="inner"><pre><a href="http://www.allegro.cc/manual/RGB" target="_blank"><span class="a">RGB</span></a> black <span class="k3">=</span> <span class="k2">{</span><span class="n">0</span>, <span class="n">0</span>, <span class="n">0</span><span class="k2">}</span><span class="k2">;</span>
<a href="http://www.allegro.cc/manual/RGB" target="_blank"><span class="a">RGB</span></a> <span class="k3">*</span>pointer_to_black <span class="k3">=</span> <span class="k3">&amp;</span>black<span class="k2">;</span> <span class="c">// initialize pointer with address of black</span>

<a href="http://www.allegro.cc/manual/set_color" target="_blank"><span class="a">set_color</span></a><span class="k2">(</span><span class="n">255</span>, <span class="k3">&amp;</span>black<span class="k2">)</span><span class="k2">;</span> <span class="c">// need to reference it</span>
<a href="http://www.allegro.cc/manual/set_color" target="_blank"><span class="a">set_color</span></a><span class="k2">(</span><span class="n">255</span>, pointer_to_black<span class="k2">)</span><span class="k2">;</span> <span class="c">// no need to reference, already an address</span>

<span class="c">// ^ above two set_color calls do mutually the same</span>
</pre></div></div><p>

<i>edit:</i> Beaten. <img src="http://www.allegro.cc/forums/smileys/tongue.gif" alt=":P" />
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Indeterminatus)</author>
		<pubDate>Fri, 01 Sep 2006 06:14:44 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>thanks</p><p>well basically by not working i mean the program just terminates with no error<br />message.</p><p>i&#39;ve always been a bit flummoxed by pointers. sometimes they make sense, other times not</p><p>it seems strange -</p><p>with the functions that use BITMAP pointers makes sense</p><p>the function argument type is BITMAP *bmp say.<br />it&#39;s used by declaring a BITMAP pointer;</p><p>BITMAP *my_bmp;</p><p>and then you just use the name my_bmp</p><p>i you apply the same logic to the functions which use RGB&#39;s</p><p>you&#39;d expect to have to declare an RGB pointer<br />and just supply it&#39;s name like with the BITMAP example</p><p><img src="http://www.allegro.cc/forums/smileys/huh.gif" alt="???" />
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (William Labbett)</author>
		<pubDate>Fri, 01 Sep 2006 06:15:00 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
i&#39;ve always been a bit flummoxed by pointers. sometimes they make sense, other times not
</p></div></div><p>
They make sense all the time, just not to you :-)</p><p>Anyway, I&#39;ll explain. In the RGB example you give, the pointer is &quot;abused&quot; for the C-version of pass-by-reference. &quot;Pass-by-reference&quot; means: When passing something to a function, we don&#39;t give the function the value (&quot;pass-by-value&quot;); instead, we&#39;re telling the function where to find it. Remember that a pointer is an &quot;address&quot; in memory; it points to a memory location. So effectively, what you need to do is give the set_color() function the address of the RGB structure you want to use. There are 2 ways of achieving this:<br />a) Allocate an RGB pointer, fill it with the desired values, pass it to the function, then deallocate. Manual memory management, in other words.
</p><div class="source-code snippet"><div class="inner"><pre><a href="http://www.allegro.cc/manual/RGB" target="_blank"><span class="a">RGB</span></a><span class="k3">*</span> black<span class="k2">;</span>
black <span class="k3">=</span> <a href="http://www.delorie.com/djgpp/doc/libc/libc_551.html" target="_blank">malloc</a><span class="k2">(</span><span class="k1">sizeof</span><span class="k2">(</span><a href="http://www.allegro.cc/manual/RGB" target="_blank"><span class="a">RGB</span></a><span class="k2">)</span><span class="k2">)</span><span class="k2">;</span>
black-&gt;r <span class="k3">=</span> <span class="n">0</span><span class="k2">;</span>
black-&gt;g <span class="k3">=</span> <span class="n">0</span><span class="k2">;</span>
black-&gt;b <span class="k3">=</span> <span class="n">0</span><span class="k2">;</span>
<a href="http://www.allegro.cc/manual/set_color" target="_blank"><span class="a">set_color</span></a><span class="k2">(</span><span class="n">0</span>, black<span class="k2">)</span><span class="k2">;</span>
<a href="http://www.delorie.com/djgpp/doc/libc/libc_350.html" target="_blank">free</a><span class="k2">(</span>black<span class="k2">)</span><span class="k2">;</span>
</pre></div></div><p>
b) Define an RGB struct, fill it with the desired values, pass its address (using the &amp; operator) to the function.
</p><div class="source-code snippet"><div class="inner"><pre><a href="http://www.allegro.cc/manual/RGB" target="_blank"><span class="a">RGB</span></a> black<span class="k2">;</span> <span class="c">// note that this is not a pointer yet, it's the actual struct</span>
black.r <span class="k3">=</span> <span class="n">0</span><span class="k2">;</span>
black.g <span class="k3">=</span> <span class="n">0</span><span class="k2">;</span>
black.b <span class="k3">=</span> <span class="n">0</span><span class="k2">;</span>
<a href="http://www.allegro.cc/manual/set_color" target="_blank"><span class="a">set_color</span></a><span class="k2">(</span><span class="n">0</span>, <span class="k3">&amp;</span>black<span class="k2">)</span><span class="k2">;</span> <span class="c">// &amp;black is a pointer, pointing to black</span>
</pre></div></div><p>
By directly initializing the struct, you can make the code more compact:
</p><div class="source-code snippet"><div class="inner"><pre><a href="http://www.allegro.cc/manual/RGB" target="_blank"><span class="a">RGB</span></a> black <span class="k3">=</span> <span class="k2">{</span> <span class="n">0</span>, <span class="n">0</span>, <span class="n">0</span> <span class="k2">}</span><span class="k2">;</span>
<a href="http://www.allegro.cc/manual/set_color" target="_blank"><span class="a">set_color</span></a><span class="k2">(</span><span class="n">0</span>, <span class="k3">&amp;</span>black<span class="k2">)</span><span class="k2">;</span>
</pre></div></div><p>

So why do we pass by reference instead of value? There are 2 reasons why one would do that. One is speed: Anything larger than a pointer can be passed more efficiently by reference; this removes strain from the stack, and requires less memory allocation / deallocation in general. Also, there is no need to copy actual data.<br />The other reason is to allow the called function to alter data from outside. When passing by value, the function receives a copy of the variable you pass, which remains intact after the function returns. Passing a pointer makes it possible to change the memory it points to.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Tobias Dammers)</author>
		<pubDate>Fri, 01 Sep 2006 19:20:47 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="source-code snippet"><div class="inner"><pre><a href="http://www.allegro.cc/manual/RGB" target="_blank"><span class="a">RGB</span></a> black<span class="k2">;</span> <span class="c">// note that this is not a pointer yet, it's the actual struct</span>
black.r <span class="k3">=</span> <span class="n">0</span><span class="k2">;</span>
black.g <span class="k3">=</span> <span class="n">0</span><span class="k2">;</span>
black.b <span class="k3">=</span> <span class="n">0</span><span class="k2">;</span>
<a href="http://www.allegro.cc/manual/set_color" target="_blank"><span class="a">set_color</span></a><span class="k2">(</span><span class="n">0</span>, <span class="k3">&amp;</span>black<span class="k2">)</span><span class="k2">;</span> <span class="c">// &amp;black is a pointer, pointing to black</span>
</pre></div></div><p>

so does this mean it&#39;s always possible to take the address of a variable<br />e.g<br />int x;</p><p>foo(&amp;x);</p><p>even when it&#39;s not actually declared as a pointer ?
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (William Labbett)</author>
		<pubDate>Fri, 01 Sep 2006 23:29:51 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Yes.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Indeterminatus)</author>
		<pubDate>Fri, 01 Sep 2006 23:40:45 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>that&#39;s great, it makes sense now <img src="http://www.allegro.cc/forums/smileys/smiley.gif" alt=":)" /></p><p>i&#39;m on the road to pointer clarity
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (William Labbett)</author>
		<pubDate>Fri, 01 Sep 2006 23:53:28 +0000</pubDate>
	</item>
</rss>
