<?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>Tayyab Bin Tariq &#187; C++</title>
	<atom:link href="http://tayyab.xenoglaux-solutions.com/category/computer-programming/cpp/feed/" rel="self" type="application/rss+xml" />
	<link>http://tayyab.xenoglaux-solutions.com</link>
	<description>I like sharing what i know</description>
	<lastBuildDate>Fri, 21 May 2010 20:59:02 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.6</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Using unmanaged code/types from managed code</title>
		<link>http://tayyab.xenoglaux-solutions.com/2010/05/08/using-unmanaged-codetypes-from-managed-code/</link>
		<comments>http://tayyab.xenoglaux-solutions.com/2010/05/08/using-unmanaged-codetypes-from-managed-code/#comments</comments>
		<pubDate>Sat, 08 May 2010 15:00:22 +0000</pubDate>
		<dc:creator>tayyabtariq</dc:creator>
				<category><![CDATA[C++]]></category>
		<category><![CDATA[Computer Programming]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[managed code]]></category>
		<category><![CDATA[unmanaged code]]></category>
		<category><![CDATA[use managed code from unmanaged code]]></category>
		<category><![CDATA[Visual Studio.NET]]></category>

		<guid isPermaLink="false">http://tayyab.xenoglaux-solutions.com/2010/05/08/using-unmanaged-codetypes-from-managed-code/</guid>
		<description><![CDATA[You cannot declare a handle type in a native type. vcclr.h provides the type-safe wrapper template gcroot, to refer to a CLR object from the C++ heap. This template lets you embed a virtual handle in a native type and treat it as if it were the underlying type.
The gcroot template is implemented using the [...]]]></description>
			<content:encoded><![CDATA[<p>You cannot declare a handle type in a native type. vcclr.h provides the type-safe wrapper template gcroot, to refer to a CLR object from the C++ heap. This template lets you embed a virtual handle in a native type and treat it as if it were the underlying type.</p>
<p>The gcroot template is implemented using the facilities of the value class System::Runtime::InteropServices::GCHandle, which provides &quot;handles&quot; into the garbage-collected heap. Note that the handles themselves are not garbage collected and are freed when no longer in use by the destructor in the gcroot class (this destructor cannot be called manually). If you instantiate a gcroot object on the native heap, you must call delete on that resource.</p>
<p>The runtime will maintain an association between the handle and the CLR object, which it references. When the CLR object moves with the garbage-collected heap, the handle will return the new address of the object. A variable does not have to be pinned before it is assigned to a gcroot template.</p>
<p> <span id="more-192"></span><br />
<h2>Example</h2>
<p>This sample shows how to create a gcroot object on the native stack.</p>
<div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.83%; padding-right: 4px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; height: 252px; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper">
<div style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet">
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #008000">// mcpp_gcroot.cpp</span></pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #008000">// compile with: /clr</span></pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #cc6633">#include</span> &lt;vcclr.h&gt;</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #0000ff">using</span> <span style="color: #0000ff">namespace</span> System;</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">&#160;</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #0000ff">class</span> CppClass {</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #0000ff">public</span>:</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">   gcroot&lt;String^&gt; str;   <span style="color: #008000">// can use str as if it were String^</span></pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">   CppClass() {}</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">};</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">&#160;</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #0000ff">int</span> main() {</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">   CppClass c;</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">   c.str = gcnew String(<span style="color: #006080">&quot;hello&quot;</span>);</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">   Console::WriteLine( c.str );   <span style="color: #008000">// no cast required</span></pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">}</pre>
<p><!--CRLF--></div>
</div>
<h4>Output</h4>
<p>hello</p>
<p>This sample shows how to create a gcroot object on the native heap.</p>
<div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper">
<div style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet">
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #008000">// mcpp_gcroot_2.cpp</span></pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #008000">// compile with: /clr</span></pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #008000">// compile with: /clr</span></pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #cc6633">#include</span> &lt;vcclr.h&gt;</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #0000ff">using</span> <span style="color: #0000ff">namespace</span> System;</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">&#160;</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #0000ff">struct</span> CppClass {</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">   gcroot&lt;String ^&gt; * str;</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">   CppClass() : str(<span style="color: #0000ff">new</span> gcroot&lt;String ^&gt;) {}</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">&#160;</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">   ~CppClass() { <span style="color: #0000ff">delete</span> str; }</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">&#160;</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">};</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">&#160;</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #0000ff">int</span> main() {</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">   CppClass c;</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">   *c.str = gcnew String(<span style="color: #006080">&quot;hello&quot;</span>);</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">   Console::WriteLine( *c.str );</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">}</pre>
<p><!--CRLF--></div>
</div>
<h4>Output</h4>
<p><font face="cour">hello</font></p>
<p>This sample shows how to gcroot to hold references to value types (not reference types) in a native type by using gcroot on the boxed type.</p>
<div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper">
<div style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet">
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #008000">// mcpp_gcroot_3.cpp</span></pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #008000">// compile with: /clr</span></pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #cc6633">#include</span> &lt; vcclr.h &gt;</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #0000ff">using</span> <span style="color: #0000ff">namespace</span> System;</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">&#160;</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #0000ff">public</span> value <span style="color: #0000ff">struct</span> V {</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">   String^ str;</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">};</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">&#160;</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #0000ff">class</span> Native {</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #0000ff">public</span>:</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">   gcroot&lt; V^ &gt; v_handle;</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">};</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">&#160;</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #0000ff">int</span> main() {</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">   Native native;</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">   V v;</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">   native.v_handle = v;</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">   native.v_handle-&gt;str = <span style="color: #006080">&quot;Hello&quot;</span>;</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">   Console::WriteLine(<span style="color: #006080">&quot;String in V: {0}&quot;</span>, native.v_handle-&gt;str);</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">}</pre>
<p><!--CRLF--></div>
</div>
<h4>Output</h4>
<p>String in V: hello</p>
<p>Read original article <a href="http://msdn.microsoft.com/en-us/library/481fa11f%28VS.80%29.aspx" target="_blank">here.</a></p>
<p class="facebook"><a href="http://www.facebook.com/share.php?u=http://tayyab.xenoglaux-solutions.com/2010/05/08/using-unmanaged-codetypes-from-managed-code/" target="_blank"><img src="http://tayyab.xenoglaux-solutions.com/wp-content/plugins/add-to-facebook-plugin/facebook_share_icon.gif" alt="Share on Facebook" title="Share on Facebook" /></a><a href="http://www.facebook.com/share.php?u=http://tayyab.xenoglaux-solutions.com/2010/05/08/using-unmanaged-codetypes-from-managed-code/" target="_blank" title="Share on Facebook">Share on Facebook</a></p>]]></content:encoded>
			<wfw:commentRss>http://tayyab.xenoglaux-solutions.com/2010/05/08/using-unmanaged-codetypes-from-managed-code/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Arrays in C++</title>
		<link>http://tayyab.xenoglaux-solutions.com/2010/04/25/arrays-in-c-2/</link>
		<comments>http://tayyab.xenoglaux-solutions.com/2010/04/25/arrays-in-c-2/#comments</comments>
		<pubDate>Sun, 25 Apr 2010 14:19:23 +0000</pubDate>
		<dc:creator>tayyabtariq</dc:creator>
				<category><![CDATA[C++]]></category>
		<category><![CDATA[Computer Programming]]></category>
		<category><![CDATA[Microsoft Technologies]]></category>
		<category><![CDATA[Array]]></category>
		<category><![CDATA[Arrays]]></category>
		<category><![CDATA[Arrays of Arrays]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Jagged Arrays]]></category>
		<category><![CDATA[Managed C++]]></category>
		<category><![CDATA[Visual Studio.NET]]></category>

		<guid isPermaLink="false">http://tayyab.xenoglaux-solutions.com/2010/04/25/arrays-in-c-2/</guid>
		<description><![CDATA[Introduction
In some programs, you may need to represent a list of items that share some characteristics. You can declare as many variables as necessary to represent such items. Here is an example of 5 numbers declared in a program: 
// This is the main project file for VC++ application project
// generated using an Application Wizard.

#include [...]]]></description>
			<content:encoded><![CDATA[<h2>Introduction</h2>
<p>In some programs, you may need to represent a list of items that share some characteristics. You can declare as many variables as necessary to represent such items. Here is an example of 5 numbers declared in a program: </p>
<pre>// This is the main project file for VC++ application project
// generated using an Application Wizard.

#include &quot;stdafx.h&quot;

#using &lt;mscorlib.dll&gt;

using namespace System;

int _tmain()
{
    // TODO: Please replace the sample code below with your own.
    int number1 = 266;
    int number1 = -4683;
    int number1 = -364;
    int number1 = 1450;
    int number1 = 8;

    Console::WriteLine();
    return 0;
}</pre>
<p>If you want to use a group of objects that are of the same kind, C++ allows you to identify them as one variable. An array is a group of variables that shared the same characteristics. </p>
<h5>Note:</h5>
<p>
  <br />Arrays in Managed C++ are a little different than those traditionally used in C++ because the Microsoft .NET Framework introduced a new way of declaring and using arrays, which also has to do with inherited classes. Because this can be a long and arduous subject, we will use arrays only as they are used and implemented in managed classes.</p>
<h2>Array Declaration </h2>
<p>An array is a group of values of the same kind or the same data type. Because the items are considered in a group, they are declared as one variable but the declaration must indicate that the variable represents various items. The items that are part of the group are also referred to as members of the array. To declare an array, you must give it a name. </p>
<p>In C++, the formula to declare an array is </p>
<pre><i>DataType</i> <i>ArrayName</i>[<i>Dimension</i>];</pre>
<p>With this formula, an array would be declared as a value type. C++&#8217; arrays can be declared either on the stack as in the above formula or on the heap. Managed C++ formally introduces the idea of declaring an array on the heap. Based on this, (most) arrays in Managed C++ are referred to as managed arrays. In this case, the formula to declare a managed array is: </p>
<pre><i>DataType</i> <i>ArrayName</i>[] = new <i>DataType</i>[<i>Dimension</i>];</pre>
<p>Another equivalent formula is: </p>
<pre><i>DataType</i> <i>ArrayName</i> __gc[] = new <i>DataType</i> __gc[<i>Dimension</i>];</pre>
<p>Therefore, the declaration of a managed array starts by specifying a data type, the <i>DataType</i> in our syntax. This indicates the kind of values shared by the members of the array. It also specify the amount of memory space that each member of the array will need to store its value. Like any other variable, an array must have a name, which is the <i>ArrayName</i> in our syntax. The name of the array must be followed by an opening and a closing square brackets &quot;[]&quot;. To indicate that you are declaring a managed array, you must assign the same data type as a managed type, using the <b>new</b> keyword. Once again, the data type must be followed by square brackets. This time, inside of the square brackets, you must type the number of items that the array is made of; that is the <i>Dimension</i> in our syntax. </p>
<p>To create a managed array, the data type can be a normal C/C++ data type such as int, double, or .NET value type such as <b>Int32</b>, <b>Double</b>, etc. Here is an example: </p>
<pre>// This is the main project file for VC++ application project
// generated using an Application Wizard.

#include &quot;stdafx.h&quot;

#using &lt;mscorlib.dll&gt;

using namespace System;

int _tmain()
{
    // TODO: Please replace the sample code below with your own.
    Double Number[] = new Double[5];

    Console::WriteLine(S&quot;&quot;);
    return 0;
}</pre>
<p>To re-enforce the idea of declaring a managed array, you can optionally type the <b>__gc</b> keyword to the left of the square brackets. You can type it either on only one side of the assignment operator or on both sides. Therefore, the following declarations are equivalents: </p>
<pre>// This is the main project file for VC++ application project
// generated using an Application Wizard.

#include &quot;stdafx.h&quot;

#using &lt;mscorlib.dll&gt;

using namespace System;

int _tmain()
{
    // TODO: Please replace the sample code below with your own.
    Double Number1[]      = new Double[5];
    Double Number2 __gc[] = new Double[5];
    Double Number3[]      = new Double __gc[5];
    Double Number4 __gc[] = new Double __gc[5];

    Console::WriteLine();
    return 0;
}</pre>
<p>When declaring a managed array, you must use the <b>new</b> operator to indicate to the compiler that the variable would be declared on the heap. Because the <b>new</b> operator is explicit enough, you can omit the <b>__gc</b> keyword. Also, you don&#8217;t have to perform the whole declaration at the same time but you must have requested that memory be allocated to the array before it can be used. Based on this, you can first declare the variable with the left part of the assignment operator. Then, when you are ready, you can allocate memory to the variable and specify the number of members of the array. When doing this, type the name of the variable without the square brackets and type the right side of our syntax. Here is an example: </p>
<pre>// This is the main project file for VC++ application project
// generated using an Application Wizard.

#include &quot;stdafx.h&quot;

#using &lt;mscorlib.dll&gt;

using namespace System;

int _tmain()
{
    // TODO: Please replace the sample code below with your own.
    Double number[];
    // You can't yet use the array

    Console::WriteLine(S&quot;The Wonderful World of Managed C++!&quot;);

    number = __gc new Double[5];
    // Now the array is ready

    Console::WriteLine();
    return 0;
}</pre>
<p>We mentioned that, when declaring the array, you should specify the number of members of the array with the <i>Dimension</i> in our syntax. After declaring the array, if you want, you can reduce or increase the number of items by simply performing a new assignment to the variable and indicating the new desired <i>Dimension</i>. Here is an example: </p>
<pre>// This is the main project file for VC++ application project
// generated using an Application Wizard.

#include &quot;stdafx.h&quot;

#using &lt;mscorlib.dll&gt;

using namespace System;

int _tmain()
{
    // TODO: Please replace the sample code below with your own.
    Double number[];
    // You can't yet use the array

    Console::WriteLine(S&quot;The Wonderful World of Managed C++!&quot;);

    number = __gc new Double[5];
    // Now the array is ready

    // The new dimension of the array
    number = new Double[8];

    Console::WriteLine();
    return 0;
}</pre>
<h2>Initializing an Array</h2>
<p>When declaring an array, before using it, we saw that you must specify the number of members of the array. This declaration allocates an amount of memory space to the variable. The first member of the array takes a portion of this space. The second member of the array occupies memory next to it: </p>
<p><b>Array Variable</b></p>
<p>Member1<br />
  <br />Member2</p>
<p>Member3</p>
<p>Member4</p>
<p>Member5</p>
<p>Each member of the array can be accessed using its position. The position is also referred to as an index. The members of an array are arranged starting at index 0, followed by index 1, then index 2, etc. This system of counting is referred to as &quot;zero-based&quot; because the counting starts at 0. To locate a member, type the name of the variable followed by an opening and a closing square brackets. Inside the bracket, type the zero-based index of the desired member. After locating the desired member of the array, you can assign it a value, exactly as you would any regular variable. Here is an example: </p>
<pre>// This is the main project file for VC++ application project
// generated using an Application Wizard.

#include &quot;stdafx.h&quot;

#using &lt;mscorlib.dll&gt;

using namespace System;

int _tmain()
{
    // TODO: Please replace the sample code below with your own.
    Double number[] = __gc new Double[5];

    number[0] = 643.18;
    number[1] = 9.64;
    number[2] = 48.05;
    number[3] = 14.26;
    number[4] = 62.55;

    Console::WriteLine(S&quot;&quot;);
    return 0;
}</pre>
<p>In the same way, you can retrieve the value of a member of an array based on its index. Such a value can be displayed to the user as follows: </p>
<pre>// This is the main project file for VC++ application project
// generated using an Application Wizard.

#include &quot;stdafx.h&quot;

#using &lt;mscorlib.dll&gt;

using namespace System;

int _tmain()
{
    // TODO: Please replace the sample code below with your own.
    Double number[] = __gc new Double[5];

    number[0] = 643.18;
    number[1] = 9.64;
    number[2] = 48.05;
    number[3] = 14.26;
    number[4] = 62.55;

    Console::WriteLine(S&quot;Value 1: {0}&quot;, __box(number[0]));
    Console::WriteLine(S&quot;Value 2: {0}&quot;, __box(number[1]));
    Console::WriteLine(S&quot;Value 3: {0}&quot;, __box(number[2]));
    Console::WriteLine(S&quot;Value 4: {0}&quot;, __box(number[3]));
    Console::WriteLine(S&quot;Value 5: {0}&quot;, __box(number[4]));

    number = new Double[8];

    Console::WriteLine(S&quot;&quot;);
    return 0;
}</pre>
<p>This would produce: </p>
<pre>Value 1: 643.18
Value 2: 9.64
Value 3: 48.05
Value 4: 14.26
Value 5: 62.55

Press any key to continue</pre>
<p>When it comes to initializing a managed array, you don&#8217;t have to access each member of the array and initialize them individually. That technique is used if you don&#8217;t yet have the values of the items at the time you are declaring the array. If you know the values of the members of the managed array at the time you are declaring its variable, you can declare and initialize the whole array at the same time. The formula you would use is: </p>
<pre><i>DataType</i> <i>VariableName</i> __gc[] = { <i>Values Separated By Commas</i> };</pre>
<p>This formula is almost the same used in C/C++ with one difference. If you omit the <b>__gc</b> keyword, the array would be a regular one as used in C/C++. The alternative is to type the __gc keyword to the left of the square brackets. If you do this, two features would automatically be applied to your array. First, the array would be garbage collected, that is, cleaned up by the garbage collector; therefore becoming a managed array. Second, the array becomes automatically derived from the .NET&#8217;s Array class, which gives it access to the properties and methods of that class.</p>
<p>If you use the formula we used earlier with the number variable, the data type must be a .NET value type such as <b>Int32</b>, <b>Double</b>, <b>Single</b>, etc. If you use the above formula to declare and initialize your array, the data type can be a regular C/C++ type (such as <b>int</b>, long, or <b>double</b>) or a .NET value type (such as <b>Int32</b> or <b>Double</b>). Here is an example:</p>
<pre>// This is the main project file for VC++ application project
// generated using an Application Wizard.

#include &quot;stdafx.h&quot;

#using &lt;mscorlib.dll&gt;

using namespace System;

int _tmain()
{
    // TODO: Please replace the sample code below with your own.
    double number __gc[] = { 79.31, 47.59, 749.75, 9.07, 9.44 };

    Console::WriteLine(S&quot;Value 1:  {0}&quot;, __box(number[0]));
    Console::WriteLine(S&quot;Value 2:  {0}&quot;, __box(number[1]));
    Console::WriteLine(S&quot;Value 3:  {0}&quot;, __box(number[2]));
    Console::WriteLine(S&quot;Value 4:  {0}&quot;, __box(number[3]));
    Console::WriteLine(S&quot;Value 5:  {0}&quot;, __box(number[4]));

    Console::WriteLine(S&quot;&quot;);
    return 0;
}</pre>
<p>This would produce:</p>
<pre>Value 1:  79.31
Value 2:  47.59
Value 3:  749.75
Value 4:  9.07
Value 5:  9.44

Press any key to continue</pre>
<h2>Multidimensional Arrays</h2>
<h2>Introduction</h2>
<p>The arrays we have used so far are referred to as single or one-dimensional arrays because they use a single group of items that can classified in a single column. In some cases, you may want to divided the list in two or more sections. For example, here is a list of numbers divided in various columns: </p>
<p>Sunday<br />
  <br />Monday</p>
<p>Tuesday</p>
<p>Wednesday</p>
<p>Thursday</p>
<p>Friday</p>
<p>Saturday</p>
<p>Week 1<br />
  <br />1</p>
<p>2</p>
<p>3</p>
<p>4</p>
<p>5</p>
<p>6</p>
<p>7</p>
<p>Week 2<br />
  <br />8</p>
<p>9</p>
<p>10</p>
<p>11</p>
<p>12</p>
<p>13</p>
<p>14</p>
<p>Week 3<br />
  <br />15</p>
<p>16</p>
<p>17</p>
<p>18</p>
<p>19</p>
<p>20</p>
<p>21</p>
<p>Week 4<br />
  <br />22</p>
<p>23</p>
<p>24</p>
<p>25</p>
<p>26</p>
<p>27</p>
<p>28</p>
<p>A multi-dimensional array is one that still includes items of the same type but the group is made of sub-divisions following an arrangement an arrangement of your choice. </p>
<h2>Two-Dimensional Arrays</h2>
<p>Imagine you create two lists of items declared in two variables as follows: </p>
<pre>// This is the main project file for VC++ application project
// generated using an Application Wizard.

#include &quot;stdafx.h&quot;

#using &lt;mscorlib.dll&gt;

using namespace System;

int _tmain()
{
    // TODO: Please replace the sample code below with your own.
    <b>Double number1 __gc[] = new Double __gc[5];
    Double number2 __gc[] = new Double __gc[5];</b>

    number1[0] = 643.18;
    number1[1] = 9.64;
    number1[2] = -48.05;
    number1[3] = 14.26;
    number1[4] = 62.55;

    number2[0] = 124.45;
    number2[1] = -62.84;
    number2[2] = 508.06;
    number2[3] = 92.68;
    number2[4] = -44.82;

    Console::WriteLine(S&quot;Value 1:  {0}&quot;, __box(number1[0]));
    Console::WriteLine(S&quot;Value 2:  {0}&quot;, __box(number1[1]));
    Console::WriteLine(S&quot;Value 3:  {0}&quot;, __box(number1[2]));
    Console::WriteLine(S&quot;Value 4:  {0}&quot;, __box(number1[3]));
    Console::WriteLine(S&quot;Value 5:  {0}&quot;, __box(number1[4]));

    Console::WriteLine(S&quot;Value 6:  {0}&quot;, __box(number2[0]));
    Console::WriteLine(S&quot;Value 7:  {0}&quot;, __box(number2[1]));
    Console::WriteLine(S&quot;Value 8:  {0}&quot;, __box(number2[2]));
    Console::WriteLine(S&quot;Value 9:  {0}&quot;, __box(number2[3]));
    Console::WriteLine(S&quot;Value 10: {0}&quot;, __box(number2[4]));

    Console::WriteLine(S&quot;&quot;);
    return 0;
}</pre>
<p>Instead of having two lists that represent the same types of values, you can declare a single array variable and divide its list of items in two. A two-dimensional array is an array made of two lists of items of the same data type. To declare a two-dimensional array, use the following formula: </p>
<pre><i>DataType</i> <i>ArrayName</i>[,] = new <i>DataType</i>[<i>Dimension1,Dimension2</i>];</pre>
<p>Once again, you can optionally type the __gc keyword to the left of square brackets. The square brackets to the left of the assignment operator must have only a comma. Inside of the square brackets to the right side of the assignment, you must specify two numbers separated by a comma. The first number indicates the number of lists that the array contains. For a two-dimensional array, this number must be 2. The right value indicates the number of items that each list would contain. Here is an example: </p>
<pre>// This is the main project file for VC++ application project
// generated using an Application Wizard.

#include &quot;stdafx.h&quot;

#using &lt;mscorlib.dll&gt;

using namespace System;

int _tmain()
{
    // TODO: Please replace the sample code below with your own.
    Double number __gc[,] = new Double __gc[2,5];

    Console::WriteLine(S&quot;&quot;);
    return 0;
}</pre>
<p>In this case, the number variable is a two-dimensional array. In other words, the variable represents two lists. Each list contains 5 numbers. </p>
<p>To access a member of the array, type the name of the variable followed by the square brackets. Inside of the brackets, type the zero-based index of the desired list. The first list has an index of 0 while the second list has an index of 1. Then type the comma. On the right side of the comma, type the zero-based index of the member you want to access. Once you have accessed a member of the array, you can either assign it a value or retrieve the value it is currently holding. Here are examples: </p>
<pre>// This is the main project file for VC++ application project
// generated using an Application Wizard.

#include &quot;stdafx.h&quot;

#using &lt;mscorlib.dll&gt;

using namespace System;

int _tmain()
{
    // TODO: Please replace the sample code below with your own.
    Double number __gc[,] = new Double __gc[2,5];

    number[0,0] = 643.18;
    number[0,1] = 9.64;
    number[0,2] = -48.05;
    number[0,3] = 14.26;
    number[0,4] = 62.55;

    number[1,0] = 124.45;
    number[1,1] = -62.84;
    number[1,2] = 508.06;
    number[1,3] = 92.68;
    number[1,4] = -44.82;

    Console::WriteLine(S&quot;Value 1:  {0}&quot;, __box(number[0,0]));
    Console::WriteLine(S&quot;Value 2:  {0}&quot;, __box(number[0,1]));
    Console::WriteLine(S&quot;Value 3:  {0}&quot;, __box(number[0,2]));
    Console::WriteLine(S&quot;Value 4:  {0}&quot;, __box(number[0,3]));
    Console::WriteLine(S&quot;Value 5:  {0}&quot;, __box(number[0,4]));

    Console::WriteLine(S&quot;Value 6:  {0}&quot;, __box(number[1,0]));
    Console::WriteLine(S&quot;Value 7:  {0}&quot;, __box(number[1,1]));
    Console::WriteLine(S&quot;Value 8:  {0}&quot;, __box(number[1,2]));
    Console::WriteLine(S&quot;Value 9:  {0}&quot;, __box(number[1,3]));
    Console::WriteLine(S&quot;Value 10: {0}&quot;, __box(number[1,4]));

    Console::WriteLine(S&quot;&quot;);
    return 0;
}</pre>
<p>This would produce: </p>
<pre>Value 1:  643.18
Value 2:  9.64
Value 3:  -48.05
Value 4:  14.26
Value 5:  62.55
Value 6:  124.45
Value 7:  -62.84
Value 8:  508.06
Value 9:  92.68
Value 10: -44.82

Press any key to continue</pre>
<h2>Multi-Dimensional Arrays</h2>
<p>A multi-dimensional array is a list of a list of a list, etc. In other words, it is a list that contains other lists while these other lists may contain other lists. For example, a three-dimensional array is one that contains 3 lists; each of the 3 lists contains a certain number of lists; then each list inside contains a list of items. </p>
<p>To declare a multi-dimensional array, in the square brackets, type the number of comma minus the total number of lists. For example, the square brackets of a three-dimensional array would have 2 commas. The square brackets of a 5-dimensional array would have 4 commas. In the square brackets on the right side of the assignment operator must indicate the dimension of each level of list. Consider the following declaration: </p>
<pre>// This is the main project file for VC++ application project
// generated using an Application Wizard.

#include &quot;stdafx.h&quot;

#using &lt;mscorlib.dll&gt;

using namespace System;

int _tmain()
{
    // TODO: Please replace the sample code below with your own.
    Double hoursWorked __gc[,,] = new Double[6,3,5];

    Console::WriteLine();
    return 0;
}</pre>
<p>In this declaration, the variable represents 6 lists. Each of the 6 lists is made of its own 3 lists. Each of the 3 lists contains 5 items. Another ways to illustrate this is that the array represents 6 months work for a contractor. In each of the six months, the contractor worked for 3 weeks. In each of the 3 weeks, the contractor worked for 5 days. </p>
<p>You can access each member of a multi-dimensional array using its index. In the above example, the first month has an index of 0 while the third month has an index of 2. Everything else is as done for the other dimensional arrays. </p>
<p>The String as an Array and Characters</p>
<h2>An Array of Characters</h2>
<p>In C/C++, you can declare a string variable as an array of characters. Here is an example: </p>
<pre>// This is the main project file for VC++ application project
// generated using an Application Wizard.

#include &quot;stdafx.h&quot;

#using &lt;mscorlib.dll&gt;

using namespace System;

int _tmain()
{
    // TODO: Please replace the sample code below with your own.
    char School[] = &quot;American College of Computer &amp; Information Sciences&quot;;

    Console::Write(&quot;School Name: &quot;);
    Console::WriteLine(School);

    Console::WriteLine(S&quot;&quot;);
    return 0;
}</pre>
<p>This would produce: </p>
<pre>School Name: American College of Computer &amp; Information Sciences

Press any key to continue</pre>
<p>In various previous examples, we used the .NET&#8217;s <b>String</b> class to declare and process string variables. In some cases, you will need access to one character inside of a string. In C/C++, if you declare a string using an array of characters as done above, to access an individual character, you type the name of the variable followed by square brackets. Inside of the square brackets, you can type the 0-based index of the character you want to access. Remember that the C/C++ <b>char</b> data type is an 8-bit value (to access a character, you should use its <b>Char</b> or <b>__wchar_t</b> type). Here is an example: </p>
<pre>// This is the main project file for VC++ application project
// generated using an Application Wizard.

#include &quot;stdafx.h&quot;

#using &lt;mscorlib.dll&gt;

using namespace System;

int _tmain()
{
    // TODO: Please replace the sample code below with your own.
    char School[] = &quot;American College of Computer &amp; Information Sciences&quot;;
    Char First    = School[0];
    Char Second   = School[9];
    Char Third    = School[20];
    Char Fourth   = School[31];
    Char Fifth    = School[43];

    Console::Write(&quot;School Name: &quot;);
    Console::WriteLine(School);
    Console::WriteLine(S&quot;Abbreviation: {0}{1}{2}{3}{4}&quot;,
	              __box(First), __box(Second), __box(Third),
	              __box(Fourth), __box(Fifth));

    Console::WriteLine(S&quot;&quot;);
    return 0;
}</pre>
<p>This would produce: </p>
<pre>School Name: American College of Computer &amp; Information Sciences
Abbreviation: ACCIS

Press any key to continue</pre>
<p>The Characters of a String Class Value</p>
<p>Many libraries that feature a string class, such as the ATL&#8217;s <b>basic_string</b> class, the MFC&#8217;s <b>CString</b> class, or the VCL&#8217;s <b>AnsiString</b> class provide you the same mechanism (by overloading the [] operator). The Microsoft .NET Framework&#8217;s <b>String</b> class provides a different functionality. If you declare a String variable and want to access any of its characters, the String class provides a Chars property that uses square brackets to locate a character. This is done using the same approach as above except that you must call the Chars property. Here is an example of how this can be done: </p>
<pre>// This is the main project file for VC++ application project
// generated using an Application Wizard.

#include &quot;stdafx.h&quot;

#using &lt;mscorlib.dll&gt;

using namespace System;

int _tmain()
{
    // TODO: Please replace the sample code below with your own.
    String* School = &quot;American College of Computer &amp; Information Sciences&quot;;
    Char First    = School-&gt;Chars[0];
    Char Second   = School-&gt;Chars[9];
    Char Third    = School-&gt;Chars[20];
    Char Fourth   = School-&gt;Chars[31];
    Char Fifth    = School-&gt;Chars[43];

    Console::Write(&quot;School Name: &quot;);
    Console::WriteLine(School);
    Console::WriteLine(S&quot;Abbreviation: {0}{1}{2}{3}{4}&quot;,
	              __box(First), __box(Second), __box(Third),
	              __box(Fourth), __box(Fifth));

    Console::WriteLine(S&quot;&quot;);
    return 0;
}</pre>
<p>It would produce the same result.</p>
<h2>Arrays and Functions</h2>
<h3>An Array as Argument</h3>
<p>An array can be passed to a function as argument. An array can also be returned by a function. To specify that a function takes an array as argument, declare the function as you would do for any regular function. In the parentheses of the function, declare the array with the __gc keyword as you have done so far. Here is an example: </p>
<pre>// This is the main project file for VC++ application project
// generated using an Application Wizard.

#include &quot;stdafx.h&quot;

#using &lt;mscorlib.dll&gt;

using namespace System;

void ShowNumbers(double n __gc[]);

int _tmain()
{
    // TODO: Please replace the sample code below with your own.

    Console::WriteLine(S&quot;&quot;);
    return 0;
}</pre>
<p>When implementing the function, proceed as you would do normally. If you want to access the members of the array, use the same technique we have used so far, accessing each member of the array by its index. When you call the function from another function, simply pass the name of the array as argument. This would be done as follows:</p>
<pre>// This is the main project file for VC++ application project
// generated using an Application Wizard.

#include &quot;stdafx.h&quot;

#using &lt;mscorlib.dll&gt;

using namespace System;

void ShowNumbers(double n __gc[]);

int _tmain()
{
    // TODO: Please replace the sample code below with your own.
    Double number[]= { 643.18, 9.64, 48.05, 14.26, 62.55 };

    ShowNumbers(number);

    Console::WriteLine(S&quot;&quot;);
    return 0;
}

void ShowNumbers(double Nbr __gc[])
{
    Console::WriteLine(S&quot;Value 1: {0}&quot;, __box(Nbr[0]));
    Console::WriteLine(S&quot;Value 2: {0}&quot;, __box(Nbr[1]));
    Console::WriteLine(S&quot;Value 3: {0}&quot;, __box(Nbr[2]));
    Console::WriteLine(S&quot;Value 4: {0}&quot;, __box(Nbr[3]));
    Console::WriteLine(S&quot;Value 5: {0}&quot;, __box(Nbr[4]));
}</pre>
<p>This would produce: </p>
<pre>Value 1: 643.18
Value 2: 9.64
Value 3: 48.05
Value 4: 14.26
Value 5: 62.55

Press any key to continue</pre>
<p>If you create a function that takes a non-managed array as argument, you usually would pass an additional argument that carries the size of the array. On the other hand, if you create a function that takes a managed array as argument, since the array would inherit from the Array class, the argument would automatically carry its dimension from the Length property of the Array class. Therefore, with a managed array as argument, you don&#8217;t have to specify the number of members to the function; the compiler would figure that out. </p>
<h3>Returning an Array</h3>
<p>Whether in C/C++ or Managed C++, an array is (always) passed as a reference. Although in C/C++, there is no formal way to return an array from a function, because an array is passed as a reference, it is very easy to return an array. Actually, this technique is used in C++ as an accessory, not as a means. Managed C++ formal features the idea of returning an array from a function. Although this is a new feature not available in C/C++, it is very easy to use, so easy that there is very little effort to implement this valuable addition. </p>
<p>To indicate that a function would return an array, if you are only declaring the function, type empty square brackets between the function&#8217;s parentheses and its semi-colon. Here is an example: </p>
<pre>// This is the main project file for VC++ application project
// generated using an Application Wizard.

#include &quot;stdafx.h&quot;

#using &lt;mscorlib.dll&gt;

using namespace System;

Double GetNumbers()[];

void ShowNumbers(double n __gc[]);

int _tmain()
{
    // TODO: Please replace the sample code below with your own.

    Console::WriteLine(S&quot;&quot;);
    return 0;
}</pre>
<p>When defining the function, type the square brackets between the function&#8217;s parentheses and its opening curly bracket. In the body of the function, you can declare an array, do what you want with it, and then return it as the return value of the function.</p>
<p>When calling a function that returns an array, proceed with the rules you would use for any other function. For example, you can call the function in another and use it as you see fit. You can also assign the function to an array variable in order to initialize the variable. Here is an example:</p>
<pre>// This is the main project file for VC++ application project
// generated using an Application Wizard.

#include &quot;stdafx.h&quot;

#using &lt;mscorlib.dll&gt;

using namespace System;

Double GetNumbers()[];

void ShowNumbers(double n __gc[]);

int _tmain()
{
    // TODO: Please replace the sample code below with your own.
    <b>Double no __gc[] = GetNumbers();</b>
    ShowNumbers(no);

    Console::WriteLine(S&quot;&quot;);
    return 0;
}

Double GetNumbers()<b>[]</b>
{
    Double number[] = { 643.18, 9.64, 48.05, 14.26, 62.55 };

    return number;
}

void ShowNumbers(double Nbr __gc[])
{
    Console::WriteLine(S&quot;Value 1: {0}&quot;, __box(Nbr[0]));
    Console::WriteLine(S&quot;Value 2: {0}&quot;, __box(Nbr[1]));
    Console::WriteLine(S&quot;Value 3: {0}&quot;, __box(Nbr[2]));
    Console::WriteLine(S&quot;Value 4: {0}&quot;, __box(Nbr[3]));
    Console::WriteLine(S&quot;Value 5: {0}&quot;, __box(Nbr[4]));
}</pre>
<p class="facebook"><a href="http://www.facebook.com/share.php?u=http://tayyab.xenoglaux-solutions.com/2010/04/25/arrays-in-c-2/" target="_blank"><img src="http://tayyab.xenoglaux-solutions.com/wp-content/plugins/add-to-facebook-plugin/facebook_share_icon.gif" alt="Share on Facebook" title="Share on Facebook" /></a><a href="http://www.facebook.com/share.php?u=http://tayyab.xenoglaux-solutions.com/2010/04/25/arrays-in-c-2/" target="_blank" title="Share on Facebook">Share on Facebook</a></p>]]></content:encoded>
			<wfw:commentRss>http://tayyab.xenoglaux-solutions.com/2010/04/25/arrays-in-c-2/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>How to Easily Download Large Number of Images from Flicker.com</title>
		<link>http://tayyab.xenoglaux-solutions.com/2010/01/21/how-to-easily-download-large-number-of-images-from-flicker-com/</link>
		<comments>http://tayyab.xenoglaux-solutions.com/2010/01/21/how-to-easily-download-large-number-of-images-from-flicker-com/#comments</comments>
		<pubDate>Thu, 21 Jan 2010 17:24:52 +0000</pubDate>
		<dc:creator>tayyabtariq</dc:creator>
				<category><![CDATA[C++]]></category>
		<category><![CDATA[Misc]]></category>
		<category><![CDATA[dowload flickr.com images. flash get]]></category>
		<category><![CDATA[Flickr.com]]></category>

		<guid isPermaLink="false">http://tayyab.xenoglaux-solutions.com/2010/01/21/how-to-easily-download-large-number-of-images-from-flicker-com/</guid>
		<description><![CDATA[Recently I needed some images to create a dataset for a project I am doing. While Flickr.com seemed to be the best source. All i needed to do was to put in a query and a large number of relevant images would show up. But downloading original images from Flickr.com can be bit of a [...]]]></description>
			<content:encoded><![CDATA[<p>Recently I needed some images to create a dataset for a project I am doing. While Flickr.com seemed to be the best source. All i needed to do was to put in a query and a large number of relevant images would show up. But downloading original images from Flickr.com can be bit of a mess, let alone a large number of images.</p>
<p>So I came up with a simple three step process to download all the images on a given page easily.</p>
<p>1. Save the html source of the <a href="http://flickr.com" target="_blank">Flickr.com</a> page.</p>
<p>2. Give the source to <a href="http://tayyab.xenoglaux-solutions.com/wp-content/uploads/2010/01/FlickrDownload1.txt" target="_blank">this</a> C++ code and get download links to all images.</p>
<p>3. Download all the images using <a href="http://download.cnet.com/windows/3055-2155_4-170652.html?tag=pdl-redir">FlashGet</a>.</p>
<p>PS: If someone could write a Java script of this C++ code and write a wrapper to automatically get the HTML source, the above can be reduced to just one step.</p>
<p class="facebook"><a href="http://www.facebook.com/share.php?u=http://tayyab.xenoglaux-solutions.com/2010/01/21/how-to-easily-download-large-number-of-images-from-flicker-com/" target="_blank"><img src="http://tayyab.xenoglaux-solutions.com/wp-content/plugins/add-to-facebook-plugin/facebook_share_icon.gif" alt="Share on Facebook" title="Share on Facebook" /></a><a href="http://www.facebook.com/share.php?u=http://tayyab.xenoglaux-solutions.com/2010/01/21/how-to-easily-download-large-number-of-images-from-flicker-com/" target="_blank" title="Share on Facebook">Share on Facebook</a></p>]]></content:encoded>
			<wfw:commentRss>http://tayyab.xenoglaux-solutions.com/2010/01/21/how-to-easily-download-large-number-of-images-from-flicker-com/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Two values one address&#8212;A challenge for programmers</title>
		<link>http://tayyab.xenoglaux-solutions.com/2009/07/15/two-values-one-address/</link>
		<comments>http://tayyab.xenoglaux-solutions.com/2009/07/15/two-values-one-address/#comments</comments>
		<pubDate>Tue, 14 Jul 2009 19:12:58 +0000</pubDate>
		<dc:creator>tayyabtariq</dc:creator>
				<category><![CDATA[C++]]></category>
		<category><![CDATA[change constant]]></category>
		<category><![CDATA[constant integer]]></category>
		<category><![CDATA[constant pointer]]></category>

		<guid isPermaLink="false">http://tayyab.xenoglaux-solutions.com/2009/07/15/two-values-one-address/</guid>
		<description><![CDATA[Have a look at this code, it shows some astonishing results.


   1: #include &#60;iostream.h&#62;

   2: using namespace std;

   3:&#160; 

   4: int main()

   5: {

   6:     const int x = 10;

   7:     int [...]]]></description>
			<content:encoded><![CDATA[<p>Have a look at this code, it shows some astonishing results.</p>
<div id="codeSnippetWrapper" style="border-right: silver 1px solid; padding-right: 4px; border-top: silver 1px solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin: 20px 0px 10px; overflow: auto; border-left: silver 1px solid; width: 97.5%; cursor: text; direction: ltr; max-height: 200px; line-height: 12pt; padding-top: 4px; border-bottom: silver 1px solid; font-family: &#39;Courier New&#39;, courier, monospace; height: 263px; background-color: #f4f4f4; text-align: left">
<div id="codeSnippet" style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; text-align: left; border-bottom-style: none">
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; text-align: left; border-bottom-style: none"><span id="lnum1" style="color: #606060">   1:</span> <span style="color: #cc6633">#include</span> &lt;iostream.h&gt;</pre>
<p><!--CRLF--></p>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; text-align: left; border-bottom-style: none"><span id="lnum2" style="color: #606060">   2:</span> <span style="color: #0000ff">using</span> <span style="color: #0000ff">namespace</span> std;</pre>
<p><!--CRLF--></p>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; text-align: left; border-bottom-style: none"><span id="lnum3" style="color: #606060">   3:</span>&#160; </pre>
<p><!--CRLF--></p>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; text-align: left; border-bottom-style: none"><span id="lnum4" style="color: #606060">   4:</span> <span style="color: #0000ff">int</span> main()</pre>
<p><!--CRLF--></p>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; text-align: left; border-bottom-style: none"><span id="lnum5" style="color: #606060">   5:</span> {</pre>
<p><!--CRLF--></p>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; text-align: left; border-bottom-style: none"><span id="lnum6" style="color: #606060">   6:</span>     <span style="color: #0000ff">const</span> <span style="color: #0000ff">int</span> x = 10;</pre>
<p><!--CRLF--></p>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; text-align: left; border-bottom-style: none"><span id="lnum7" style="color: #606060">   7:</span>     <span style="color: #0000ff">int</span> * ptr;</pre>
<p><!--CRLF--></p>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; text-align: left; border-bottom-style: none"><span id="lnum8" style="color: #606060">   8:</span>     ptr = (<span style="color: #0000ff">int</span> *)( &amp;x );    <span style="color: #008000">//make the pointer to constant int*</span></pre>
<p><!--CRLF--></p>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; text-align: left; border-bottom-style: none"><span id="lnum9" style="color: #606060">   9:</span>     </pre>
<p><!--CRLF--></p>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; text-align: left; border-bottom-style: none"><span id="lnum10" style="color: #606060">  10:</span>     *ptr = 8;               <span style="color: #008000">//change the value of the constant using the pointer.</span></pre>
<p><!--CRLF--></p>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; text-align: left; border-bottom-style: none"><span id="lnum11" style="color: #606060">  11:</span>&#160; </pre>
<p><!--CRLF--></p>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; text-align: left; border-bottom-style: none"><span id="lnum12" style="color: #606060">  12:</span>     <span style="color: #008000">//here is the real surprising part</span></pre>
<p><!--CRLF--></p>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; text-align: left; border-bottom-style: none"><span id="lnum13" style="color: #606060">  13:</span>     cout&lt;&lt;<span style="color: #006080">&quot;x: &quot;</span>&lt;&lt;x&lt;&lt;endl;          <span style="color: #008000">//prints 10, means value is not changed</span></pre>
<p><!--CRLF--></p>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; text-align: left; border-bottom-style: none"><span id="lnum14" style="color: #606060">  14:</span>     cout&lt;&lt;<span style="color: #006080">&quot;*ptr: &quot;</span>&lt;&lt;*ptr&lt;&lt;endl;    <span style="color: #008000">//prints 8, means value is changed</span></pre>
<p><!--CRLF--></p>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; text-align: left; border-bottom-style: none"><span id="lnum15" style="color: #606060">  15:</span>     cout&lt;&lt;<span style="color: #006080">&quot;ptr: &quot;</span>&lt;&lt;(<span style="color: #0000ff">int</span>)ptr&lt;&lt;endl; <span style="color: #008000">//prints some address lets say 0xfadc02</span></pre>
<p><!--CRLF--></p>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; text-align: left; border-bottom-style: none"><span id="lnum16" style="color: #606060">  16:</span>     cout&lt;&lt;<span style="color: #006080">&quot;&amp;x: &quot;</span>&lt;&lt;(<span style="color: #0000ff">int</span>)&amp;x&lt;&lt;endl;   <span style="color: #008000">//prints the same address, i.e. 0xfadc02</span></pre>
<p><!--CRLF--></p>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; text-align: left; border-bottom-style: none"><span id="lnum17" style="color: #606060">  17:</span>     <span style="color: #008000">//This means that x resides at the same location ptr points to yet </span></pre>
<p><!--CRLF--></p>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; text-align: left; border-bottom-style: none"><span id="lnum18" style="color: #606060">  18:</span>     <span style="color: #008000">//two different values are printed, I cant understand this.</span></pre>
<p><!--CRLF--></p>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; text-align: left; border-bottom-style: none"><span id="lnum19" style="color: #606060">  19:</span>     </pre>
<p><!--CRLF--></p>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; text-align: left; border-bottom-style: none"><span id="lnum20" style="color: #606060">  20:</span>     <span style="color: #0000ff">return</span> 0;</pre>
<p><!--CRLF--></p>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; text-align: left; border-bottom-style: none"><span id="lnum21" style="color: #606060">  21:</span> }</pre>
<p><!--CRLF--></div>
</div>
<p>If anyone knows the reason, please post it here.</p>
<p class="facebook"><a href="http://www.facebook.com/share.php?u=http://tayyab.xenoglaux-solutions.com/2009/07/15/two-values-one-address/" target="_blank"><img src="http://tayyab.xenoglaux-solutions.com/wp-content/plugins/add-to-facebook-plugin/facebook_share_icon.gif" alt="Share on Facebook" title="Share on Facebook" /></a><a href="http://www.facebook.com/share.php?u=http://tayyab.xenoglaux-solutions.com/2009/07/15/two-values-one-address/" target="_blank" title="Share on Facebook">Share on Facebook</a></p>]]></content:encoded>
			<wfw:commentRss>http://tayyab.xenoglaux-solutions.com/2009/07/15/two-values-one-address/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Generating Documentation from XML Comments in C#</title>
		<link>http://tayyab.xenoglaux-solutions.com/2009/06/04/generating-documentation-from-xml-comments-in-c/</link>
		<comments>http://tayyab.xenoglaux-solutions.com/2009/06/04/generating-documentation-from-xml-comments-in-c/#comments</comments>
		<pubDate>Thu, 04 Jun 2009 18:28:35 +0000</pubDate>
		<dc:creator>tayyabtariq</dc:creator>
				<category><![CDATA[C++]]></category>
		<category><![CDATA[Visual Studion .NET]]></category>
		<category><![CDATA[Comments]]></category>
		<category><![CDATA[Generate Documentation]]></category>
		<category><![CDATA[Visual Studio.NET]]></category>
		<category><![CDATA[XML Comments]]></category>

		<guid isPermaLink="false">http://tayyab.xenoglaux-solutions.com/2009/06/04/generating-documentation-from-xml-comments-in-c/</guid>
		<description><![CDATA[C# allows developers to embed XML comments into their source files—a useful facility, especially when more than one programmer is working on the same code. The C# parser can expand these XML tags to provide additional information and export them to an external document for further processing. This article shows how to use XML comments [...]]]></description>
			<content:encoded><![CDATA[<p align="justify">C# allows developers to embed XML comments into their source files—a useful facility, especially when more than one programmer is working on the same code. The C# parser can expand these XML tags to provide additional information and export them to an external document for further processing. This article shows how to use XML comments and explains the relevant tags. The author demonstrates how to set up your project to export your XML comments into convenient documentation for the benefit of other developers. He also shows how to use comments to generate help files using <a href="http://sourceforge.net/project/showfiles.php?group_id=36057" target="_blank">nDoc</a>.</p>
<div align="justify"><span id="more-107"></span></div>
<p align="justify">Wouldn&#8217;t it be nice if all this information were kept in one location? The obvious place to put this information is in the source code itself; this makes it easy to modify the docs along with the code. But it is hard enough to go through someone else&#8217;s program when you know how to code, let alone when you are less technically savvy. Also, adding code documentation takes time.   <br />This article will show you how to solve many of those problems using XML comments. Code comments, user manuals, developer manuals, test plans, and many other documents can be generated from a single source using XML tags. I&#8217;ll explain how to insert XML comments and enable exportation of these comments to another file. I&#8217;ll then discuss each of the available tags and the format of the XML file where the tags are used and walk through an example of using XML comments and XSLT to generate help files.</p>
<h2>XML Comments</h2>
<p>All XML comments begin with three forward slashes (///). The first two slashes signify a comment and tell the compiler to ignore the text that follows. The third slash tells the parser that this is an XML comment and should be handled appropriately.   <br />&#160;&#160;&#160;&#160;&#160; When the developer types the three forward slashes, the Microsoft® Visual Studio® .NET IDE checks to see if it precedes an identifiable type or type member definition. If it is identifiable, then the Visual Studio .NET IDE will automatically insert a few comment tags. The developer then adds additional tags and values as needed. For example, here are the XML tags that are generated when the three forward slashes are entered before this member function that has a parameter:</p>
<div>
<pre><code>/// &lt;summary&gt;
///
/// &lt;/summary&gt;
/// &lt;param name=&quot;strFilePath&quot;&gt;&lt;/param&gt;
public void LoadXMLFromFile(string strFilePath)
</code></pre>
</div>
<p align="justify">The tags inserted here represent only a couple of many that the Visual Studio .NET IDE already knows.</p>
<p align="justify">While the current implementation of IntelliSense® for XML comments does not show all the tags listed in the C# specification, the missing tags can always be inserted manually.<br />
  <br />&#160;&#160;&#160;&#160;&#160; Once comments are inserted, it would be useful to do something with them. If you set things up correctly (as I&#8217;ll explain shortly), this usually just means exporting all the text that comes after the three forward slashes into an external file.</p>
<p>&#160;&#160;&#160;&#160;&#160; There are also several predefined XML comment tags that the parser knows. When these tags are encountered (and have been used correctly by the developer), the parser will format the tags&#8217; output to a text file. If a tag that is not predefined is encountered, it is just written verbatim along with any text to the external file. This means that you can make up your own tags or you can even use known tags recognizable from another source (such as HTML) in the comments.</p>
<p>&#160;&#160;&#160;&#160;&#160; When exportation is set up correctly, the C# parser extracts the XML comments, does formatting if needed, and places them into an XML file of your choice. You can set the location and file name of the XML document file, and enable exportation, through your project&#8217;s property pages. To do this, follow these four steps:</p>
<ul>
<li>
<div align="justify">Open the property page for the project, usually by right-clicking on the project in the Solution Explorer, and click Properties.</div>
</li>
<li>
<div align="justify">After the dialog has opened, click the Configuration Properties folder.</div>
</li>
<li>
<div align="justify">Click the Build option.</div>
</li>
<li>
<div align="justify">In the right pane, there will be a property field called XML Documentation File. Set this to the path and file name of the desired file. The path entered is relative to the project directory, not absolute.</div>
</li>
</ul>
<h2>Recognized Tags</h2>
<p align="justify">I have classified the XML comment tags into two categories. The first set of tags, which I&#8217;ll call primary tags, always starts a group of XML comment tags. They are never embedded within other tags. The second set contains tags that are used within primary tags as modifiers to the text. I call them support tags. </p>
<p>here are nine primary tags: &lt;remarks&gt;, &lt;summary&gt;, &lt;example&gt;, &lt;exception&gt;, &lt;param&gt;, &lt;permission&gt;, &lt;returns&gt;, &lt;seealso&gt;, and &lt;include&gt;.<br />
  <br />&#160;&#160;&#160;&#160;&#160; In this context, the &lt;remarks&gt; tag is used to describe a type such as a class: </p>
<div align="justify">
<pre><code>/// &lt;remarks&gt;
/// Class that contains functions to do
/// transformations to help files.
/// &lt;/remarks&gt;
</code></pre>
</div>
<p>&#160;</p>
<p>The C# documentation recommends using &lt;remarks&gt; to describe a type and a &lt;summary&gt; tag to describe a type member.Oddly, if you start a comment before a type using the /// combination, the Visual Studio .NET IDE will still insert a &lt;summary&gt; tag. Therefore, &lt;remarks&gt; tags need to be inserted manually.<br />
  <br />The &lt;summary&gt; tag is the tag found most often in a C# source file. This tag is used to describe type members, including methods, properties, and fields:</p>
<div align="justify">
<pre><code>/// &lt;summary&gt;
/// This XmlDocument based attribute contains the
/// XML documentation for the project.
/// &lt;/summary&gt;
</code></pre>
</div>
<p align="justify">&#160;</p>
<p align="justify">The &lt;summary&gt; tag can describe types as well, but it is not recommended. The XML comment documentation recommends that the &lt;remarks&gt; tag be used for describing types.<br />
  <br />The &lt;example&gt; tag is used to mark the beginning of an example showing how to use the item. The example can be any valid text, but most often it is a snippet of code:</p>
<div align="justify">
<pre><code>/// &lt;example&gt;
/// &lt;code&gt;
/// // create the class that does translations
/// GiveHelpTransforms ght = new GiveHelpTransforms();
/// // have it load our XML into the SourceXML property
/// ght.LoadXMLFromFile(
///  &quot;E:\\Inetpub\\wwwroot\\GiveHelp\\GiveHelpDoc.xml&quot;);
/// &lt;/code&gt;
/// &lt;/example&gt;
</code></pre>
</div>
<p align="justify">&#160;</p>
<p align="justify">If code is used, it is usually marked with a &lt;code&gt; tag. The &lt;code&gt; tag will be discussed in the section on support tags.<br />
  <br />The &lt;exception&gt; tag documents the exceptions, if any, that the item may throw. If more than one exception may be thrown, then more than one of these tags may be used. Unlike most of the tags, the &lt;exception&gt; tag has one attribute, cref. The value of this attribute is the name of the exception that could be thrown. This must be a valid class because the C# parser will verify it and extract the context information to put into the XML documentation. I will explain this later.</p>
<p>I did not throw any exceptions in the code for this article, but here is an example of how the cref attribute can be used with the &lt;exception&gt; tag:</p>
<div align="justify">
<pre><code>/// &lt;exception cref=&quot;SampleException&quot;&gt;
/// Normally, a discussion on any exceptions thrown would go here.
/// &lt;/exception&gt;
</code></pre>
</div>
<p align="justify">&#160;</p>
<p align="justify">The &lt;param&gt; tag describes the parameters of a method or property. It is automatically inserted by the IDE when using the three forward slashes before a method. The &lt;param&gt; tag has one attribute, name, which is simply the same name that the parameter has in the source:</p>
<div align="justify">
<pre><code>/// &lt;param name=&quot;strFilePath&quot;&gt;The path to the file containing the
/// source XML.&lt;/param&gt;
</code></pre>
</div>
<p align="justify">&#160;</p>
<p align="justify">Member access is identified with the &lt;permission&gt; tag. The text that is assigned to it sets permission-related descriptions. There is no requirement for the value for this tag. Permission is one possibility, with values such as public, private, protected, and so on. Scope is another possible value, with information about whether the method is static. However, you are free to put whatever value you must have here.<br />
  <br />The &lt;permission&gt; tag has one attribute—cref. The documentation describes the use of cref in this context as &quot;a reference to a member or field that is available to be called from the current compilation environment.&quot; It is usually set to System.Security.PermissionSet:</p>
<div align="justify">
<pre><code>/// &lt;permission cref=&quot;System.Security.PermissionSet&quot;&gt;Public
/// Access&lt;/permission&gt;
</code></pre>
</div>
<p align="justify">&#160;</p>
<p align="justify">The &lt;returns&gt; tag is similar to the &lt;param&gt; tag, but it&#8217;s used to describe what the method or property returns:</p>
<div align="justify">
<pre><code>/// &lt;returns&gt;The HTML for a list of types based on the XML
/// documentation.&lt;/returns&gt;
</code></pre>
</div>
<p align="justify">&#160;</p>
<p align="justify">The &lt;seealso&gt; tag specifies &quot;other&quot; links related to the same topic. This tag usually does not include a text value, just a cref attribute that specifies a reference to a symbol. This could be a type, member, field, and so on.</p>
<div align="justify">
<pre><code>/// &lt;seealso cref=&quot;GiveMemberListHTMLHelp&quot;/&gt;
</code></pre>
</div>
<p align="justify">&#160;</p>
<p align="justify">The XML compiler will identify the content of the symbol and use it accordingly in the compiled XML documentation. I&#8217;ll discuss this again in the section on the XML documentation file.<br />
  <br />When XML comments are extracted from the code by the compiler, any file specified in the &lt;include&gt; tag will be expanded and the comments inside it will be used as if that had been included inline. Because the compiler finds them, you can keep comments in an external file. This can help organize your code, but then the comments are not readily available. I would never use the &lt;include&gt; tag for this reason, but if your comments are lengthy, you may find the trade-off acceptable.</p>
<p>&#160;&#160;&#160;&#160;&#160; Several attributes must accompany the &lt;include&gt; tag to specify the external file. The file attribute is the name of the file using relative or fully qualified paths. The include file itself is an XML document that holds XML comments. The path attribute is an XPath statement that points to the parent element of the XML comments in the external document, as you can see here:</p>
<div align="justify">
<pre><code>/// &lt;include file='MyXMLCommentFile.xml'
/// path='doc/members/member[@name=&quot;T:MyExampleClass&quot;]/*'/&gt;
public class MyExampleClass
{
/// &lt;include file='MyXMLCommentFile.xml'
   /// path='doc/members/member[@name=&quot;M:MyExampleMethod&quot;]/*'/&gt;
   public string MyExampleMethod(string strReturnThis)
   {
     return strReturnThis;
   }
}
</code></pre>
</div>
<h2>Support Tags</h2>
<p align="justify">&#160;&#160;&#160;&#160;&#160; There are eleven support tags: &lt;c&gt;, &lt;code&gt;, &lt;list&gt;, &lt;listheader&gt;, &lt;item&gt;, &lt;term&gt;, &lt;description&gt;, &lt;para&gt;, &lt;paramref&gt;, &lt;see&gt;, and &lt;value&gt;.<br />
  <br />&#160;&#160;&#160;&#160;&#160; The &lt;c&gt; tag marks a line of text as code. It is usually used inline in descriptive text.</p>
<div align="justify">
<pre><code>/// The source XML is loaded into the &lt;see cref=&quot;SourceXML&quot;/&gt;
/// property (e.g. &lt;c&gt;&lt;I&gt;obj&lt;/I&gt;.SourceXML =
/// &quot;&lt;I&gt;XML goes here&lt;/I&gt;&quot;&lt;/c&gt;).
</code></pre>
</div>
<p align="justify">&#160;</p>
<p align="justify">The &lt;code&gt; tag also defines a section of text as code. It is often used within an &lt;example&gt; tag block (as shown earlier). The &lt;code&gt; tag is similar to the &lt;c&gt; tag, but &lt;c&gt; is used for a single line of code while &lt;code&gt; is used for a block of code. It specifies that the formatting of the text in the comments needs to remain the same:</p>
<div align="justify">
<pre><code>/// &lt;code&gt;
/// // create the class that does translations
/// GiveHelpTransforms ght = new GiveHelpTransforms();
/// // have it load our XML into the SourceXML property
/// ght.LoadXMLFromFile(
///      &quot;E:\\Inetpub\\wwwroot\\GiveHelp\\GiveHelpDoc.xml&quot;);
/// &lt;/code&gt;
</code></pre>
</div>
<p align="justify">&#160;</p>
<p align="justify">The &lt;list&gt; tag is used in other comment tags to define a specialized list. The type of list is defined by the type attribute, which can have the values bullet, number, or table. Within the &lt;list&gt; tag, there are tags that denote the components of the list, as you can see in this example:</p>
<div align="justify">
<pre><code>/// &lt;list type=&quot;table&quot;&gt;
/// &lt;listheader&gt;
/// &lt;term&gt;Help Page&lt;/term&gt;
/// &lt;description&gt;Function to call&lt;/description&gt;
/// &lt;/listheader&gt;
/// &lt;item&gt;&lt;term&gt;List of Types&lt;/term&gt;
/// &lt;description&gt;GiveTypeListHTMLHelp&lt;/description&gt;&lt;/item&gt;
/// &lt;item&gt;&lt;term&gt;List of members&lt;/term&gt;
/// &lt;description&gt;GiveMemberListHTMLHelp&lt;/description&gt;&lt;/item&gt;
/// &lt;item&gt;&lt;term&gt;Help for a single member&lt;/term&gt;
/// &lt;description&gt;GiveMemberHTMLHelp&lt;/description&gt;&lt;/item&gt;
/// &lt;/list&gt;
</code></pre>
</div>
<p align="justify">&#160;</p>
<p align="justify">The &lt;listheader&gt; tag holds header information for the list (see the previous example). It is typically used for a table of list types.<br />
  <br />As you probably guessed, the &lt;item&gt; tag identifies each item in the list. It can stand alone or wrap &lt;term&gt; and &lt;description&gt; tags. The &lt;term&gt; and &lt;description&gt; tags are always children tags of the &lt;listheader&gt; tag or &lt;item&gt; tags. They are always used in pairs and their function is obvious.</p>
<p>The &lt;para&gt; tag is used to identify a new paragraph. It is very similar to the &lt;P&gt; tag in HTML. You should definitely use this tag to break up long comment sections:</p>
<div align="justify">
<pre><code>/// &lt;summary&gt;This is a summary.
/// &lt;para&gt;This is a new paragraph.&lt;/para&gt;
/// &lt;/summary&gt;
</code></pre>
</div>
<p align="justify">&#160;</p>
<p align="justify">If text within a comment needs to have a parameter specially identified, then &lt;paramref&gt; is the tag to use. It is inserted in the text in the location where the parameter text should appear. The main purpose of this tag is to identify the name of a parameter that should be formatted in a special way. It has one attribute, name, which is the name of the parameter that would appear in the text, usually formatted in a special way:</p>
<div align="justify">
<pre><code>/// Loads the XML documentation in the file specified by
/// &lt;paramref&gt;strFilePath&lt;/paramref&gt;.
</code></pre>
</div>
<p align="justify">&#160;</p>
<p align="justify">The &lt;see&gt; tag is used within the text of other comment tags to specify a hyperlink. It is used inline as part of the text and usually just includes one attribute, cref:</p>
<div align="justify">
<pre><code>/// One of the associated member functions (&lt;see
/// cref=&quot;GiveTypeListHTMLHelp&quot;/&gt;,
/// &lt;see cref=&quot;GiveMemberListHTMLHelp&quot;/&gt;, &lt;see
/// cref=&quot;GiveMemberHTMLHelp&quot;/&gt;)
/// is called to initiate and then return the transformation.
</code></pre>
</div>
<p align="justify">&#160;</p>
<p align="justify">The cref attribute specifies a reference to an existing symbol. See the description for the &lt;exception&gt; tag for more information.<br />
  <br />The &lt;value&gt; tag defines the meaning of a property member. It is used just like the &lt;remarks&gt; tag is used for classes and the &lt;summary&gt; tag is used for other members.</p>
<div align="justify">
<pre><code>/// &lt;value&gt;
/// The SourceXML property contains the XML that will be used in
/// the transformations by the member functions for this class.
/// &lt;/value&gt;</code></pre>
</div>
<div align="justify">
<pre><code></code></pre>
</div>
<h2 align="justify"><code><font face="Lucida Sans Unicode">Generating Documentation</font></code></h2>
<p align="justify"><code></code></p>
<p align="justify"><code></code></p>
<div align="right">
<pre><code><font face="Lucida Sans Unicode">Now that you have successfully created an XML file from the comments you need to generate documentation from it. For this purpose you can download and use </font><a href="http://sourceforge.net/project/showfiles.php?group_id=36057" target="_blank"><font face="Lucida Sans Unicode">nDoc</font></a><font face="Lucida Sans Unicode">.</font></code></pre>
</div>
<div align="right">
<pre><code><font face="Lucida Sans Unicode">Once you have downloaded nDoc, you can use its GUI interface to select a binary from your project directory (make sure it is in the debug folder and the .XML is located next to it).</font></code></pre>
</div>
<div align="right">
<pre><code><font face="Lucida Sans Unicode">nDoc allows you to create different types of documentations from simple HTML to MSDN style.</font></code></pre>
</div>
<div align="right">
<pre><code><font face="Lucida Sans Unicode"></font></code></pre>
</div>
<div align="right">
<pre><code><font face="Lucida Sans Unicode">If you need anymore help, please feel free to ask.</font></code></pre>
</div>
<div align="right">
<pre><code><font face="Lucida Sans Unicode">Related Article;</font></code></pre>
</div>
<div align="right">
<pre><a title="http://msdn.microsoft.com/en-us/magazine/cc302121.aspx" href="http://msdn.microsoft.com/en-us/magazine/cc302121.aspx"><font face="Lucida Sans Unicode">http://msdn.microsoft.com/en-us/magazine/cc302121.aspx</font></a></pre>
</div>
<p class="facebook"><a href="http://www.facebook.com/share.php?u=http://tayyab.xenoglaux-solutions.com/2009/06/04/generating-documentation-from-xml-comments-in-c/" target="_blank"><img src="http://tayyab.xenoglaux-solutions.com/wp-content/plugins/add-to-facebook-plugin/facebook_share_icon.gif" alt="Share on Facebook" title="Share on Facebook" /></a><a href="http://www.facebook.com/share.php?u=http://tayyab.xenoglaux-solutions.com/2009/06/04/generating-documentation-from-xml-comments-in-c/" target="_blank" title="Share on Facebook">Share on Facebook</a></p>]]></content:encoded>
			<wfw:commentRss>http://tayyab.xenoglaux-solutions.com/2009/06/04/generating-documentation-from-xml-comments-in-c/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Reversing words in a string</title>
		<link>http://tayyab.xenoglaux-solutions.com/2009/06/03/reversing-words-in-a-string/</link>
		<comments>http://tayyab.xenoglaux-solutions.com/2009/06/03/reversing-words-in-a-string/#comments</comments>
		<pubDate>Wed, 03 Jun 2009 18:16:38 +0000</pubDate>
		<dc:creator>tayyabtariq</dc:creator>
				<category><![CDATA[C++]]></category>
		<category><![CDATA[reverse string]]></category>
		<category><![CDATA[reverse word]]></category>
		<category><![CDATA[string]]></category>

		<guid isPermaLink="false">http://tayyab.xenoglaux-solutions.com/2009/06/03/reversing-words-in-a-string/</guid>
		<description><![CDATA[Problem statement: Given a string you have transform the string such that the order of words in the string is reversed. 
Input: This is a string
Output: string a is This
 
Solution: First of all we consider the simple string reverse function.


   1: void ReverseString(char str[])

   2: {

   3:  [...]]]></description>
			<content:encoded><![CDATA[<p>Problem statement: Given a string you have transform the string such that the order of words in the string is reversed. </p>
<p>Input: This is a string</p>
<p>Output: string a is This</p>
<p> <span id="more-106"></span>
<p>Solution: First of all we consider the simple string reverse function.</p>
<div id="codeSnippetWrapper" style="border-right: silver 1px solid; padding-right: 4px; border-top: silver 1px solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin: 20px 0px 10px; overflow: auto; border-left: silver 1px solid; width: 97.5%; cursor: text; direction: ltr; max-height: 200px; line-height: 12pt; padding-top: 4px; border-bottom: silver 1px solid; font-family: &#39;Courier New&#39;, courier, monospace; background-color: #f4f4f4; text-align: left">
<div id="codeSnippet" style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; text-align: left; border-bottom-style: none">
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; text-align: left; border-bottom-style: none"><span id="lnum1" style="color: #606060">   1:</span> <span style="color: #0000ff">void</span> ReverseString(<span style="color: #0000ff">char</span> str[])</pre>
<p><!--CRLF--></p>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; text-align: left; border-bottom-style: none"><span id="lnum2" style="color: #606060">   2:</span> {</pre>
<p><!--CRLF--></p>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; text-align: left; border-bottom-style: none"><span id="lnum3" style="color: #606060">   3:</span>     <span style="color: #0000ff">int</span> size = strlen(str);</pre>
<p><!--CRLF--></p>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; text-align: left; border-bottom-style: none"><span id="lnum4" style="color: #606060">   4:</span>     <span style="color: #0000ff">for</span> ( <span style="color: #0000ff">int</span> i = 0, j = size - 1; i &lt; j; i++, j-- )</pre>
<p><!--CRLF--></p>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; text-align: left; border-bottom-style: none"><span id="lnum5" style="color: #606060">   5:</span>     {</pre>
<p><!--CRLF--></p>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; text-align: left; border-bottom-style: none"><span id="lnum6" style="color: #606060">   6:</span>         swap( str[i] , str[j] );</pre>
<p><!--CRLF--></p>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; text-align: left; border-bottom-style: none"><span id="lnum7" style="color: #606060">   7:</span>     }</pre>
<p><!--CRLF--></p>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; text-align: left; border-bottom-style: none"><span id="lnum8" style="color: #606060">   8:</span> }</pre>
<p><!--CRLF--></div>
</div>
<p>This function would reverse the entire string irrespective of words.</p>
<p>Input: This is a string</p>
<p>Output: gnirts a si sihT</p>
<p>Our required output however is different. We need ‘string a is This’.</p>
<p>No compare the two;</p>
<p>Output1: gnirts a si sihT</p>
<p>Output2: string a is This</p>
<p>It is evident that we can get Ouput2 from output 1 just by reversing the individual words (Reversing something twice gives the original). Now that we know what to do, lets see how we can go about doing it.</p>
<p>First of all, it would be helpful to slightly modify the above function to work with start and end indexes of the string given.</p>
<div id="codeSnippetWrapper" style="border-right: silver 1px solid; padding-right: 4px; border-top: silver 1px solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin: 20px 0px 10px; overflow: auto; border-left: silver 1px solid; width: 97.5%; cursor: text; direction: ltr; max-height: 200px; line-height: 12pt; padding-top: 4px; border-bottom: silver 1px solid; font-family: &#39;Courier New&#39;, courier, monospace; background-color: #f4f4f4; text-align: left">
<div id="codeSnippet" style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; text-align: left; border-bottom-style: none">
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; text-align: left; border-bottom-style: none"><span id="lnum1" style="color: #606060">   1:</span> <span style="color: #0000ff">void</span> ReverseString(<span style="color: #0000ff">char</span> str[], <span style="color: #0000ff">int</span> start, <span style="color: #0000ff">int</span> end)</pre>
<p><!--CRLF--></p>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; text-align: left; border-bottom-style: none"><span id="lnum2" style="color: #606060">   2:</span> {</pre>
<p><!--CRLF--></p>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; text-align: left; border-bottom-style: none"><span id="lnum3" style="color: #606060">   3:</span>     <span style="color: #0000ff">for</span> ( <span style="color: #0000ff">int</span> i = start, j = end - 1; i &lt; j; i++, j-- )</pre>
<p><!--CRLF--></p>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; text-align: left; border-bottom-style: none"><span id="lnum4" style="color: #606060">   4:</span>     {</pre>
<p><!--CRLF--></p>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; text-align: left; border-bottom-style: none"><span id="lnum5" style="color: #606060">   5:</span>         swap( str[i] , str[j] );</pre>
<p><!--CRLF--></p>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; text-align: left; border-bottom-style: none"><span id="lnum6" style="color: #606060">   6:</span>     }</pre>
<p><!--CRLF--></p>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; text-align: left; border-bottom-style: none"><span id="lnum7" style="color: #606060">   7:</span> }</pre>
<p><!--CRLF--></div>
</div>
<p>Now to reverse an entire string you will call this function with start = 0 and end = strlen(str). But how does this help our&#160; cause? Simple, we will first reverse the entire string and then pass the start and end index of each word to it making the words normal again.</p>
<p>Here is the code:</p>
<div id="codeSnippetWrapper" style="border-right: silver 1px solid; padding-right: 4px; border-top: silver 1px solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin: 20px 0px 10px; overflow: auto; border-left: silver 1px solid; width: 97.5%; cursor: text; direction: ltr; max-height: 200px; line-height: 12pt; padding-top: 4px; border-bottom: silver 1px solid; font-family: &#39;Courier New&#39;, courier, monospace; background-color: #f4f4f4; text-align: left">
<div id="codeSnippet" style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; text-align: left; border-bottom-style: none">
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; text-align: left; border-bottom-style: none"><span id="lnum1" style="color: #606060">   1:</span> <span style="color: #0000ff">void</span> ReverseWordOrder(<span style="color: #0000ff">char</span> str[])</pre>
<p><!--CRLF--></p>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; text-align: left; border-bottom-style: none"><span id="lnum2" style="color: #606060">   2:</span> {</pre>
<p><!--CRLF--></p>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; text-align: left; border-bottom-style: none"><span id="lnum3" style="color: #606060">   3:</span>     <span style="color: #0000ff">int</span> size = strlen(str);</pre>
<p><!--CRLF--></p>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; text-align: left; border-bottom-style: none"><span id="lnum4" style="color: #606060">   4:</span>     <span style="color: #008000">//Reverse the entire string</span></pre>
<p><!--CRLF--></p>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; text-align: left; border-bottom-style: none"><span id="lnum5" style="color: #606060">   5:</span>     ReverseString(str, 0, size);</pre>
<p><!--CRLF--></p>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; text-align: left; border-bottom-style: none"><span id="lnum6" style="color: #606060">   6:</span>     <span style="color: #008000">//Now we have: gnirts a si sihT</span></pre>
<p><!--CRLF--></p>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; text-align: left; border-bottom-style: none"><span id="lnum7" style="color: #606060">   7:</span>     </pre>
<p><!--CRLF--></p>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; text-align: left; border-bottom-style: none"><span id="lnum8" style="color: #606060">   8:</span>     <span style="color: #008000">//Now we will find start and end indexes of words</span></pre>
<p><!--CRLF--></p>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; text-align: left; border-bottom-style: none"><span id="lnum9" style="color: #606060">   9:</span>     <span style="color: #008000">//and pass them to this function</span></pre>
<p><!--CRLF--></p>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; text-align: left; border-bottom-style: none"><span id="lnum10" style="color: #606060">  10:</span>&#160; </pre>
<p><!--CRLF--></p>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; text-align: left; border-bottom-style: none"><span id="lnum11" style="color: #606060">  11:</span>     <span style="color: #0000ff">int</span> i = 0;</pre>
<p><!--CRLF--></p>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; text-align: left; border-bottom-style: none"><span id="lnum12" style="color: #606060">  12:</span>     <span style="color: #0000ff">int</span> start, end;</pre>
<p><!--CRLF--></p>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; text-align: left; border-bottom-style: none"><span id="lnum13" style="color: #606060">  13:</span>     <span style="color: #0000ff">while</span>(i &lt; size)</pre>
<p><!--CRLF--></p>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; text-align: left; border-bottom-style: none"><span id="lnum14" style="color: #606060">  14:</span>     {</pre>
<p><!--CRLF--></p>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; text-align: left; border-bottom-style: none"><span id="lnum15" style="color: #606060">  15:</span>         <span style="color: #008000">//find the first char of the word</span></pre>
<p><!--CRLF--></p>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; text-align: left; border-bottom-style: none"><span id="lnum16" style="color: #606060">  16:</span>         <span style="color: #0000ff">while</span>(str[i] == <span style="color: #006080">' '</span>)</pre>
<p><!--CRLF--></p>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; text-align: left; border-bottom-style: none"><span id="lnum17" style="color: #606060">  17:</span>             i++;</pre>
<p><!--CRLF--></p>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; text-align: left; border-bottom-style: none"><span id="lnum18" style="color: #606060">  18:</span>         start = i;</pre>
<p><!--CRLF--></p>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; text-align: left; border-bottom-style: none"><span id="lnum19" style="color: #606060">  19:</span>         <span style="color: #008000">//look for the end of a word</span></pre>
<p><!--CRLF--></p>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; text-align: left; border-bottom-style: none"><span id="lnum20" style="color: #606060">  20:</span>         <span style="color: #0000ff">while</span>(str[i] != <span style="color: #006080">' '</span> &amp;&amp; str[i] != <span style="color: #006080">'\0'</span>)</pre>
<p><!--CRLF--></p>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; text-align: left; border-bottom-style: none"><span id="lnum21" style="color: #606060">  21:</span>             i++;</pre>
<p><!--CRLF--></p>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; text-align: left; border-bottom-style: none"><span id="lnum22" style="color: #606060">  22:</span>         end = i;</pre>
<p><!--CRLF--></p>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; text-align: left; border-bottom-style: none"><span id="lnum23" style="color: #606060">  23:</span>         ReverseString(str, start, end);</pre>
<p><!--CRLF--></p>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; text-align: left; border-bottom-style: none"><span id="lnum24" style="color: #606060">  24:</span>     }   </pre>
<p><!--CRLF--></p>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; text-align: left; border-bottom-style: none"><span id="lnum25" style="color: #606060">  25:</span> }</pre>
<p><!--CRLF--></div>
</div>
<p>This solution is has a running time of O(n) ( T(n) = 3n to be precise ). The first ReverseString takes n steps, the while loop itself is again has proportional to n steps. Internal call to ReverseString takes n steps in all (Can you tell how?).</p>
<p>Can it be further reduced without using extra memory? Post answers with proof please (for a bonus of course).</p>
<p class="facebook"><a href="http://www.facebook.com/share.php?u=http://tayyab.xenoglaux-solutions.com/2009/06/03/reversing-words-in-a-string/" target="_blank"><img src="http://tayyab.xenoglaux-solutions.com/wp-content/plugins/add-to-facebook-plugin/facebook_share_icon.gif" alt="Share on Facebook" title="Share on Facebook" /></a><a href="http://www.facebook.com/share.php?u=http://tayyab.xenoglaux-solutions.com/2009/06/03/reversing-words-in-a-string/" target="_blank" title="Share on Facebook">Share on Facebook</a></p>]]></content:encoded>
			<wfw:commentRss>http://tayyab.xenoglaux-solutions.com/2009/06/03/reversing-words-in-a-string/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Callbacks, Threads and GUI</title>
		<link>http://tayyab.xenoglaux-solutions.com/2009/05/24/callbacks-threads-and-gui/</link>
		<comments>http://tayyab.xenoglaux-solutions.com/2009/05/24/callbacks-threads-and-gui/#comments</comments>
		<pubDate>Sat, 23 May 2009 20:39:51 +0000</pubDate>
		<dc:creator>tayyabtariq</dc:creator>
				<category><![CDATA[C++]]></category>
		<category><![CDATA[callback]]></category>
		<category><![CDATA[callback functions]]></category>
		<category><![CDATA[GUI]]></category>

		<guid isPermaLink="false">http://tayyab.xenoglaux-solutions.com/2009/05/24/callbacks-threads-and-gui/</guid>
		<description><![CDATA[This article assumes that you have a working knowledge of C++ callbacks. Recently me and a friend encountered a situation where we had to show some data on dialog box that was generated inside of an independent thread. Sharing memory can be a good idea of passing a pointer to the dialog box. However, we [...]]]></description>
			<content:encoded><![CDATA[<p>This article assumes that you have a working knowledge of C++ <a href="http://tayyab.xenoglaux-solutions.com/2009/05/24/callbacks-c/">callbacks</a>. Recently me and a friend encountered a situation where we had to show some data on dialog box that was generated inside of an independent thread. Sharing memory can be a good idea of passing a pointer to the dialog box. However, we could not pass the pointer as we had to keep the dependence to a minimum and he latter resulted in a cyclic reference, because we creating the thread from the dialog box class. What we wanted to do was to be able to make a call back to function as if it were not a member function (so as to reduce dependence). An obvious way to call a member function as a non member using a callback is to make that function static in the dialog class. However, it would create a slight problem, the static function would not be able to use any of the controls on the dialog box, beating us back to square one. </p>
<p>So I though of using the ‘this’ pointer. Obviously the ‘this’ pointer is not static and cannot be used in a static context. As a work around i introduced a static member inside the dialog box class that was a pointer to the dialog box class itself. Since this pointer was static it could be used from the static context of the function. Now to keep this pointer pointing to whatever ‘this’ is pointing to, one can either use the gain focus event of the dialog box or some other event as may be appropriate to make the static pointer equal to ‘this’ pointer. Once this is done the static function can use the static pointer to make changes inside the GUI, while the thread can call this function just like a normal function (non member).</p>
<p>Here is the signature i used to pass the function to the thread.</p>
<div id="codeSnippetWrapper" style="border-right: silver 1px solid; padding-right: 4px; border-top: silver 1px solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin: 20px 0px 10px; overflow: auto; border-left: silver 1px solid; width: 97.5%; cursor: text; direction: ltr; max-height: 200px; line-height: 12pt; padding-top: 4px; border-bottom: silver 1px solid; font-family: &#39;Courier New&#39;, courier, monospace; background-color: #f4f4f4; text-align: left">
<div id="codeSnippet" style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; text-align: left; border-bottom-style: none">
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; text-align: left; border-bottom-style: none"><span id="lnum1" style="color: #606060">   1:</span>&#160; </pre>
<p><!--CRLF--></p>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; text-align: left; border-bottom-style: none"><span id="lnum2" style="color: #606060">   2:</span> <span style="color: #0000ff">void</span> (*printFunction)(<span style="color: #0000ff">char</span> * message);</pre>
<p><!--CRLF--></div>
</div>
<p>This allows any function that accepts a char string and returns void to be used with the thread, allowing it to be used in many a context.</p>
<p>This article was written in very short time, if you find something lacking, please feel free to ask.</p>
<p class="facebook"><a href="http://www.facebook.com/share.php?u=http://tayyab.xenoglaux-solutions.com/2009/05/24/callbacks-threads-and-gui/" target="_blank"><img src="http://tayyab.xenoglaux-solutions.com/wp-content/plugins/add-to-facebook-plugin/facebook_share_icon.gif" alt="Share on Facebook" title="Share on Facebook" /></a><a href="http://www.facebook.com/share.php?u=http://tayyab.xenoglaux-solutions.com/2009/05/24/callbacks-threads-and-gui/" target="_blank" title="Share on Facebook">Share on Facebook</a></p>]]></content:encoded>
			<wfw:commentRss>http://tayyab.xenoglaux-solutions.com/2009/05/24/callbacks-threads-and-gui/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Callbacks C++</title>
		<link>http://tayyab.xenoglaux-solutions.com/2009/05/24/callbacks-c/</link>
		<comments>http://tayyab.xenoglaux-solutions.com/2009/05/24/callbacks-c/#comments</comments>
		<pubDate>Sat, 23 May 2009 20:21:58 +0000</pubDate>
		<dc:creator>tayyabtariq</dc:creator>
				<category><![CDATA[C++]]></category>
		<category><![CDATA[callback]]></category>
		<category><![CDATA[callback functions]]></category>
		<category><![CDATA[function pointers]]></category>

		<guid isPermaLink="false">http://tayyab.xenoglaux-solutions.com/2009/05/24/callbacks-c/</guid>
		<description><![CDATA[This tutorial assumes that the reader has a basic knowledge of C++ function pointers and pointers to class member functions. 
Callback function
A callback function is a function whose pointer is passed to another function and and that function calls the first function using the pointer. When such a call is made, it is said that [...]]]></description>
			<content:encoded><![CDATA[<p>This tutorial assumes that the reader has a basic knowledge of C++ function pointers and pointers to class member functions. </p>
<h3>Callback function</h3>
<p>A callback function is a function whose pointer is passed to another function and and that function calls the first function using the pointer. When such a call is made, it is said that a callback has been made. </p>
<h3>Why use a Callback?</h3>
<p>You must be wondering why use a callback? Consider the scenario where you are implementing a sorting technique based on comparisons. You want it to be generic and allow the user to choose which number goes first (ascending sort or descending sort). To solve this you can have the user pass a function to your sort program and you can use that function to decide which number goes first. Here you are only making a callback and you do not care how or where the function is implemented, neither does the writer of the passed function care who or where the caller is. </p>
<h3>How its done?</h3>
<p>First i create a class for sorting numbers that requires a callback function and makes a callback. </p>
<div id="codeSnippetWrapper" style="border-right: silver 1px solid; padding-right: 4px; border-top: silver 1px solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin: 20px 0px 10px; overflow: auto; border-left: silver 1px solid; width: 97.5%; cursor: text; direction: ltr; max-height: 200px; line-height: 12pt; padding-top: 4px; border-bottom: silver 1px solid; font-family: &#39;Courier New&#39;, courier, monospace; background-color: #f4f4f4; text-align: left">
<div id="codeSnippet" style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; text-align: left; border-bottom-style: none">
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; text-align: left; border-bottom-style: none"><span id="lnum1" style="color: #606060">   1:</span> <span style="color: #0000ff">class</span> SortNumber</pre>
<p><!--CRLF--></p>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; text-align: left; border-bottom-style: none"><span id="lnum2" style="color: #606060">   2:</span> {</pre>
<p><!--CRLF--></p>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; text-align: left; border-bottom-style: none"><span id="lnum3" style="color: #606060">   3:</span> <span style="color: #0000ff">public</span>: </pre>
<p><!--CRLF--></p>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; text-align: left; border-bottom-style: none"><span id="lnum4" style="color: #606060">   4:</span>     SortNumber(<span style="color: #0000ff">int</span> * a, <span style="color: #0000ff">int</span> s)</pre>
<p><!--CRLF--></p>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; text-align: left; border-bottom-style: none"><span id="lnum5" style="color: #606060">   5:</span>     {</pre>
<p><!--CRLF--></p>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; text-align: left; border-bottom-style: none"><span id="lnum6" style="color: #606060">   6:</span>         arr = a;</pre>
<p><!--CRLF--></p>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; text-align: left; border-bottom-style: none"><span id="lnum7" style="color: #606060">   7:</span>         size = s;</pre>
<p><!--CRLF--></p>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; text-align: left; border-bottom-style: none"><span id="lnum8" style="color: #606060">   8:</span>     }</pre>
<p><!--CRLF--></p>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; text-align: left; border-bottom-style: none"><span id="lnum9" style="color: #606060">   9:</span>&#160; </pre>
<p><!--CRLF--></p>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; text-align: left; border-bottom-style: none"><span id="lnum10" style="color: #606060">  10:</span>     <span style="color: #0000ff">void</span> Sort( <span style="color: #0000ff">bool</span> (*sortOrder(<span style="color: #0000ff">int</span> , <span style="color: #0000ff">int</span>)) )</pre>
<p><!--CRLF--></p>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; text-align: left; border-bottom-style: none"><span id="lnum11" style="color: #606060">  11:</span>     {</pre>
<p><!--CRLF--></p>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; text-align: left; border-bottom-style: none"><span id="lnum12" style="color: #606060">  12:</span>         <span style="color: #008000">// sort algorithm that uses the sortOrder</span></pre>
<p><!--CRLF--></p>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; text-align: left; border-bottom-style: none"><span id="lnum13" style="color: #606060">  13:</span>         <span style="color: #008000">// as (*sortOrder)(firstNum, secondNum);</span></pre>
<p><!--CRLF--></p>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; text-align: left; border-bottom-style: none"><span id="lnum14" style="color: #606060">  14:</span>     }    </pre>
<p><!--CRLF--></p>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; text-align: left; border-bottom-style: none"><span id="lnum15" style="color: #606060">  15:</span> };</pre>
<p><!--CRLF--></div>
</div>
<p>In the above code we have a class that sort an array of integers. In all comparison sorts, we need to be able to make decision whether two number are out of place or not. However, this decision can be made in a variety of ways (Can you figure out why a variety of ways and not just two?). The user can then supply a function of their own choosing how different numbers are treated in comparison. </p>
<p>The comments inside the sort function also show how to make the call back. This whole exercise when a function pointer is passed to another function and the pointer is used to call that function is called a callback.</p>
<p class="facebook"><a href="http://www.facebook.com/share.php?u=http://tayyab.xenoglaux-solutions.com/2009/05/24/callbacks-c/" target="_blank"><img src="http://tayyab.xenoglaux-solutions.com/wp-content/plugins/add-to-facebook-plugin/facebook_share_icon.gif" alt="Share on Facebook" title="Share on Facebook" /></a><a href="http://www.facebook.com/share.php?u=http://tayyab.xenoglaux-solutions.com/2009/05/24/callbacks-c/" target="_blank" title="Share on Facebook">Share on Facebook</a></p>]]></content:encoded>
			<wfw:commentRss>http://tayyab.xenoglaux-solutions.com/2009/05/24/callbacks-c/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Linked List: Tutorial I</title>
		<link>http://tayyab.xenoglaux-solutions.com/2009/05/01/linked-list-tutorial-i/</link>
		<comments>http://tayyab.xenoglaux-solutions.com/2009/05/01/linked-list-tutorial-i/#comments</comments>
		<pubDate>Fri, 01 May 2009 18:23:00 +0000</pubDate>
		<dc:creator>tayyabtariq</dc:creator>
				<category><![CDATA[C++]]></category>
		<category><![CDATA[Data Structure]]></category>
		<category><![CDATA[Link List]]></category>
		<category><![CDATA[Linked List]]></category>

		<guid isPermaLink="false">http://tayyab.xenoglaux-solutions.com/2009/05/01/linked-list-tutorial-i/</guid>
		<description><![CDATA[Although linked lists sounds kind of scary, don&#8217;t worry they are really easy to use once you&#8217;ve got a little practice under your belt! Linked lists form the foundation of many data storing schemes in my game! 
They are really nice when you don&#8217;t know how many of a data type you will need, and [...]]]></description>
			<content:encoded><![CDATA[<p><img style="display: inline; margin-left: 0px; margin-right: 0px" src="http://www.inversereality.org/tutorials/c++/linkedlistspic.gif" align="left" />Although linked lists sounds kind of scary, don&#8217;t worry they are really easy to use once you&#8217;ve got a little practice under your belt! Linked lists form the foundation of many data storing schemes in my game! </p>
<p>They are really nice when you don&#8217;t know how many of a data type you will need, and don&#8217;t want to waste space. They are like having a dynamically allocated string that fluctuates in size as the program runs. Before I really confuse you lets get into a better explanation!</p>
<p> <span id="more-60"></span>
<p>A <i>linked list</i> is a chain of structs or records called nodes. Each node has at least two members, one of which points to the next item or node in the list! These are defined as <i>Single Linked Lists </i>because they only point to the next item, and not the previous. Those that do point to both are called <i>Doubly Linked Lists </i>or <i>Circular Linked Lists</i>. Please note that there is a distinct difference betweeen Double Linked lists and Circular Linked lists. I won&#8217;t go into any depth on it because it doesn&#8217;t concern this tutorial. According to this definition, we could have our record hold <b>anything</b> we wanted! The only drawback is that each record must be an instance of the same structure. This means that we couldn&#8217;t have a record with a char pointing to another structure holding a short, a char array, and a long. Again, they have to be instances of the same structure for this to work. Another cool aspect is that each structure can be located anywhere in memory, each node doesn&#8217;t have to be linear in memory!</p>
<div id="codeSnippetWrapper" style="border-right: silver 1px solid; padding-right: 4px; border-top: silver 1px solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin: 20px 0px 10px; overflow: auto; border-left: silver 1px solid; width: 97.5%; cursor: text; direction: ltr; max-height: 200px; line-height: 12pt; padding-top: 4px; border-bottom: silver 1px solid; font-family: &#39;Courier New&#39;, courier, monospace; background-color: #f4f4f4; text-align: left">
<div id="codeSnippet" style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; text-align: left; border-bottom-style: none">
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; text-align: left; border-bottom-style: none"><span id="lnum1" style="color: #606060">   1:</span> <span style="color: #0000ff">class</span> List</pre>
<p><!--CRLF--></p>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; text-align: left; border-bottom-style: none"><span id="lnum2" style="color: #606060">   2:</span> {</pre>
<p><!--CRLF--></p>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; text-align: left; border-bottom-style: none"><span id="lnum3" style="color: #606060">   3:</span>     <span style="color: #0000ff">long</span> Data;</pre>
<p><!--CRLF--></p>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; text-align: left; border-bottom-style: none"><span id="lnum4" style="color: #606060">   4:</span>     List* Next;</pre>
<p><!--CRLF--></p>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; text-align: left; border-bottom-style: none"><span id="lnum5" style="color: #606060">   5:</span>     List(<span style="color: #0000ff">long</span> d)</pre>
<p><!--CRLF--></p>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; text-align: left; border-bottom-style: none"><span id="lnum6" style="color: #606060">   6:</span>     {</pre>
<p><!--CRLF--></p>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; text-align: left; border-bottom-style: none"><span id="lnum7" style="color: #606060">   7:</span>         Data = d;</pre>
<p><!--CRLF--></p>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; text-align: left; border-bottom-style: none"><span id="lnum8" style="color: #606060">   8:</span>         Next = NULL;</pre>
<p><!--CRLF--></p>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; text-align: left; border-bottom-style: none"><span id="lnum9" style="color: #606060">   9:</span>     }</pre>
<p><!--CRLF--></p>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; text-align: left; border-bottom-style: none"><span id="lnum10" style="color: #606060">  10:</span> };</pre>
<p><!--CRLF--></div>
</div>
<p>Notice that we define a constructor for our class that sets Next equal to NULL. This is because we need to know when we have reached the end of our linked list. Each Next item that is <b>NOT</b> equal to NULL means that it is pointing to another allocated instance. If it <b>does</b> equal NULL, then we have reached the end of our list.</p>
<h2>Starting up</h2>
<p>First off, we need to set our Link pointers to some know location in memory. We will create a temp pointer, allocate an instance, then assign our pointers. Something like this: </p>
<div id="codeSnippetWrapper" style="border-right: silver 1px solid; padding-right: 4px; border-top: silver 1px solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin: 20px 0px 10px; overflow: auto; border-left: silver 1px solid; width: 97.5%; cursor: text; direction: ltr; max-height: 200px; line-height: 12pt; padding-top: 4px; border-bottom: silver 1px solid; font-family: &#39;Courier New&#39;, courier, monospace; background-color: #f4f4f4; text-align: left">
<div id="codeSnippet" style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; text-align: left; border-bottom-style: none">
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; text-align: left; border-bottom-style: none"><span id="lnum1" style="color: #606060">   1:</span> SLList:: SLList()</pre>
<p><!--CRLF--></p>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; text-align: left; border-bottom-style: none"><span id="lnum2" style="color: #606060">   2:</span> {</pre>
<p><!--CRLF--></p>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; text-align: left; border-bottom-style: none"><span id="lnum3" style="color: #606060">   3:</span>     Head = NULL;</pre>
<p><!--CRLF--></p>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; text-align: left; border-bottom-style: none"><span id="lnum4" style="color: #606060">   4:</span>     Tail=Head;</pre>
<p><!--CRLF--></p>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; text-align: left; border-bottom-style: none"><span id="lnum5" style="color: #606060">   5:</span>     CurrentPtr = Head;</pre>
<p><!--CRLF--></p>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; text-align: left; border-bottom-style: none"><span id="lnum6" style="color: #606060">   6:</span> }</pre>
<p><!--CRLF--></div>
</div>
<p>We have allocated no memory for out link list nodes. So no nodes exist at the time, some call this the smallest possible like list, others require at least one node. I support the first point of view.</p>
<h2>Adding a Node</h2>
<p>We can actually add nodes in two possible places, the beginning or the end, although the standard seems to be the end. This makes our linked list act kind of like a queue with the head node being the oldest and end pointing to the newest objects. <img src="http://www.inversereality.org/tutorials/c++/adding.gif" align="right" /> This brings up an interesting subject also. How will we use our linked list? This is what makes the linked list so powerful. We could use it as a priority list where the oldest objects get a higher precedence until deleted from the list. We could also use it as a master listing of items that need to be kept track of at one time, deleting object when they need to be, without using any precedence scheme. Here&#8217;s some code that will add a node onto the end of the list, and then move the end pointer so that it really does point at the end.</p>
<p>Following is the code to add a node at the start of the link list. If the tail is NULL then we have no nodes and the new node is the tail node.</p>
<div id="codeSnippetWrapper" style="border-right: silver 1px solid; padding-right: 4px; border-top: silver 1px solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin: 20px 0px 10px; overflow: auto; border-left: silver 1px solid; width: 97.5%; cursor: text; direction: ltr; max-height: 200px; line-height: 12pt; padding-top: 4px; border-bottom: silver 1px solid; font-family: &#39;Courier New&#39;, courier, monospace; background-color: #f4f4f4; text-align: left">
<div id="codeSnippet" style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; text-align: left; border-bottom-style: none">
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; text-align: left; border-bottom-style: none"><span id="lnum1" style="color: #606060">   1:</span> <span style="color: #0000ff">void</span> AddAtStart(<span style="color: #0000ff">int</span> d)</pre>
<p><!--CRLF--></p>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; text-align: left; border-bottom-style: none"><span id="lnum2" style="color: #606060">   2:</span> {</pre>
<p><!--CRLF--></p>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; text-align: left; border-bottom-style: none"><span id="lnum3" style="color: #606060">   3:</span>     Node * newNode = <span style="color: #0000ff">new</span> Node(d);</pre>
<p><!--CRLF--></p>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; text-align: left; border-bottom-style: none"><span id="lnum4" style="color: #606060">   4:</span>     <span style="color: #0000ff">if</span> (tal == NULL)</pre>
<p><!--CRLF--></p>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; text-align: left; border-bottom-style: none"><span id="lnum5" style="color: #606060">   5:</span>     {</pre>
<p><!--CRLF--></p>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; text-align: left; border-bottom-style: none"><span id="lnum6" style="color: #606060">   6:</span>         tail = newNode;</pre>
<p><!--CRLF--></p>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; text-align: left; border-bottom-style: none"><span id="lnum7" style="color: #606060">   7:</span>     }</pre>
<p><!--CRLF--></p>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; text-align: left; border-bottom-style: none"><span id="lnum8" style="color: #606060">   8:</span> }</pre>
</p></div>
</div>
<p>Here we try and add a node at the end of the link list. If there are no nodes in the link list, it is the same as adding a node at the start of the link list so we use that function.</p>
<div id="codeSnippetWrapper" style="border-right: silver 1px solid; padding-right: 4px; border-top: silver 1px solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin: 20px 0px 10px; overflow: auto; border-left: silver 1px solid; width: 97.5%; cursor: text; direction: ltr; max-height: 200px; line-height: 12pt; padding-top: 4px; border-bottom: silver 1px solid; font-family: &#39;Courier New&#39;, courier, monospace; background-color: #f4f4f4; text-align: left">
<div id="codeSnippet" style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; text-align: left; border-bottom-style: none">
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; text-align: left; border-bottom-style: none"><span id="lnum1" style="color: #606060">   1:</span> <span style="color: #0000ff">void</span> AddAtEnd(<span style="color: #0000ff">int</span> d)</pre>
<p><!--CRLF--></p>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; text-align: left; border-bottom-style: none"><span id="lnum2" style="color: #606060">   2:</span> {</pre>
<p><!--CRLF--></p>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; text-align: left; border-bottom-style: none"><span id="lnum3" style="color: #606060">   3:</span>     <span style="color: #0000ff">if</span> (tail == NULL)</pre>
<p><!--CRLF--></p>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; text-align: left; border-bottom-style: none"><span id="lnum4" style="color: #606060">   4:</span>     {</pre>
<p><!--CRLF--></p>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; text-align: left; border-bottom-style: none"><span id="lnum5" style="color: #606060">   5:</span>         AddAtStart(d);</pre>
<p><!--CRLF--></p>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; text-align: left; border-bottom-style: none"><span id="lnum6" style="color: #606060">   6:</span>     }</pre>
<p><!--CRLF--></p>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; text-align: left; border-bottom-style: none"><span id="lnum7" style="color: #606060">   7:</span>     <span style="color: #0000ff">else</span></pre>
<p><!--CRLF--></p>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; text-align: left; border-bottom-style: none"><span id="lnum8" style="color: #606060">   8:</span>     {</pre>
<p><!--CRLF--></p>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; text-align: left; border-bottom-style: none"><span id="lnum9" style="color: #606060">   9:</span>         Node * newNode = <span style="color: #0000ff">new</span> Node(d);</pre>
<p><!--CRLF--></p>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; text-align: left; border-bottom-style: none"><span id="lnum10" style="color: #606060">  10:</span>         tail-&gt;next = newNode;</pre>
<p><!--CRLF--></p>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; text-align: left; border-bottom-style: none"><span id="lnum11" style="color: #606060">  11:</span>         tail = newNode;</pre>
<p><!--CRLF--></p>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; text-align: left; border-bottom-style: none"><span id="lnum12" style="color: #606060">  12:</span>     }</pre>
<p><!--CRLF--></p>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; text-align: left; border-bottom-style: none"><span id="lnum13" style="color: #606060">  13:</span> }</pre>
<p><!--CRLF--></div>
</div>
<p>We make another function, add after. This function adds a node after the given node.</p>
<div id="codeSnippetWrapper" style="border-right: silver 1px solid; padding-right: 4px; border-top: silver 1px solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin: 20px 0px 10px; overflow: auto; border-left: silver 1px solid; width: 97.5%; cursor: text; direction: ltr; max-height: 200px; line-height: 12pt; padding-top: 4px; border-bottom: silver 1px solid; font-family: &#39;Courier New&#39;, courier, monospace; background-color: #f4f4f4; text-align: left">
<div id="codeSnippet" style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; text-align: left; border-bottom-style: none">
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; text-align: left; border-bottom-style: none"><span id="lnum1" style="color: #606060">   1:</span> <span style="color: #0000ff">void</span> AddAfter(Node * p, <span style="color: #0000ff">int</span> d)</pre>
<p><!--CRLF--></p>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; text-align: left; border-bottom-style: none"><span id="lnum2" style="color: #606060">   2:</span> {</pre>
<p><!--CRLF--></p>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; text-align: left; border-bottom-style: none"><span id="lnum3" style="color: #606060">   3:</span>     Node * newNode = <span style="color: #0000ff">new</span> Node(d);</pre>
<p><!--CRLF--></p>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; text-align: left; border-bottom-style: none"><span id="lnum4" style="color: #606060">   4:</span>     newNode-&gt;next = p-&gt;next;</pre>
<p><!--CRLF--></p>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; text-align: left; border-bottom-style: none"><span id="lnum5" style="color: #606060">   5:</span>     p-&gt;next = newNode;</pre>
<p><!--CRLF--></p>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; text-align: left; border-bottom-style: none"><span id="lnum6" style="color: #606060">   6:</span>     <span style="color: #0000ff">if</span> (p == tail)</pre>
<p><!--CRLF--></p>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; text-align: left; border-bottom-style: none"><span id="lnum7" style="color: #606060">   7:</span>         tail = newNode;    </pre>
<p><!--CRLF--></p>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; text-align: left; border-bottom-style: none"><span id="lnum8" style="color: #606060">   8:</span> }</pre>
<p><!--CRLF--></div>
</div>
<p>We will use this function in our sorted addition function. To implement a sorted insertion function, we need to know the node after which the node is to be added. We have four cases:</p>
<ol>
<li>The link list is empty </li>
<li>The given data is less than the head data (to be inserted at the start) </li>
<li>The given data is greater than the tail node (to be inserted at the end of the node) </li>
<li>When the data is to be inserted somewhere in the middle </li>
</ol>
<div id="codeSnippetWrapper" style="border-right: silver 1px solid; padding-right: 4px; border-top: silver 1px solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin: 20px 0px 10px; overflow: auto; border-left: silver 1px solid; width: 97.5%; cursor: text; direction: ltr; max-height: 200px; line-height: 12pt; padding-top: 4px; border-bottom: silver 1px solid; font-family: &#39;Courier New&#39;, courier, monospace; background-color: #f4f4f4; text-align: left">
<div id="codeSnippet" style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; text-align: left; border-bottom-style: none">
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; text-align: left; border-bottom-style: none"><span id="lnum1" style="color: #606060">   1:</span> <span style="color: #0000ff">void</span> AddSorted(<span style="color: #0000ff">int</span> d)</pre>
<p><!--CRLF--></p>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; text-align: left; border-bottom-style: none"><span id="lnum2" style="color: #606060">   2:</span> {</pre>
<p><!--CRLF--></p>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; text-align: left; border-bottom-style: none"><span id="lnum3" style="color: #606060">   3:</span>     <span style="color: #0000ff">if</span> (head == NULL)</pre>
<p><!--CRLF--></p>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; text-align: left; border-bottom-style: none"><span id="lnum4" style="color: #606060">   4:</span>     {</pre>
<p><!--CRLF--></p>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; text-align: left; border-bottom-style: none"><span id="lnum5" style="color: #606060">   5:</span>         AddAtStart(d);</pre>
<p><!--CRLF--></p>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; text-align: left; border-bottom-style: none"><span id="lnum6" style="color: #606060">   6:</span>     }</pre>
<p><!--CRLF--></p>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; text-align: left; border-bottom-style: none"><span id="lnum7" style="color: #606060">   7:</span>     <span style="color: #0000ff">else</span> <span style="color: #0000ff">if</span> (d &lt; head-&gt;data)</pre>
<p><!--CRLF--></p>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; text-align: left; border-bottom-style: none"><span id="lnum8" style="color: #606060">   8:</span>     {</pre>
<p><!--CRLF--></p>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; text-align: left; border-bottom-style: none"><span id="lnum9" style="color: #606060">   9:</span>         AddAtStart(d);</pre>
<p><!--CRLF--></p>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; text-align: left; border-bottom-style: none"><span id="lnum10" style="color: #606060">  10:</span>     }</pre>
<p><!--CRLF--></p>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; text-align: left; border-bottom-style: none"><span id="lnum11" style="color: #606060">  11:</span>     <span style="color: #0000ff">else</span> <span style="color: #0000ff">if</span> (d &gt; tail-&gt;data)</pre>
<p><!--CRLF--></p>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; text-align: left; border-bottom-style: none"><span id="lnum12" style="color: #606060">  12:</span>     {</pre>
<p><!--CRLF--></p>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; text-align: left; border-bottom-style: none"><span id="lnum13" style="color: #606060">  13:</span>         AddAtEnd(d);</pre>
<p><!--CRLF--></p>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; text-align: left; border-bottom-style: none"><span id="lnum14" style="color: #606060">  14:</span>     }</pre>
<p><!--CRLF--></p>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; text-align: left; border-bottom-style: none"><span id="lnum15" style="color: #606060">  15:</span>     <span style="color: #0000ff">else</span></pre>
<p><!--CRLF--></p>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; text-align: left; border-bottom-style: none"><span id="lnum16" style="color: #606060">  16:</span>     {</pre>
<p><!--CRLF--></p>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; text-align: left; border-bottom-style: none"><span id="lnum17" style="color: #606060">  17:</span>         <span style="color: #0000ff">for</span> (Node * temp = head; temp-&gt;next != NULL; temp = temp-&gt;next)</pre>
<p><!--CRLF--></p>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; text-align: left; border-bottom-style: none"><span id="lnum18" style="color: #606060">  18:</span>         {</pre>
<p><!--CRLF--></p>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; text-align: left; border-bottom-style: none"><span id="lnum19" style="color: #606060">  19:</span>             <span style="color: #0000ff">if</span> (temp-&gt;next-&gt;data &gt; d)</pre>
<p><!--CRLF--></p>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; text-align: left; border-bottom-style: none"><span id="lnum20" style="color: #606060">  20:</span>             {</pre>
<p><!--CRLF--></p>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; text-align: left; border-bottom-style: none"><span id="lnum21" style="color: #606060">  21:</span>                 AddAfter(temp, d);</pre>
<p><!--CRLF--></p>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; text-align: left; border-bottom-style: none"><span id="lnum22" style="color: #606060">  22:</span>                 <span style="color: #0000ff">break</span>;</pre>
<p><!--CRLF--></p>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; text-align: left; border-bottom-style: none"><span id="lnum23" style="color: #606060">  23:</span>             }</pre>
<p><!--CRLF--></p>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; text-align: left; border-bottom-style: none"><span id="lnum24" style="color: #606060">  24:</span>         }</pre>
<p><!--CRLF--></p>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; text-align: left; border-bottom-style: none"><span id="lnum25" style="color: #606060">  25:</span>     }</pre>
<p><!--CRLF--></p>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; text-align: left; border-bottom-style: none"><span id="lnum26" style="color: #606060">  26:</span> }</pre>
<p><!--CRLF--></div>
</div>
<p class="facebook"><a href="http://www.facebook.com/share.php?u=http://tayyab.xenoglaux-solutions.com/2009/05/01/linked-list-tutorial-i/" target="_blank"><img src="http://tayyab.xenoglaux-solutions.com/wp-content/plugins/add-to-facebook-plugin/facebook_share_icon.gif" alt="Share on Facebook" title="Share on Facebook" /></a><a href="http://www.facebook.com/share.php?u=http://tayyab.xenoglaux-solutions.com/2009/05/01/linked-list-tutorial-i/" target="_blank" title="Share on Facebook">Share on Facebook</a></p>]]></content:encoded>
			<wfw:commentRss>http://tayyab.xenoglaux-solutions.com/2009/05/01/linked-list-tutorial-i/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
