<?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>Codebreaker &#187; C</title>
	<atom:link href="http://blog.sumin.us/archives/tag/c/feed" rel="self" type="application/rss+xml" />
	<link>http://blog.sumin.us</link>
	<description>2bc08752a0894eb2c7afb345286e391d</description>
	<lastBuildDate>Wed, 08 Sep 2010 02:15:36 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>The Ways Returning Multiple Variables in C/C++</title>
		<link>http://blog.sumin.us/archives/683</link>
		<comments>http://blog.sumin.us/archives/683#comments</comments>
		<pubDate>Fri, 02 Nov 2007 06:54:07 +0000</pubDate>
		<dc:creator>Sumin</dc:creator>
				<category><![CDATA[development]]></category>
		<category><![CDATA[C]]></category>

		<guid isPermaLink="false">http://blog.sumin.us/archives/683</guid>
		<description><![CDATA[There are several ways to get multiple variables from a function in C/C++. Remember C is a pass-by-value language and there is no exception. 1. Pointers Using pointers is a common way to get multiple variables back. void add(int* tiger, int* leopard) { *tiger++; *leopard++; } 2. Arrays We can do the same job by [...]]]></description>
			<content:encoded><![CDATA[<p>There are several ways to get multiple variables from a function in C/C++. Remember C is a pass-by-value language and there is no exception.</p>

<h3>1. Pointers</h3>

<p>Using pointers is a common way to get multiple variables back.</p>

<pre><code>void add(int* tiger, int* leopard) {
    *tiger++;
    *leopard++;
}
</code></pre>

<h3>2. Arrays</h3>

<p>We can do the same job by passing an array as an argument of a function which is expect to be modified, but I wouldn't recommend this as a general solution because every single element of the array has to be the same data type.</p>

<pre><code>int* add(int arg[]) {
    arg[0]++;
    arg[1]++;

    return arg;
}
</code></pre>

<h3>3. Struct</h3>

<p>Structure is one of beautiful features in C even though there is no way to explicitly involve functions with it. It just works like a default data type such as <code>int</code>.</p>

<pre><code>typedef struct _zoo {
    int tiger;
    int leopard;
} zoo;

zoo add(zoo arg) {
    arg.tiger++;
    arg.leopard++;

    return arg;
}
</code></pre>

<h3>4. More?</h3>

<p>How about this? Would it work?</p>

<pre><code>zoo* add(int tiger, int leopard) {
    zoo v = {tiger+1, leopard+1};

    return &amp;v;
}
</code></pre>

<p>The answer is <em>no</em>. It does not work because the stack of the function <code>add</code> is going to be cleaned up when the function is done. That means all the local variable of the function will be gone! Also, you will see a warning message like <code>function returns address of local variable</code> when you try to compile it.</p>
<!-- AdSense Now! V1.83 -->
<!-- Post[count: 2] -->
<div class="adsense adsense-leadout" style="text-align:center;margin: 12px;"><script type="text/javascript"><!--
google_ad_client = "pub-2353453386862167";
/* 468x60, created 10/26/09 */
google_ad_slot = "1252542387";
google_ad_width = 468;
google_ad_height = 60;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></div>]]></content:encoded>
			<wfw:commentRss>http://blog.sumin.us/archives/683/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>
