title: Returning and passing a file descriptor by reference in C
body: <p>I am building a Weather Station on a Raspberry Pi from scratch in C. I have a file with the code to get the temperature and I also have another file to call these functions.
  I have written code before to do this but the quality of the code was very poor and I am trying to re-write it better.</p>
  
  <p>I believe I am having my issue is understanding how to pass around the reference to the file descriptor pointing to the sensor.</p>
  
  <p>Please consider this code snippet from my software. (I am certain the error exists in this section).</p>
  
  <pre><code>#include "readTemp.h"
  #include &lt;stdio.h&gt;
  
  int main() {
      int fd;
      fd = initGPIO(0, 0x76);
      getTemp(fd);
      return(0);
  }
  </code></pre>
  
  <p>and the file readTemp.C</p>
  
  <pre><code>int * initGPIO(int i2cBus, int i2cAddr){
      int *addr = malloc(sizeof(int));
      int fd;
      //Initialise GPIO
      if(gpioInitialise() &lt; 0) {
          perror("Initialisation failed\n");
          exit(EXIT_FAILURE);
      }
      //Open the I2C bus
      fd = i2cOpen(i2cBus, i2cAddr, 0);
      if(fd &lt; 0) {
          perror("Device failed to  open\n");
          exit(EXIT_FAILURE);
      }
      //Send Reset
      if(i2cWriteByte(fd, CMD_RESET) != 0){
          perror("Error sending reset---\n");
      }
      addr = &amp;fd;
      printf("3-%d\n", &amp;addr);
      return(addr);
  }
  
  /*
   * Reads the calibration data from the PROMs on the sensor
   * Parameters - fd - File Descriptor of the i2c device
   *              *proms - Pointer to an array of 8 unsigned 16-bit integers
   */
  static int getCalibData(int *fd, u_int16_t *proms) {
      const int bytesToRead = 2;
      char buf[2] = {0};
      printf("2-%d\n", &amp;fd);
      // Populate for each prom (7)
      for(int i = PROM_START; i &lt; PROM_STOP; i = i + 2){
          // Write PROM read commands
          if(i2cWriteByte(*fd, i) != 0) {
              perror("Error writing PROM command!!\n");
              exit(EXIT_FAILURE);
          }
          // Read result from PROM
          if(i2cReadDevice(*fd, buf, bytesToRead) &lt;= 0) {
              perror("Error reading from PROM\n");
              exit(EXIT_FAILURE);
          }
          // Store result in array
          proms[(i - PROM_START) / 2] = (buf[0] &lt;&lt; 8) | (buf[1]);
      }
      return(0);
  }
  
  int getTemp(int *fd){
      u_int16_t proms[8];
      u_int32_t rawTemp = getRawTemp(fd);
      printf("%d\n", rawTemp);
      getCalibData(fd, proms);
      for(int i = 0; i &lt; 8; i++){
          printf("%d-%d\n", i, proms[i]);
      }
      int temp = calcTemp(rawTemp, proms);
      printf("---%d\n", temp);
      return 0;
  }
  </code></pre>
  
  <p>The code compiles but when run I get my <code>"Error writing PROM Command"</code> error.</p>
  
  <p>I think my error might lie in the small section of code where I am assigning the result of <code>initGPIO()</code> to an <code>int</code> where it should be to a pointer. But, when I do that my code seg faults.</p>