<?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>Coding Friends &#187; struct</title>
	<atom:link href="http://www.codingfriends.com/index.php/tag/struct/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.codingfriends.com</link>
	<description>Coding Friends, place for developers.</description>
	<lastBuildDate>Sun, 04 Dec 2011 21:11:08 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>struct &#8211; setup and memory locations</title>
		<link>http://www.codingfriends.com/index.php/2010/07/23/struct-setup-and-memory-locations/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=struct-setup-and-memory-locations</link>
		<comments>http://www.codingfriends.com/index.php/2010/07/23/struct-setup-and-memory-locations/#comments</comments>
		<pubDate>Fri, 23 Jul 2010 09:47:17 +0000</pubDate>
		<dc:creator>genux</dc:creator>
				<category><![CDATA[C / C++]]></category>
		<category><![CDATA[memcpy]]></category>
		<category><![CDATA[struct]]></category>

		<guid isPermaLink="false">http://www.codingfriends.com/?p=1109</guid>
		<description><![CDATA[When you setup a struct within c++, it is kinder like having a array of data and if you want to you can access the internal parts by using some memory pointer location fun!. Lets say that you have a struct of struct intvalue &#123; int a; int b; &#125;; Just to say that since [...]]]></description>
			<content:encoded><![CDATA[<p>When you setup a struct within c++, it is kinder like having a array of data and if you want to you can access the internal parts by using some memory pointer location fun!.</p>
<p>Lets say that you have a struct of</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">struct</span> intvalue <span style="color: #008000;">&#123;</span>
    <span style="color: #0000ff;">int</span> a<span style="color: #008080;">;</span>
    <span style="color: #0000ff;">int</span> b<span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span></pre></div></div>

<p>Just to say that since I am using int(eger) values so that is what I am incrementing by in the pointer arithmetic which is why I am casting the pointer to a integer value.</p>
<p>So lets say that we create a variable of intvalue and setup the values as below</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;">intvalue testvalue<span style="color: #008080;">;</span>
testvalue.<span style="color: #007788;">a</span> <span style="color: #000080;">=</span> <span style="color: #0000dd;">4</span><span style="color: #008080;">;</span>
testvalue.<span style="color: #007788;">b</span> <span style="color: #000080;">=</span> <span style="color: #0000dd;">5</span><span style="color: #008080;">;</span></pre></div></div>

<p>We can then pull out the value of a or b, but using memcpy and just outputting to a int(eger) variable as below, the key is the ((int*)&#038;testvalue)+1, this will first convert the testvalue variable to a pointer to memory location and then (int*) casts that pointer to a int pointer, because internally that is what it is, and then just add 1 to it, which points to the second value ( in this case the value of b which is 5)</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;">    <span style="color: #0000ff;">int</span> avalue<span style="color: #008080;">;</span>
    <span style="color: #666666;">// convert to a int pointer type and then add one to it (to the next array element as such).</span>
    <span style="color: #0000dd;">memcpy</span><span style="color: #008000;">&#40;</span><span style="color: #000040;">&amp;</span>avalue, <span style="color: #008000;">&#40;</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span><span style="color: #000040;">*</span><span style="color: #008000;">&#41;</span><span style="color: #000040;">&amp;</span>testvalue<span style="color: #008000;">&#41;</span><span style="color: #000040;">+</span><span style="color: #0000dd;">1</span>,<span style="color: #0000dd;">sizeof</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    <span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;a value (or is it the b value :) ) &quot;</span> <span style="color: #000080;">&lt;&lt;</span> avalue <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span></pre></div></div>

<p>The output would be</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">a value <span style="color: #7a0874; font-weight: bold;">&#40;</span>or is it the b value :<span style="color: #7a0874; font-weight: bold;">&#41;</span> <span style="color: #7a0874; font-weight: bold;">&#41;</span> <span style="color: #000000;">5</span></pre></div></div>

<p>because I am pointing to the second value which is b and thus 5 <img src='http://www.codingfriends.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> .</p>
<p>Of course if you just wanted the value of first int (the value of a in this case) you do not add the 1 to the memory location, for example</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;">    <span style="color: #0000dd;">memcpy</span><span style="color: #008000;">&#40;</span><span style="color: #000040;">&amp;</span>avalue, <span style="color: #008000;">&#40;</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span><span style="color: #000040;">*</span><span style="color: #008000;">&#41;</span><span style="color: #000040;">&amp;</span>testvalue<span style="color: #008000;">&#41;</span>,<span style="color: #0000dd;">sizeof</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span></pre></div></div>

<p>this time I am just converting the testvalue (casting) to a int pointer and thus pointing to the start of the struct and that is where the int a variable is living <img src='http://www.codingfriends.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> .</p>
]]></content:encoded>
			<wfw:commentRss>http://www.codingfriends.com/index.php/2010/07/23/struct-setup-and-memory-locations/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>struct &#8211; initialization values</title>
		<link>http://www.codingfriends.com/index.php/2010/03/05/struct-initialization-values/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=struct-initialization-values</link>
		<comments>http://www.codingfriends.com/index.php/2010/03/05/struct-initialization-values/#comments</comments>
		<pubDate>Fri, 05 Mar 2010 09:58:15 +0000</pubDate>
		<dc:creator>genux</dc:creator>
				<category><![CDATA[C / C++]]></category>
		<category><![CDATA[initialization]]></category>
		<category><![CDATA[struct]]></category>

		<guid isPermaLink="false">http://www.codingfriends.com/?p=780</guid>
		<description><![CDATA[When you define a new struct(ure) and initialize it you can use the {} to define the internals of the values within for example typedef struct &#123; int coding; int friends; &#125; newType; &#160; newType nT = &#123; 10, 20&#125;; so coding = 10 and friends = 20 But if you try and over initialize [...]]]></description>
			<content:encoded><![CDATA[<p>When you define a new <a href="http://en.wikipedia.org/wiki/Struct_%28C_programming_language%29">struct</a>(ure) and initialize it you can use the {} to define the internals of the values within for example</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">typedef</span> <span style="color: #0000ff;">struct</span>
<span style="color: #008000;">&#123;</span>
    <span style="color: #0000ff;">int</span> coding<span style="color: #008080;">;</span>
    <span style="color: #0000ff;">int</span> friends<span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span> newType<span style="color: #008080;">;</span>
&nbsp;
newType nT <span style="color: #000080;">=</span> <span style="color: #008000;">&#123;</span> <span style="color: #0000dd;">10</span>, <span style="color: #0000dd;">20</span><span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span></pre></div></div>

<p>so coding = 10 and friends = 20</p>
<p>But if you try and over initialize the values that are not present, e.g.</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;">newType nT <span style="color: #000080;">=</span> <span style="color: #008000;">&#123;</span> <span style="color: #0000dd;">10</span>, <span style="color: #0000dd;">20</span>, <span style="color: #0000dd;">30</span> <span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span> <span style="color: #666666;">// there is not place to put the 30</span></pre></div></div>

<p>then the compiler will compile with</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">error: too many initializers <span style="color: #000000; font-weight: bold;">for</span> ‘newType<span style="color: #ff0000;">'</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.codingfriends.com/index.php/2010/03/05/struct-initialization-values/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>struct &#8211; what are they for</title>
		<link>http://www.codingfriends.com/index.php/2010/02/17/struct-what-are-they-for/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=struct-what-are-they-for</link>
		<comments>http://www.codingfriends.com/index.php/2010/02/17/struct-what-are-they-for/#comments</comments>
		<pubDate>Wed, 17 Feb 2010 11:49:04 +0000</pubDate>
		<dc:creator>genux</dc:creator>
				<category><![CDATA[C / C++]]></category>
		<category><![CDATA[struct]]></category>

		<guid isPermaLink="false">http://www.codingfriends.com/?p=743</guid>
		<description><![CDATA[A struct (structure) is a defined type that the user can define at compile time, for example if you want to have a user defined type that holds details of a database connection you could hold the hostname, username, password of the database connection within a structure (struct) without having to define 3 different variables [...]]]></description>
			<content:encoded><![CDATA[<p>A struct (structure) is a defined type that the user can define at compile time, for example if you want to have a user defined type that holds details of a database connection you could hold the hostname, username, password of the database connection within a structure (struct) without having to define 3 different variables you only have one that you need to fill in.</p>
<p>For the above example of a database struct</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">struct</span> database_connection
<span style="color: #008000;">&#123;</span>
   <span style="color: #0000ff;">char</span> <span style="color: #000040;">*</span>username<span style="color: #008080;">;</span>
   <span style="color: #0000ff;">char</span> <span style="color: #000040;">*</span>hostname<span style="color: #008080;">;</span>
   <span style="color: #0000ff;">char</span> <span style="color: #000040;">*</span>password<span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span></pre></div></div>

<p>then within the code you could create a new structure of this type and also fill in the details as</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;">database_connection mydatabase<span style="color: #008080;">;</span>
mydatabase.<span style="color: #007788;">username</span> <span style="color: #000080;">=</span> <span style="color: #FF0000;">&quot;usernamebob&quot;</span><span style="color: #008080;">;</span>
mydatabase.<span style="color: #007788;">hostname</span> <span style="color: #000080;">=</span> <span style="color: #FF0000;">&quot;localhost&quot;</span><span style="color: #008080;">;</span>
mydatabase.<span style="color: #007788;">password</span> <span style="color: #000080;">=</span> <span style="color: #FF0000;">&quot;userpassword&quot;</span><span style="color: #008080;">;</span></pre></div></div>

<p>and you just need to pass that struct to a function if you wanted to to do the connections e.g.</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">bool</span> dummyconnection<span style="color: #008000;">&#40;</span>database_connection conn<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
   createdatabase<span style="color: #008000;">&#40;</span> conn.<span style="color: #007788;">hostname</span>, conn.<span style="color: #007788;">username</span>, conn.<span style="color: #007788;">password</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>saves passing in three different parameters.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.codingfriends.com/index.php/2010/02/17/struct-what-are-they-for/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

