qsort 함수로 구조체의 멤버를 정렬할 때...

kernelbomb의 이미지

안녕하세요? 아래와 같은 구조체의 멤버를 qsort로 정렬할려고 하는데

동작은 되지만 경고가 계속 뜹니다.

struct file_info
{
    char fname[256];
    int    atime;
};

int compare_atime(struct file_info *a, struct file_info *b)
{
    if(a->atime > b->atime)
        return 0;
    else
        return 1;
}

int main(void)
{
    int i;
    struct file_info array[5];

    strcpy(array[0].fname, "abc.txt");
    array[0].atime = 102343;

    strcpy(array[1].fname, "def.txt");
    array[1].atime = 102342;

    strcpy(array[2].fname, "ghi.txt");
    array[2].atime = 123455;

    strcpy(array[3].fname, "jkl.txt");
    array[3].atime = 324321;

    strcpy(array[4].fname, "mno.txt");
    array[4].atime = 435411;

    qsort(array, 5, sizeof(struct file_info), compare_atime);

    for(i = 0; i < 5; i++)
         printf("file_time : %d\n", array[i].atime);

    return 0;
}

경고내용>
warning: passing arg 4 of `qsort' from incompatible pointer type

경고 내용을 보니 함수포인터의 인자가 void *가 아니라서 그런거 같은데요.

qsort의 함수포인터에 구조체를 넘길려고 어떻게 해야 할지 모르겠습니다.

int compare_atime(const void *a, const void *b)
{
    struct file_info *pa;
    struct file_info *pb;

    if(((int *)a->atime) > ((int *)b->atime))
         return 0;
    else
         return 1;
}

이렇게 하면 경고는 안나지만 atime값이 -값이 들어가네요. ㅜㅜ;

cdpark의 이미지

우선 qsort에서 쓰는 compare 함수는 0/1이 아니라 -/0/+ 값이 나와야 합니다.

우선 위 소스가 돌긴 도나요? -_- 그리고 (int *)로 캐스팅하다뇨?

int compare_atime(const void *a, const void *b) 
{ 
    struct file_info *pa = a; 
    struct file_info *pb = b;

    return pa->atime - pb->atime; 
}
kernelbomb의 이미지

int compare_atime(const void *a, const void *b) 
{ 
    struct file_info *pa = (struct file_info *)a; 
    struct file_info *pb = (struct file_info *)b; 

    return pa->atime - pb->atime; 
}

이렇게 하니깐 잘 되네요. 감사합니다. 제가 급하게 코드를 올리다 보니

이상한걸 올렸네요. ㅜㅜ;

당신들을 사랑합니다.

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.