problem with qsort and array via calloc

This is a discussion on problem with qsort and array via calloc within the C Programming forums, part of the General Programming Boards category; Hello there, I am having the following problem. I am making an array of structures. The struct contains an int. ...

  1. #1
    Registered User
    Join Date
    Nov 2009
    Posts
    4

    problem with qsort and array via calloc

    Hello there,

    I am having the following problem.

    I am making an array of structures.
    The struct contains an int.
    I give this struct array to qsort to sort the struct array according to this int.
    This works if I create the struct array like

    struct Point track[] = { {5}, {4}, {3} };

    for example.

    However, I need to have a variable array size and therefore I want to use calloc.
    But if I create the struct array via calloc, qsort won't sort anymore.

    See bellow for the code that reproduces my problem.

    If I use the calloc part instead, qsort won't sort.

    Any ideas what's going on?

    Thanks.


    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    struct Point { int x; };
    
    int Point_cmp(const void *a, const void *b)
    {
        struct Point *ia = (struct Point *)a; 
        struct Point *ib = (struct Point *)b;
        return ia->x  - ib->x; 
    }
    
    
    main() {
      size_t m = 4;
      struct Point Circle[] = { {4}, {1}, {0}, {-1} };
      //struct Point * Circle = NULL;
      //Circle = (struct Point *) calloc( m, sizeof(struct Point));
    
      size_t i;
      for ( i = 0; i < m; i++ ) {
        Circle[i].x = 100 - i;
      }
    
      for ( i = 0; i < m; i++ ) {
        printf("%d ",Circle[i].x);
      }
      printf("\n");
    
      size_t Circle_leng = sizeof(Circle)/sizeof(struct Point);
      qsort(Circle, Circle_leng, sizeof(struct Point), Point_cmp);
    
      for ( i = 0; i < m; i++ ) {
        printf("%d ",Circle[i].x);
      }
      printf("\n");
    
    
      return 0;
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    23,773
    The problem lies here:
    Code:
    size_t Circle_leng = sizeof(Circle)/sizeof(struct Point);
    This works for determining the number of elements of the array Circle when it is indeed an array of struct Point, but when it is a pointer, sizeof(Circle) gives the size in bytes of the pointer, not the array.

    From what I see, you can just use m instead.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Nov 2009
    Posts
    4
    Yuhooo, you are right.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using qsort to sort an array of strings
    By MSF1981 in forum C Programming
    Replies: 31
    Last Post: 05-17-2009, 01:31 AM
  2. qsort() in Array of Pointer to String
    By vb.bajpai in forum C Programming
    Replies: 8
    Last Post: 06-16-2007, 04:18 PM
  3. malloced 2d array passed to qsort
    By jamie85 in forum C Programming
    Replies: 7
    Last Post: 11-25-2005, 12:55 PM
  4. using qsort to sort an array of strings
    By bobthebullet990 in forum C Programming
    Replies: 6
    Last Post: 11-25-2005, 07:31 AM
  5. qsort( ) on a structured array
    By Thumper333 in forum C Programming
    Replies: 2
    Last Post: 10-21-2004, 07:39 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21