Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!sun-barr!ccut!kogwy!wnoc-tyo-news!sragwa!sran124!katsu From: katsu@sra.co.jp (WATANABE Katsuhiro) Newsgroups: comp.unix.questions Subject: Re: how to use scandir()? Message-ID: Date: 28 Aug 90 01:54:57 GMT References: <9008271747.AA02292@ws-38.cae.wisc.edu> Sender: news@sran124.sra.co.jp Organization: Software Research Associates, Inc.,Japan Lines: 46 In-reply-to: wyuen@CAE.WISC.EDU's message of 27 Aug 90 17:47:30 GMT # Sorry for my poor English. In article <9008271747.AA02292@ws-38.cae.wisc.edu> wyuen@CAE.WISC.EDU writes: > Could someone give examples of how to use scandir? > struct direct *(*namelist[]); is a reasonable declaration, > n = scandir(".", namelist, NULL, NULL) is a reasonable call, It's a `illegal pointer probrem'. The 2nd argument of scandir() must be the address in which you want scandir() to set the address of name list. Indeed TYPE of each identifiers are surely reasonable in your program. But `namelist' is uninitialized and it holds nonsense address and its VALUE is not reasonable. Anyway, my example: #include #include main(argc, argv) int argc; char **argv; { int i, n; struct direct **namelist; if ((n = scandir(".", &namelist, NULL, NULL)) < 0) { perror(argv[0]); exit(1); } for (i = 0; i < n; i++) printf("filename = %s\n", namelist[i]->d_name); /* example of free-ing */ for (i = 0; i < n; i++) { free(namelist[i]->d_name); } free(namelist); exit(0); } ----____----____ WATANABE Katsuhiro Software Research Associates, Inc. Japan. Not execute, but evaluate.