Domanda

is there any way to use static array, defined in main(), in another function, without giving it to the function as a parameter?

For example:

main() has defined array:

int Array[10];

filled with integers. I'd like to create a comparing function for qsort, that has to have this header:

int compar (const void* a, const void* b);

and I would like it to decide like this:

if Array[a]<Array[b] return 1

etc...

This array cannot be given to qsort directly, but is required for exact sorting.

Also, this array has to be static (no reallocing).

Does anyone have any ideas?

È stato utile?

Soluzione

The only way is, of course, so make the address of the array available as a global variable.

This is possible even if the array itself is inside main(), but you have to initialize the global to the properly scoped address, and watch the life-time, of course.

int *mains_array;

static int qsort_callback(const void *a, const void *b)
{
  /* use mains_array */
}

int main(void)
{
  int secret_array[100];

  mains_array = secret_array;
  qsort(something, something, qsort_callback);
}

It's a pretty ugly solution, it should be given more thought.

Altri suggerimenti

You can't access a local variable from another function. You need to make it global or file scoped.

Global:

/* Declare Array outside any function */
int Array[10];

int main(...

or, file scoped:

/* Declare Array outside any function */
static int Array[10];

int main(...

Note:

Your compare function will receive pointers to the elements to compare. If you are sorting an array of int you need to dereference the pointer in your compare function:

I'm assuming that Array isn't the array you want to sort, but an array that contains information on how to sort an array.

int compare (const void * ap, const void * bp)
{
    int a = *((int*)ap);
    int b = *((int*)bp);

    if (Array[a] < Array[b]) {
      return 1;
    }
    if (Array[a] > Array[b]) {
      return -1;
    }
    return 0;
}

qsort requires the address of the array, so you don't have a choice. But where is defined the array, that does not matter. You just need to be able to refer to it.

The qsort signature is:

   void qsort(void *base, size_t nmemb, size_t size,
              int (*compar)(const void *, const void *));

So you will call it by:

qsort(Array, 10, sizeof(int), compar);

And you'll do the compare function as usual:

int compar (const void* a, const void* b) {
    return *((int*)a) - *((int*)b);
}

You need to understand that the values passed to compar are not the indexes but the address of the cells of your array. So you don't have to use Array from the compar function, you already have what you need.

You're already giving him your array by calling

qsort (a, numberOfElements, sizeOfEachElement, compare);

What you need to do in your compare function is this:

int compare (const void * a, const void * b)
{
    //Here int can be other type
    return ( *(int*)a - *(int*)b );
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top