[C 언어] scandir의 3번째 인자 warning 좀 잡아주세요.

antz의 이미지

다음과 같이 warning이 납니다.

Quote:
common.c:939: warning: passing arg 3 of `scandir' from incompatible pointer type

다음은 939라인 입니다.
nCount = (int)scandir (pSrcDir, &files, file_select, alphasort);
다음은 3번째 인자 함수 입니다.

int file_select (struct direct *entry) 
{
    if ((strcmp (entry->d_name, ".") == 0)
            || (strcmp (entry->d_name, "..") == 0))
        return FALSE;

    return TRUE;
}

Quote:
int scandir(const char *dir, struct dirent ***namelist, int (*select)(const struct dirent *), int (*compar)(const struct dirent **, const struct dirent **));

에서 "int (*select)(const struct dirent *)" 의 의미를 명확히 모르겠습니다.
인자를 "const struct dirent *" 를 갖고, return값이 int 라는 것 같은데...
"(*select)"의 의미가 뭔지 모르겠습니다.

답변 부탁드립니다.

yielding의 이미지

Quote:

에서 "int (*select)(const struct dirent *)" 의 의미를 명확히 모르겠습니다.
인자를 "const struct dirent *" 를 갖고, return값이 int 라는 것 같은데...

인자를 const struct dirent*를 가지고 int를 리턴하는 함수의 포인터가 scandir의 3번째 인자라는 말입니다

Life rushes on, we are distracted

antz의 이미지

yielding wrote:
Quote:

에서 "int (*select)(const struct dirent *)" 의 의미를 명확히 모르겠습니다.
인자를 "const struct dirent *" 를 갖고, return값이 int 라는 것 같은데...

인자를 const struct dirent*를 가지고 int를 리턴하는 함수의 포인터가 scandir의 3번째 인자라는 말입니다

"(*select)" 가 함수라는 뜻인가요?

잘몰라서,
"int (*select)(const struct dirent *)"
보다는
"int (const struct dirent *)"
라고 생각했습니다.

sodomau의 이미지

return_type (*func_ptr)(parms...)
요렇게 생긴놈들을 함수포인터라고 합니다.
int (*select)(const struct dirent *);
이넘 역시 함수 포인터..
int func(const struct dirent *);
select = func;
select(...);
머 이렇게 쓸 수 있습니다.

함수 이름 자체도 함수 포인터나 마찬가지이니 위의 처음 코드처럼
그냥 쓰셔도 될텐데
아마도 경고가 그

int file_select(const struct dirent *entry)

라고 선언되어야 할 것이

int file_select (struct direct *entry)

요렇게 const가 빠져서.. (direct가 아니고 dirent)

생기는 것 같네요.

mollla wrote:
yielding wrote:
Quote:

에서 "int (*select)(const struct dirent *)" 의 의미를 명확히 모르겠습니다.
인자를 "const struct dirent *" 를 갖고, return값이 int 라는 것 같은데...

인자를 const struct dirent*를 가지고 int를 리턴하는 함수의 포인터가 scandir의 3번째 인자라는 말입니다

"(*select)" 가 함수라는 뜻인가요?

잘몰라서,
"int (*select)(const struct dirent *)"
보다는
"int (const struct dirent *)"
라고 생각했습니다.

antz의 이미지

감사합니다. warning이 사라졌네요. :-)

const가 빠져서 warning이 났던것 같습니다.

sodomau wrote:
요렇게 const가 빠져서.. (direct가 아니고 dirent)

제가 #include <sys/dir.h> 를 해서 에러가 안났던거군요.

dirent를 쓰는게 더 좋은것 같습니다.

Quote:
$ cat /usr/include/sys/dir.h
...
#define direct dirent
...