<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	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/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>John Roach &#187; array</title>
	<atom:link href="http://johnroach.info/tag/array/feed/" rel="self" type="application/rss+xml" />
	<link>http://johnroach.info</link>
	<description>Coding for life</description>
	<lastBuildDate>Wed, 18 Jan 2012 19:30:55 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Changing size of array in C programming language</title>
		<link>http://johnroach.info/2010/04/17/changing-size-of-array-in-c-programming-language/</link>
		<comments>http://johnroach.info/2010/04/17/changing-size-of-array-in-c-programming-language/#comments</comments>
		<pubDate>Sat, 17 Apr 2010 09:59:44 +0000</pubDate>
		<dc:creator>John Roach</dc:creator>
				<category><![CDATA[Coding for fun]]></category>
		<category><![CDATA[array]]></category>
		<category><![CDATA[c]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://johnroach.info/?p=266</guid>
		<description><![CDATA[It&#8217;s been a long time since I coded in C. I needed to change the size of an array within the program. At first I just simply tried; int ab=10; int array[ab]; And surprisingly it didn&#8217;t work. (I mean it &#8230; <a href="http://johnroach.info/2010/04/17/changing-size-of-array-in-c-programming-language/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>It&#8217;s been a long time since I coded in C. I needed to change the size of an array within the program. At first I just simply tried;</p>
<pre name="code" class="c">
int ab=10;
int array[ab];
</pre>
<p>And surprisingly it didn&#8217;t work. (I mean it works in C++ and C#)<br />
Anyway I was thinking of ways on how to do this thought of using malloc() however I really didn&#8217;t know how hence I did some Google&#8217;ing around. And found this neat piece of code.</p>
<pre name="code" class="c">
int *resize_array(int *a, size_t new_size)
{
  int *save;

  save = realloc(a, new_size);
  if (save == NULL) {
    fprintf(stderr, "Memory exhausted\n");
    exit(EXIT_FAILURE);
  }
  return save;
}

int *user_old_array; // the array
int new_array_size=10;
user_old_array = malloc(initial_array_size * sizeof *user_old_array); //resized array
</pre>
<p>Quite neat isn&#8217;t it. I thought I should probably write this somewhere so I won&#8217;t forget. Hence the post.</p>
<p>Enjoy!</p>
]]></content:encoded>
			<wfw:commentRss>http://johnroach.info/2010/04/17/changing-size-of-array-in-c-programming-language/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

