简体   繁体   中英

Conflicting types when returning array of strings

I want to catch an array of filenames(strings) from getListOfFiles() and use it in my main. I have tried my every idea and there is still appearing an error:

gcc testowe.c -o test testowe.c: In function ‘main’:
testowe.c:8:15: warning: implicit declaration of function ‘getListOfFiles’ [-Wimplicit-function-declaration]
 char **list = getListOfFiles(argv[1]);

testowe.c:8:15: warning: initialization makes pointer from integer without a cast [-Wint-conversion]

testowe.c: At top level:
testowe.c:11:8: error: conflicting types for ‘getListOfFiles’
 char** getListOfFiles(char *path) {

testowe.c:8:15: note: previous implicit declaration of ‘getListOfFiles’ was here
 char **list = getListOfFiles(argv[1]);
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <dirent.h>

int main(int argc, char *argv[])
{
char **list = getListOfFiles(argv[1]);
}

char** getListOfFiles(char *path) {
    int n=0, i=0;
    DIR *d;
    struct dirent *dir;
    d = opendir(path);

    while((dir = readdir(d)) != NULL) {
        if ( !strcmp(dir->d_name, ".") || !strcmp(dir->d_name, "..") )
        {

        } else {
            n++;
        }
    }
    rewinddir(d);

    char **filesList;
    filesList = malloc((n + 1)*sizeof(char*));

    while((dir = readdir(d)) != NULL) {
        if ( !strcmp(dir->d_name, ".") || !strcmp(dir->d_name, "..") ){

        }else {
            filesList[i] = (char*) malloc (strlen(dir->d_name)+1);
            strncpy (filesList[i],dir->d_name, strlen(dir->d_name) );
            i++;
        }
    }
    filesList[i] = NULL;
    closedir(d);
    return filesList;
}

What's the real problem cause I think all types and pointers look fine?

Read the warnings: implicit declaration of function 'getListOfFiles' .

You are using getListOfFiles() not seen it before and so assumes that it returns an int. You can solve by either declaring the function before defining it. char** getListOfFiles(char *path) near the top of your file or for this simple case just move the whole definition before main()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM