title: Unable to printf() char array in c
body: <p>I don't seem to be understanding how <code>printf()</code> formats the output of char arrays in C. I have a bit of code whose purpose is to translate an integer into a string of binary characters, and print it. The code works up to the point where I go to print the string returned from <code>integerToString()</code>, because it doesn't print the contents of the string.</p>
  
  <p>I've checked gdb to ensure that both <code>res</code> and <code>str</code> contained an array of the correct values before the <code>printf()</code> call. I've also tried printing <code>*res</code> instead of just <code>res</code>, but that returns <code>(null)</code>. What am I missing? Is there something about pointers that I am not grasping?</p>
  
  <p>Thanks much in advance.</p>
  
  <pre><code>#include&lt;stdio.h&gt;
  #include&lt;stdlib.h&gt;
  
  int main () {   
    printf("Please enter a positive integer: ");
    unsigned int input;
  
    scanf("%d", &amp;input);
  
    char* res = integerToString(input);
    printf("Represented in binary that integer is: %s\n", res);
  }
  
  char* integerToString(unsigned int number){
    unsigned int value = number; 
  
    char* str;
    str = malloc(33); //32 bits for the string, +1 null value on the end
  
    unsigned int i = 31;
  
    while (i &gt; 0){
      str[i] = value % 2;
      value /= 2;
      i--;
    }
    str[32] = '\0';
    return(str); 
  }
  </code></pre>