#include<dirent.h>
#include<stdio.h>
#include<stdlib.h>
#include<sys/stat.h>
int main ()
{
struct dirent **namelist;
int i,j;
char userd[20];
struct stat statBuf;
printf("Enter a directory %s\n",userd);
scanf("%s",&userd);
printf("the dir is %s\n",*userd);
        i=scandir(".",&namelist,0,alphasort);
        printf("enter a directory name %s",*userd);
        printf("scandir returned i=%d\n",&i);

if (i<0)
perror("Scandir failed to open directory I hope you understand \n");
else
 {
        for(j=0;j<i;j++)
        {
          printf("j=%d i=%d %s\n",j,i,namelist[j]->d_name);
         // lstat
          free(namelist[j]);
        }
 }
free(namelist);
}

Can some one help to understand why am I getting warning in above code?

Recommended Answers

All 2 Replies

Which one are you talking about? I tried compiling your program and got several warnings and errors.e.g

gcc testc.c -Wall -ansi -pedantic -o testc.c
testc.c: In function ‘main’:
testc.c:12: warning: format ‘%s’ expects type ‘char *’, but argument 2 has type ‘char (*)[20]’
testc.c:13: warning: format ‘%s’ expects type ‘char *’, but argument 2 has type ‘int’
testc.c:14: warning: implicit declaration of function ‘scandir’
testc.c:14: error: ‘alphasort’ undeclared (first use in this function)
testc.c:14: error: (Each undeclared identifier is reported only once
testc.c:14: error: for each function it appears in.)
testc.c:15: warning: format ‘%s’ expects type ‘char *’, but argument 2 has type ‘int’
testc.c:16: warning: format ‘%d’ expects type ‘int’, but argument 2 has type ‘int *’
testc.c:10: warning: unused variable ‘statBuf’

Can some one help to understand why am I getting warning in above code?

What compiler are you using? Because that makes a difference. I just compiled your source with the gcc compiler and got these warnings:

test.c: In function ‘main’:
test.c:12: warning: format ‘%s’ expects type ‘char *’, but argument 2 has type ‘char (*)[20]’
test.c:13: warning: format ‘%s’ expects type ‘char *’, but argument 2 has type ‘int’
test.c:15: warning: format ‘%s’ expects type ‘char *’, but argument 2 has type ‘int’
test.c:16: warning: format ‘%d’ expects type ‘int’, but argument 2 has type ‘int *’

If you don't resolve these warnings you'll most likely get a segmentation fault error at run-time,

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.