ARGV help in C

Hi,
Can somehelp help how to list file in a dir?

Here is a minimalist example:

#include <stdio.h>
#include <sys/types.h>
#include <dirent.h>

int
main(int argc, char *argv[])
{
   struct dirent *ent;
   DIR *dir;

   if (argc == 2) {
       if ((dir = opendir(argv[1]))) {
           while((ent = readdir(dir)) != NULL)
               puts(ent->d_name);
       } else
           fprintf(stderr, "Error opening directory %s\n", argv[1]);
   }

   return 0;
}

Thanks Sir,
If you dont mind, can you please explain how this code is working?

Have a read of the Man Page for readdir (All Section 3posix) - The UNIX and Linux Forums

Regards

argv is an array of strings, argc is a count of them. argv[0] is, by tradition, the name of the program being called. argv[1] would be the first argument, if any. So a program called with no arguments would have argc=1 and argv[0] as the name of the program, with one argument would have argc=2 and argv[0] as the program name and argv[1] as the first argument, etc.

I think the confusion lies in shell globbing - turning the * character into a list of files that are then picked up in the **argv array.

experiment with:

#!/bin/ksh

set +f
ls *
set -f
ls *

-f turns off globbing; +f turns on globbing.