<?xml version="1.0"?>
<rss version="2.0">
	<channel>
		<title>programming difficulty</title>
		<link>http://www.allegro.cc/forums/view/587955</link>
		<description>Allegro.cc Forum Thread</description>
		<webMaster>matthew@allegro.cc (Matthew Leverton)</webMaster>
		<lastBuildDate>Tue, 10 Oct 2006 01:01:18 +0000</lastBuildDate>
	</channel>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>hi,</p><p> I got a struct and an array of pointers to such structs :-</p><div class="source-code snippet"><div class="inner"><pre><span class="k1">struct</span> scenery_object <span class="k2">{</span>
    <span class="k1">int</span> <a href="http://www.allegro.cc/manual/exists" target="_blank"><span class="a">exists</span></a><span class="k2">;</span>
    <span class="k1">int</span> w, h<span class="k2">;</span>
    <span class="k1">int</span> x, y<span class="k2">;</span>
    <span class="k1">int</span> blit_to_x, blit_to_y<span class="k2">;</span>
    <a href="http://www.allegro.cc/manual/BITMAP" target="_blank"><span class="a">BITMAP</span></a> <span class="k3">*</span>thing<span class="k2">;</span>
<span class="k2">}</span><span class="k2">;</span>

<span class="k1">struct</span> scenery_object <span class="k3">*</span>scenery_objects<span class="k2">[</span>MAX_OBJECTS<span class="k2">]</span><span class="k2">;</span>
</pre></div></div><p>

 I need to be able to initialise what the pointers point to.</p><p>If I do this :-</p><div class="source-code snippet"><div class="inner"><pre><span class="k1">struct</span> scenery_object <span class="k2">{</span>
    <span class="k1">int</span> <a href="http://www.allegro.cc/manual/exists" target="_blank"><span class="a">exists</span></a><span class="k2">;</span>
    <span class="k1">int</span> w, h<span class="k2">;</span>
    <span class="k1">int</span> x, y<span class="k2">;</span>
    <span class="k1">int</span> blit_to_x, blit_to_y<span class="k2">;</span>
    <a href="http://www.allegro.cc/manual/BITMAP" target="_blank"><span class="a">BITMAP</span></a> <span class="k3">*</span>thing<span class="k2">;</span>
<span class="k2">}</span><span class="k2">;</span>

<span class="k1">struct</span> scenery_object <span class="k3">*</span>scenery_objects<span class="k2">[</span>MAX_OBJECTS<span class="k2">]</span> <span class="k3">=</span>
<span class="k2">{</span>
    <span class="k2">{</span><span class="n">1</span>, <span class="n">20</span>, <span class="n">30</span>, <span class="n">25</span>, <span class="n">25</span>, <span class="n">0</span>, <span class="n">0</span>, bitmap<span class="k2">}</span>
<span class="k2">}</span><span class="k2">;</span>
</pre></div></div><p>

 (I&#39;ve only initialised one member deliberately.)</p><p>Wouldn&#39;t this be wrong ?</p><p> Is there a way to initialise the structs not the pointers ?
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (William Labbett)</author>
		<pubDate>Tue, 10 Oct 2006 00:45:17 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>(In C) Nope, you can&#39;t do that.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (CGamesPlay)</author>
		<pubDate>Tue, 10 Oct 2006 00:49:24 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>That sucks really. I take it you can in C++.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (William Labbett)</author>
		<pubDate>Tue, 10 Oct 2006 00:51:52 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>In C++ you can give the struct a constructor and initializing it using new. Although that really isn&#39;t practical <img src="http://www.allegro.cc/forums/smileys/smiley.gif" alt=":)" /></p><p>[append]</p><p>If you are initializing them that way, why are you using pointers?
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (CGamesPlay)</author>
		<pubDate>Tue, 10 Oct 2006 00:53:09 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>To be honest I just thought I aught to. I thought large structures are usually accessed with pointers to save copying them around. Is this wrong ?
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (William Labbett)</author>
		<pubDate>Tue, 10 Oct 2006 00:56:40 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>No, large structures are allocated using pointers to keep them off the stack, which has limited space. You can take the address of any variable, whether ir was made with malloc, or a local, or a global, or anything. Taking the address of a variable--the same as making a pointer to it--is done like this:
</p><div class="source-code snippet"><div class="inner"><pre><span class="k1">struct</span> scenery_object so<span class="k2">;</span>
<span class="k1">struct</span> scenery_object<span class="k3">*</span> ptr<span class="k2">;</span>
ptr <span class="k3">=</span> <span class="k3">&amp;</span>so<span class="k2">;</span>
</pre></div></div><p>

[append]<br />Oh, what you said about saving copying them around is valid, but you don&#39;t need to allocate them using malloc to save on the copying (see above).
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (CGamesPlay)</author>
		<pubDate>Tue, 10 Oct 2006 00:59:12 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Okay thanks CGamesPlay - you&#39;re a good man <img src="http://www.allegro.cc/forums/smileys/smiley.gif" alt=":)" /> I&#39;ll see how I get on without the pointers.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (William Labbett)</author>
		<pubDate>Tue, 10 Oct 2006 01:01:18 +0000</pubDate>
	</item>
</rss>
