title: Passing pointers/references to structs into functions
body: <p>This is going to sound like a silly question, but I'm still learning C, so please bear with me. :)</p>
  
  <p>I'm working on chapter 6 of K&amp;R (structs), and thus far through the book have seen great success. I decided to work with structs pretty heavily, and therefore did a lot of work early in the chapter with the point and rect examples. One of the things I wanted to try was changing the <code>canonrect</code> function (2nd Edition, p 131) work via pointers, and hence return <code>void</code>.</p>
  
  <p>I have this working, but ran into a hiccup I was hoping you guys could help me out with. I wanted <code>canonRect</code> to create a temporary rectangle object, perform its changes, then reassign the pointer it's passed to the temporary rectangle, thus simplifying the code. </p>
  
  <p>However, if I do that, the rect doesn't change. Instead, I find myself manually repopulating the fields of the rect I'm passed in, which does work.</p>
  
  <p>The code follows:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  
  #define min(a, b) ((a) &lt; (b) ? (a) : (b))
  #define max(a, b) ((a) &gt; (b) ? (a) : (b))
  
  struct point {
      int x;
      int y;
  };
  
  struct rect {
      struct point lowerLeft;
      struct point upperRight;
  };
  
  // canonicalize coordinates of rectangle
  void canonRect(struct rect *r);
  
  int main(void) {
      struct point p1, p2;
      struct rect r;
  
      p1.x = 10;
      p1.y = 10;
      p2.x = 20;
      p2.y = 40;
      r.lowerLeft = p2; // note that I'm inverting my points intentionally
      r.upperRight = p1;
  
      printf("Rectangle, lower left: %d, %d; upper right: %d %d\n\n", 
          r.lowerLeft.x, r.lowerLeft.y, r.upperRight.x, r.upperRight.y);
  
      // can't pass a pointer, only a reference. 
      // (Passing pointers results in illegal indirection compile time errors)
      canonRect(&amp;r); 
      printf("Rectangle, lower left: %d, %d; upper right: %d %d\n\n", 
          r.lowerLeft.x, r.lowerLeft.y, r.upperRight.x, r.upperRight.y);    
  }
  
  void canonRect(struct rect *r) {
      struct rect temp;
      temp.lowerLeft.x = min(r-&gt;lowerLeft.x, r-&gt;upperRight.x);
      temp.lowerLeft.y = min(r-&gt;lowerLeft.y, r-&gt;upperRight.y);
      temp.upperRight.x = max(r-&gt;lowerLeft.x, r-&gt;upperRight.x);
      temp.upperRight.y = max(r-&gt;lowerLeft.y, r-&gt;upperRight.y);
  
      r = &amp;temp; // doesn't work; my passed-in rect remains the same
  
      // I wind up doing the following instead, to reassign all 
      // the members of my passed-in rect
      //r-&gt;lowerLeft = temp.lowerLeft;
      //r-&gt;upperRight = temp.upperRight;
  }
  </code></pre>
  
  <p>So here are the questions:</p>
  
  <ol>
  <li>Why does <code>r = &amp;temp;</code> not work? (I <strong>think</strong> this is because I pass in a reference instead of a pointer; am I correct in thinking that references are not modifiable but pointers are?)</li>
  <li>Why might I get an illegal indirection compile-time error if I try to pass in a pointer to <code>canonRect</code>? (IE, if I had <code>canonRect(*r);</code> in <code>main()</code>.)</li>
  </ol>
  
  <p>I suspect I already know the answer to #1, but #2 perplexes me -- I thought it was legal to pass pointers around.</p>
  
  <p>Anyway ... please forgive the C newb.</p>