<?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>zacharyc &#187; Programming</title>
	<atom:link href="http://zacharyc.com/tag/programming/feed/" rel="self" type="application/rss+xml" />
	<link>http://zacharyc.com</link>
	<description></description>
	<lastBuildDate>Wed, 01 Feb 2012 16:50:49 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	
		<item>
		<title>Nuances in C: Struct Assignment in C</title>
		<link>http://zacharyc.com/2008/03/13/nuances-in-c-struct-assignment-in-c/</link>
		<comments>http://zacharyc.com/2008/03/13/nuances-in-c-struct-assignment-in-c/#comments</comments>
		<pubDate>Thu, 13 Mar 2008 16:09:00 +0000</pubDate>
		<dc:creator>zacharyc</dc:creator>
				<category><![CDATA[C/C++]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Structs]]></category>

		<guid isPermaLink="false">http://zacharyc.com/2008/03/13/nuances-in-c-struct-assignment-in-c/</guid>
		<description><![CDATA[When I started this post, it was going to be a revolutionary post, talking about something that was really bothering me. As I have spent more time thinking about this, the answer seems so simple and obvious, still there was a time where I did not get this concept, so I here is a brief [...]]]></description>
			<content:encoded><![CDATA[<p>When I started this post, it was going to be a revolutionary post, talking about something that was really bothering me. As I have spent more time thinking about this, the answer seems so simple and obvious, still there was a time where I did not get this concept, so I here is  a brief post on the topic,</p>
<p>The question is, in straight C, will this work:<br />
<code class="prettyprint C"></code></p>
<pre>
#include &lt;stdio.h&gt;
#include &lt;stdlib.h&gt;
#include &lt;string.h&gt;

typedef struct point_t {
  int x,y;
} Point;

int main(int argc, char **argv)
{
  Point a,b;
  int c[4] = {1, 2, 3, 4};

  b = c;

  printf("b(%p) is now b[x] = %d, b[y] = %d\n", &amp;b, b.x, b.y);

  return 0;
}</pre>
<p>In C++ this works no problem. Structs are teated like classes where all members have public scope. Assigning one struct to another simple implements the copy constructor that is created by the C++ language, but  what about straight C?</p>
<p>Well, like I said the answer is the obvious one, IT WORKS! The reason this originally confused me was because you <strong>CAN&#8217;T</strong> do this:</p>
<pre>
   int a[4] = { 1, 2, 3, 4};
   int b[4];

   b = a;
</pre>
<p>Why not? The compiler knows how big each of these integer arrays are. If you run <code class="prettyprint">sizeof(a);</code> and it will give you the same thing as <code class="prettyprint">sizeof(b);</code>. Copying an array is as simple as copying over the bytes (bit for bit), using <code class="prettyprint">memset()</code> or something similar.</p>
<p>The reason structs work and arrays don&#8217;t is simple. Basically we are doing a type check on the object. If the object is a struct of defined type, we know how to do an assignment because we have the size. If the item is an array of integers, we would have to do a size comparison and the compiler doesn&#8217;t do that.</p>
<p>Okay, so I hope that explains the obvious, good luck.</p>
]]></content:encoded>
			<wfw:commentRss>http://zacharyc.com/2008/03/13/nuances-in-c-struct-assignment-in-c/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

