title: printf %s segfaulting - why?
body: <p>I was hoping this would o/p the last letter of my argv[1]: </p>
  
  <pre><code>$ cat input_string_fu.c 
  #include &lt;stdio.h&gt;
  
  int main(int argc, char *argv[])
  {
      if (argc !=2){
         printf("Error: Invalid syntax\n");
         return 1;
      };
  
      int a;
      a = strlen(argv[1]);
      if (a &gt;= 2){
          printf("Last char of %s : %c\n", argv[1], argv[1][a-1] );
          printf("Last char of %s : %s\n", argv[1], argv[1][a-1] );
      } 
      return 0;
  }
  </code></pre>
  
  <p>I am confused as to why is printf("%s") on argv[1][a-1] segfaulting. </p>
  
  <pre><code>$ ./a.out stackoverflow
  Last char of stackoverflow : w
  Segmentation fault
  $ 
  </code></pre>
  
  <p>Thank you for your time and expertise! </p>
  
  <p><strong>EDIT 1:</strong> </p>
  
  <p>Thanks for the guidance. TIL, <code>printf("%s\n", argv[1][a-1]);</code> was segfaulting because the %s format specification in printf expects the corresponding argument to be a null terminated char array. </p>
  
  <p>either, <strong>(1)</strong> pass a pointer:</p>
  
  <pre><code>printf("%s\n", &amp;argv[1][a-1]);
  </code></pre>
  
  <p>or, as : </p>
  
  <pre><code>char *ptr = argv[1];
  printf("%s\n", &amp;ptr[a-1]);
  </code></pre>
  
  <p>Or, <strong>(2)</strong> use char:</p>
  
  <pre><code>printf("%c\n", argv[1][a-1]); 
  </code></pre>