<?xml version="1.0"?>
<rss version="2.0">
	<channel>
		<title>Passing ALLEGRO_* by reference</title>
		<link>http://www.allegro.cc/forums/view/617656</link>
		<description>Allegro.cc Forum Thread</description>
		<webMaster>matthew@allegro.cc (Matthew Leverton)</webMaster>
		<lastBuildDate>Sat, 08 Dec 2018 22:42:54 +0000</lastBuildDate>
	</channel>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Hey there, new guy here.  I&#39;ve written a function that does all the initializing (al_init, al_create_display, etc.) so I can keep all that stuff out of main(); it&#39;s also important because I&#39;d like to pass them to other functions later.  I would like to pass by reference, but some of the structures like ALLEGRO_DISPLAY, ALLEGRO_EVENT_QUEUE etc. seem to have something &quot;incomplete&quot; about them (if you don&#39;t make it a pointer, Visual Studio screams &quot;incomplete type is not allowed&quot;).  Now I&#39;ve been working in C for awhile, and I&#39;ve used structs before (and created plenty myself), and I&#39;ve never seen this error.  I don&#39;t know what makes Allegro&#39;s &quot;incomplete&quot;, but apparently it is because {reasons}.  In other words, I&#39;m trying to do:</p><p>	ALLEGRO_DISPLAY * screen = NULL;<br />	ALLEGRO_EVENT_QUEUE * event_q = NULL;<br />	ALLEGRO_EVENT_SOURCE * keyboard = NULL;<br />	int setup = load_allegro(screen, event_q, keyboard);<br />	// this &quot;load_allegro&quot; returns a value indicating whether<br />	// it worked and what failed if it didn&#39;t.</p><p>The function works as expected, I get a display for a second, and it returns 0 (no error), but then the variables go out of scope, so all those pointers are NULL again, so I can&#39;t do anything with them.</p><p>So normally, if I want output variables, I would pass by reference (using &quot;&amp;&quot;), but trying that here results in the same error.  I&#39;m sure there&#39;s some super-simple solution I just haven&#39;t seen before... or is there?  Maybe it&#39;s a Visual Studio issue?  I suppose I could just make them global and have a header with all my global variables, but I&#39;d rather not do that if I can help it.  I&#39;ve been all over Google and I&#39;m all out of theories, so I figured I&#39;d ask the experts.  Any info on the subject would be awesome.  Thanks! <img src="http://www.allegro.cc/forums/smileys/smiley.gif" alt=":)" />
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Geek on Skates)</author>
		<pubDate>Sat, 08 Dec 2018 06:34:32 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Geek on Skates said </p><div class="quote_container"><div class="title"><a href="http://www.allegro.cc/forums/thread/617656/1040368#target">Geek on Skates</a> said:</div><div class="quote"><p>
So normally, if I want output variables, I would pass by reference (using &quot;&amp;&quot;), but trying that here results in the same error. I&#39;m sure there&#39;s some super-simple solution I just haven&#39;t seen before... or is there? </p></div></div><p>

  passing &amp; of screen seems wrong since screen is a reference.<br />  <br />  if you are doing something like <br />  </p><div class="source-code snippet"><div class="inner"><pre>    <span class="c">// In main</span>
    <span class="k1">int</span> setup <span class="k3">=</span> load_allegro<span class="k2">(</span><a href="http://www.allegro.cc/manual/screen"><span class="a">screen</span></a>, event_q, keyboard<span class="k2">)</span><span class="k2">;</span>

    <span class="c">// Outside main</span>
    load_allegro<span class="k2">(</span><a href="http://www.allegro.cc/manual/ALLEGRO_DISPLAY"><span class="a">ALLEGRO_DISPLAY</span></a> <span class="k3">&amp;</span><a href="http://www.allegro.cc/manual/screen"><span class="a">screen</span></a>, <a href="http://www.allegro.cc/manual/ALLEGRO_EVENT_QUEUE"><span class="a">ALLEGRO_EVENT_QUEUE</span></a> <span class="k3">&amp;</span>event_q, <a href="http://www.allegro.cc/manual/ALLEGRO_EVENT_SOURCE"><span class="a">ALLEGRO_EVENT_SOURCE</span></a> <span class="k3">&amp;</span>keyboard<span class="k2">)</span><span class="k2">{</span>
            <span class="c">// Your code</span>
         <span class="k2">}</span>
</pre></div></div><p>

Then it should not be a problem. Try updating your IDE.<br />I don&#39;t guarantee that it will work.</p><p>What you are trying to do is a bad practice, that&#39;s how cool kids create bottlenecks for their programs. They try to push the uncool code into a separate function and try to make C code look like python code, don&#39;t be like them.</p><p>and upload source code from next time, its really hard to guess what you&#39;re doing actually wrong.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Doctor Cop)</author>
		<pubDate>Sat, 08 Dec 2018 11:29:07 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Thanks for the advice, and I apologize for not pasting more code in.  I didn&#39;t think there was anything particularly wrong about separating stuff out into different functions; I was thinking more along the lines of keeping the code organized.  But of course it&#39;s always better to follow best practices.  So a 1000-line main() it is. (<img src="http://www.allegro.cc/forums/smileys/cheesy.gif" alt=";D" border="0" />)</p><p>Seriously though, while I was waiting for this I actually put all my load_allegro and unload_allegro code into main, and while it&#39;s a bit more &quot;speaghetti-like&quot; but it definitely works,  and in my main event loop I can still use other functions without having to do that anyway.  So thanks again &amp; have a great weekend. <img src="http://www.allegro.cc/forums/smileys/smiley.gif" alt=":)" /></p><p>PS: Speaking of posting code, how did you make your code indented and use syntax highlighting?  I don&#39;t see any button for it.  Or is this one of those where you can just do [CODE]//my code[/CODE}?
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Geek on Skates)</author>
		<pubDate>Sat, 08 Dec 2018 11:58:20 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Its like HTML code &lt;code&gt;<br />and &lt;quote&gt;</p><p>don&#39;t forget to close the tags with &lt;/tagname&gt;
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Doctor Cop)</author>
		<pubDate>Sat, 08 Dec 2018 14:40:19 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>You don&#39;t need to pass by reference, since they&#39;re already pointers. You would only need a reference if you wanted to change the variable itself. And passing a reference to an allegro struct is usually wrong.</p><p>For a quick example, say you wanted to write a function that initializes all your allegro variables :
</p><div class="source-code"><div class="toolbar"><span class="button numbers"><b>#</b></span><span class="button select">Select</span><span class="button expand">Expand</span></div><div class="inner"><span class="number">  1</span><span class="k1">bool</span> SetupAllegro<span class="k2">(</span><a href="http://www.allegro.cc/manual/ALLEGRO_DISPLAY"><span class="a">ALLEGRO_DISPLAY</span></a><span class="k3">*</span><span class="k3">*</span> pdisplay , <a href="http://www.allegro.cc/manual/ALLEGRO_EVENT_QUEUE"><span class="a">ALLEGRO_EVENT_QUEUE</span></a><span class="k3">*</span><span class="k3">*</span> pqueue , <a href="http://www.allegro.cc/manual/ALLEGRO_TIMER"><span class="a">ALLEGRO_TIMER</span></a><span class="k3">*</span><span class="k3">*</span> ptimer<span class="k2">)</span> <span class="k2">{</span>
<span class="number">  2</span>   <span class="k1">if</span> <span class="k2">(</span><span class="k3">!</span>pdisplay <span class="k3">|</span><span class="k3">|</span> <span class="k3">!</span>pqueue <span class="k3">|</span><span class="k3">|</span> <span class="k3">!</span>ptimer<span class="k2">)</span> <span class="k2">{</span><span class="k1">return</span> <span class="k1">false</span><span class="k2">;</span><span class="k2">}</span>
<span class="number">  3</span>
<span class="number">  4</span>   <a href="http://www.allegro.cc/manual/ALLEGRO_DISPLAY"><span class="a">ALLEGRO_DISPLAY</span></a><span class="k3">*</span> d <span class="k3">=</span> <a href="http://www.allegro.cc/manual/al_create_display"><span class="a">al_create_display</span></a><span class="k2">(</span><span class="n">800</span>,<span class="n">600</span><span class="k2">)</span><span class="k2">;</span>
<span class="number">  5</span>   <a href="http://www.allegro.cc/manual/ALLEGRO_EVENT_QUEUE"><span class="a">ALLEGRO_EVENT_QUEUE</span></a><span class="k3">*</span> q <span class="k3">=</span> <a href="http://www.allegro.cc/manual/al_create_event_queue"><span class="a">al_create_event_queue</span></a><span class="k2">(</span><span class="k2">)</span><span class="k2">;</span>
<span class="number">  6</span>   <a href="http://www.allegro.cc/manual/ALLEGRO_TIMER"><span class="a">ALLEGRO_TIMER</span></a><span class="k3">*</span> t <span class="k3">=</span> <a href="http://www.allegro.cc/manual/al_create_timer"><span class="a">al_create_timer</span></a><span class="k2">(</span><span class="n">1</span>.<span class="n">0</span><span class="k3">/</span><span class="n">60</span>.<span class="n">0</span><span class="k2">)</span><span class="k2">;</span>
<span class="number">  7</span>
<span class="number">  8</span>   <span class="k1">if</span> <span class="k2">(</span>q<span class="k2">)</span> <span class="k2">{</span>
<span class="number">  9</span>      <a href="http://www.allegro.cc/manual/al_register_event_source"><span class="a">al_register_event_source</span></a><span class="k2">(</span>q , <a href="http://www.allegro.cc/manual/al_get_timer_event_source"><span class="a">al_get_timer_event_source</span></a><span class="k2">(</span>t<span class="k2">)</span><span class="k2">)</span><span class="k2">;</span>
<span class="number"> 10</span>      <a href="http://www.allegro.cc/manual/al_register_event_source"><span class="a">al_register_event_source</span></a><span class="k2">(</span>q , <a href="http://www.allegro.cc/manual/al_get_display_event_source"><span class="a">al_get_display_event_source</span></a><span class="k2">(</span>d<span class="k2">)</span><span class="k2">)</span><span class="k2">;</span>
<span class="number"> 11</span>   <span class="k2">}</span>
<span class="number"> 12</span>
<span class="number"> 13</span>   <span class="k1">if</span> <span class="k2">(</span><span class="k3">!</span>d<span class="k2">)</span> <span class="k2">{</span>
<span class="number"> 14</span>      <a href="http://www.delorie.com/djgpp/doc/libc/libc_624.html" target="_blank">printf</a><span class="k2">(</span><span class="s">"Failed to create display.\n"</span><span class="k2">)</span><span class="k2">;</span>
<span class="number"> 15</span>   <span class="k2">}</span>
<span class="number"> 16</span>   <span class="k1">if</span> <span class="k2">(</span><span class="k3">!</span>q<span class="k2">)</span> <span class="k2">{</span>
<span class="number"> 17</span>      <a href="http://www.delorie.com/djgpp/doc/libc/libc_624.html" target="_blank">printf</a><span class="k2">(</span><span class="s">"Failed to create event queue.\n"</span><span class="k2">)</span><span class="k2">;</span>
<span class="number"> 18</span>   <span class="k2">}</span>
<span class="number"> 19</span>   <span class="k1">if</span> <span class="k2">(</span><span class="k3">!</span>t<span class="k2">)</span> <span class="k2">{</span>
<span class="number"> 20</span>      <a href="http://www.delorie.com/djgpp/doc/libc/libc_624.html" target="_blank">printf</a><span class="k2">(</span><span class="s">"Failed to create timer.\n"</span><span class="k2">)</span><span class="k2">;</span>
<span class="number"> 21</span>   <span class="k2">}</span>
<span class="number"> 22</span>
<span class="number"> 23</span>   <span class="k3">*</span>pdisplay <span class="k3">=</span> d<span class="k2">;</span>
<span class="number"> 24</span>   <span class="k3">*</span>pqueue <span class="k3">=</span> q<span class="k2">;</span>
<span class="number"> 25</span>   <span class="k3">*</span>ptimer <span class="k3">=</span> t<span class="k2">;</span>
<span class="number"> 26</span>   <span class="k1">return</span> d <span class="k3">&amp;</span><span class="k3">&amp;</span> q <span class="k3">&amp;</span><span class="k3">&amp;</span> t<span class="k2">;</span>
<span class="number"> 27</span><span class="k2">}</span>
</div></div><p>

</p><div class="source-code snippet"><div class="inner"><pre><a href="http://www.allegro.cc/manual/ALLEGRO_DISPLAY"><span class="a">ALLEGRO_DISPLAY</span></a><span class="k3">*</span> d <span class="k3">=</span> <span class="n">0</span><span class="k2">;</span>
<a href="http://www.allegro.cc/manual/ALLEGRO_EVENT_QUEUE"><span class="a">ALLEGRO_EVENT_QUEUE</span></a><span class="k3">*</span> q <span class="k3">=</span> <span class="n">0</span><span class="k2">;</span>
<a href="http://www.allegro.cc/manual/ALLEGRO_TIMER"><span class="a">ALLEGRO_TIMER</span></a><span class="k3">*</span> t <span class="k3">=</span> <span class="n">0</span><span class="k2">;</span>

<span class="k1">if</span> <span class="k2">(</span><span class="k3">!</span>SetupAllegro<span class="k2">(</span><span class="k3">&amp;</span>d , <span class="k3">&amp;</span>q , <span class="k3">&amp;</span>t<span class="k2">)</span><span class="k2">)</span> <span class="k2">{</span>
   <a href="http://www.delorie.com/djgpp/doc/libc/libc_624.html" target="_blank">printf</a><span class="k2">(</span><span class="s">"Setup failed.\n"</span><span class="k2">)</span><span class="k2">;</span>
<span class="k2">}</span>
</pre></div></div><p>

Now you can pass a reference to a pointer, but you cannot pass a reference to most allegro structs, because they are <a href="https://en.wikipedia.org/wiki/Opaque_data_type">opaque data structure</a>.</p><div class="quote_container"><div class="title"><a href="https://www.allegro.cc/forums/thread/617656/1040373#target">Doctor Cop</a> said:</div><div class="quote"><p>
What you are trying to do is a bad practice, that&#39;s how cool kids create bottlenecks for their programs. They try to push the uncool code into a separate function and try to make C code look like python code, don&#39;t be like them.
</p></div></div><p>
Using functions to organize and separate data is not only NOT a bad practice, it is ESSENTIAL to writing good code. It has nothing to do with Python. Python can be just as unorganized and messy as C can.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Edgar Reynaldo)</author>
		<pubDate>Sat, 08 Dec 2018 22:42:54 +0000</pubDate>
	</item>
</rss>
