title: C Structure to String
body: <p>So I have a structure that looks like this: </p>
  
  <pre><code>typedef struct {
  char *gpsId;
  char *type;
  char *name;
  double latitude;
  double longitude;
  int elevationFeet;
  char *city;
  char *countryAbbrv;
  } Airport;
  </code></pre>
  
  <p>I also have a function and the prototype for that function looks like this: </p>
  
  <pre><code>char * airportToString(const Airport *a);
  </code></pre>
  
  <p>I need to do what the name of the function suggest, I need to convert the Airport structure passed to a string and then using the driver program it will print the returned character array. I know about sprintf and all those ways but I don't want to print from this function I need to print from the the main function. I have some code where it is just a series of strcat but it seems like it is the wrong way of doing things PLUS when it gets to latitude it would fail because you can't use strcat to put a double into a string. The other stipulation is that I have to allocate the string dynamically so I had a line for malloc that looks like: </p>
  
  <pre><code>char * array = (char * ) malloc((sizeof(Airport) * 1) + 8);
  </code></pre>
  
  <p>but I think that will also present more errors than it is worth, the + 8 is just for the spaces for formatting and a null terminator at the end, but if got around to converting the doubles or int to string and they were large it would exeed the array bounds and go out of bounds correct? What is the best way to accomplish this task what I need to do is:</p>
  
  <p>Constructs a new string that represents the given airport.  The details of formatting can be anything, but it should be readable and provide a reasonable
  amount of details about the Airport structure.  Moreover, the returned string
  should be dynamically allocated.</p>