Big- D
Big- D

Reputation: 1

Passing an array of Struct to a sorting function in C

I'm trying to pass an array of struct to a function that works with it and modify its content. I've read many articles but I still cannot find what I need.

I need to pass this array of struct to a function and work with the values of this array, and the modifications need to be global modifications.

Here, I'm actually sorting the values of the areas of this structure; the program gives me this warning and crashes:

warning: passing argument 1 of 'insersort' from incompatible pointer type [-Wincompatible-pointer-types]|

struct rectangle{
    char name[MAXC];
    float x;
    float y;
    float area;
    float perimeter;
};

void insersort(struct rectangle *rect[],int k)
{
    int i,j;
    float x;
    for(i=1;i<k;i++)
    {
        x=rect[i]->area;
        j=i-1;
        while(j>=0 && x<rect[j]->area)
        {
            rect[j+1]->area=rect[j]->area;
            j--;
        }
        rect[j]->area=x;
    }
    return;
}
..... 

I call the function like so:

struct rectangle rect[MAX];
insersort(rect,k); 

Upvotes: 0

Views: 80

Answers (1)

Joshua
Joshua

Reputation: 43298

You wrote

void insersort(struct rectangle *rect[],int k)

and the body of insertof is written as though you meant

void insersort(struct rectangle (*rect)[],int k)

that is, array of pointer to rect

but you probably meant to write it as

void insersort(struct rectangle rect[],int k)

and not use -> inside insertof

Upvotes: 1

Related Questions