fork download
  1. #include <stdio.h>
  2. #include <string.h>
  3.  
  4. int main() {
  5. char buf[ 256 ];
  6.  
  7. strcpy( buf, "./a.out foo bar foobar barfoo\n" ); // simulate fgets()
  8.  
  9. int ac = 0;
  10. char *av[ 32 ]; // Or make it dynamic (a minor challenge)
  11.  
  12. av[ac] = strtok(buf, " \n" );
  13. while( av[ac])
  14. {
  15. ++ac;
  16. av[ac] = strtok(NULL, " \n" );
  17. }
  18.  
  19. for( int i = 0; i < ac; i++ )
  20. printf( "%d: %s\n", i, av[i] );
  21. printf( "And on the end of the array: %p\n", (void*)av[ac] );
  22.  
  23. return 0;
  24. }
Success #stdin #stdout 0s 5348KB
stdin
Standard input is empty
stdout
0: ./a.out
1: foo
2: bar
3: foobar
4: barfoo
And on the end of the array: (nil)