title: C printf: variable string column width, left align
body: <p>I'm trying to format printf string output:</p>
  
  <pre><code>printf("%*s %*s %*s %*s %*s %*s %*s %*s %*s\n", 
  maxlen[0], "intf", maxlen[1], "ssid", maxlen[2], 
  "bssid", maxlen[3], "vlan", maxlen[4], "STA", 
  maxlen[5], "tx-pkts", maxlen[6], "rx-pkts", maxlen[7],
   "security", maxlen[8], "Capabilities");
  </code></pre>
  
  <p>where <code>maxlen</code> is an array with values 5, 13,17, 4, 3, 7, 7, 15, and 12 in this case.</p>
  
  <p>However, in the output, the strings are right aligned, meaning they are floated to the right of their width-sized blocks. Output:</p>
  
  <p><code>intf          ssid             bssid vlan STA tx-pkts rx-pkts        security Capabilities</code></p>
  
  <p>Is there a way to float these strings to the left?</p>
  
  <p>So instead of <code>[][][][][][][]security</code>, I want <code>security[][][][][][][]</code>for the <code>width==15</code>second-to-last block, for example.</p>
  
  <p>I tried creating a <code>spaces</code>array of strings, like below:</p>
  
  <p><code>char **spaces = (char**)malloc(9*sizeof(char*));</code></p>
  
  <p>then, for each <code>i</code>,  <code>calloc</code>  <code>spaces[i]</code>, and then, instead of doing <code>"%*s"</code>, I do <code>%s%s</code>, where the second string printed is <code>spaces[i]</code>.</p>
  
  <p>But even this doesn't seem to work because I would need to do </p>
  
  <pre><code>snprintf(spaces[i], maxlen[i]-size_of_string, "  (however many spaces)    ");
  </code></pre>
  
  <p>And I don't know how many spaces to hardcode for the third argument. There is no <code>" " * length</code> operation like in python, to create a <code>length</code> sized string of <code>" "</code> characters.</p>