tags/struct
David Bremner
by-nc-sa-2.5
Copyright 2020, David Bremner
https://www.cs.unb.ca/~bremner//tags/struct/
David Bremner
ikiwiki
2010-09-11T20:23:13Z
Tutorial Exercise 7
https://www.cs.unb.ca/~bremner//teaching/old/cs2023/events/tut07/
<a href="../../whyCC/">by-nc-sa-2.5</a>
Copyright 2020, David Bremner
2010-09-11T20:23:13Z
2009-11-30T16:19:00Z
<h3 id="Preprocessor">Preprocessor</h3>
<ol>
<li><p>Given the following definition</p>
<p> #define DOUBLE(X) 2*X</p>
<ul>
<li>What is the value of DOUBLE(1+2)</li>
<li>What is the value of 4/DOUBLE(2)</li>
<li>Fix the definition of DOUBLE, and test it.</li>
</ul>
</li>
<li><p>Write a macro DISP(f,x) so that</p>
<p> DISP(sqrt,3.0)</p></li>
</ol>
<p>expands into</p>
<pre><code> printf("sqrt(%g) = %g\n", 3.0, sqrt(3.0));
</code></pre>
<p>Test your macro.</p>
<h3 id="Structures_and_Unions">Structures and Unions</h3>
<p>Given the following definitions</p>
<div class="highlight-c"><pre class="hl"><span class="hl kwb">struct</span> point <span class="hl opt">{</span> <span class="hl kwb">int</span> x<span class="hl opt">,</span> y <span class="hl opt">};</span>
<span class="hl kwb">struct</span> shape <span class="hl opt">{</span>
<span class="hl kwb">enum</span> <span class="hl opt">{</span> RECTANGLE<span class="hl opt">,</span> CIRCLE <span class="hl opt">}</span> shape_kind<span class="hl opt">;</span>
<span class="hl kwb">struct</span> point center<span class="hl opt">;</span>
<span class="hl kwb">union</span> <span class="hl opt">{</span>
<span class="hl kwb">struct</span> <span class="hl opt">{</span>
<span class="hl kwb">int</span> height<span class="hl opt">,</span> width<span class="hl opt">;</span>
<span class="hl opt">}</span> rectangle<span class="hl opt">;</span>
<span class="hl kwb">struct</span> <span class="hl opt">{</span>
<span class="hl kwb">int</span> radius<span class="hl opt">;</span>
<span class="hl opt">}</span> radius<span class="hl opt">;</span>
<span class="hl opt">}</span> u<span class="hl opt">;</span>
<span class="hl opt">}</span>
</pre></div>
<p>Correct any of the following statements that are illegal:</p>
<ol>
<li><p>s.shape_kind = RECTANGLE;</p></li>
<li><p>s.center.x = 10 ;</p></li>
<li><p>s.height = 25;</p></li>
<li><p>s.u.rectangle.width = 8;</p></li>
<li><p>s.u.circle=5;</p></li>
<li><p>s.u.radius=6;</p></li>
</ol>
<h3 id="Function_pointers">Function pointers</h3>
<p>Write the function <code>sum</code> with prototype</p>
<div class="highlight-c"><pre class="hl"><span class="hl kwb">int</span> <span class="hl kwd">sum</span><span class="hl opt">(</span><span class="hl kwb">int</span> <span class="hl opt">(*</span>f<span class="hl opt">)(</span><span class="hl kwb">int</span><span class="hl opt">),</span> <span class="hl kwb">int</span> start<span class="hl opt">,</span> <span class="hl kwb">int</span> end<span class="hl opt">)</span>
</pre></div>
<p>The call <code>sum(g,i,j)</code> should return <code>g(i)+g(i+1)+...+g(j)</code></p>
Lecture 34
https://www.cs.unb.ca/~bremner//teaching/old/cs2023/events/L34/
<a href="../../whyCC/">by-nc-sa-2.5</a>
Copyright 2020, David Bremner
2010-09-11T20:23:13Z
2009-11-30T04:00:00Z
<ul>
<li><p>Chapter 17 variable sized structs. Chapter 15, multiple compilation units, include files</p></li>
<li><p><span class="createlink">slides</span>
<span class="createlink">printable</span></p></li>
<li><p><a href="https://www.cs.unb.ca/~bremner//teaching/old/cs2023/about_slides/">about slides</a></p></li>
</ul>
Lecture 33
https://www.cs.unb.ca/~bremner//teaching/old/cs2023/events/L33/
<a href="../../whyCC/">by-nc-sa-2.5</a>
Copyright 2020, David Bremner
2010-09-11T20:23:13Z
2009-11-27T04:00:00Z
<ul>
<li><p>Chapter 17 Dynamic memory allocation, linked lists, variable sized structs</p></li>
<li><p><span class="createlink">slides</span>
<span class="createlink">printable</span></p></li>
<li><p><a href="https://www.cs.unb.ca/~bremner//teaching/old/cs2023/about_slides/">about slides</a></p></li>
</ul>
Lecture 29
https://www.cs.unb.ca/~bremner//teaching/old/cs2023/events/L29/
<a href="../../whyCC/">by-nc-sa-2.5</a>
Copyright 2020, David Bremner
2010-09-11T20:23:13Z
2009-11-18T04:00:00Z
<ul>
<li><p>Chapter 16 Structures, Unions</p></li>
<li><p><span class="createlink">slides</span>
<span class="createlink">printable</span></p></li>
<li><p><a href="https://www.cs.unb.ca/~bremner//teaching/old/cs2023/about_slides/">about slides</a></p></li>
</ul>
Lecture 28
https://www.cs.unb.ca/~bremner//teaching/old/cs2023/events/L28/
<a href="../../whyCC/">by-nc-sa-2.5</a>
Copyright 2020, David Bremner
2010-09-11T20:23:13Z
2009-11-16T04:00:00Z
<ul>
<li><p>Chapter 16 Structures</p></li>
<li><p><span class="createlink">slides</span>
<span class="createlink">printable</span></p></li>
<li><p><a href="https://www.cs.unb.ca/~bremner//teaching/old/cs2023/about_slides/">about slides</a></p></li>
</ul>