0

I wrote this program that is supposed to sort NxN array. It gets compiled but doesn't work because the pointer type is incompatible.

I just need help with the pointers as argument. I get incompatible pointer type warning for both functions swap and dblArraySort. Any idea why is that ?

thanks in advance !

#include <stdio.h>
#include <stdlib.h>

void
swap(int **a, int **b)
{
    int temp;

    temp = **a;
    **a = **b;
    **b = temp;
}

void
dblArraySort(int **dblArray, int arrLen)
{
    int chkIndex;
    int i, j, k;

    for (i = 0; i < arrLen; i++) {
        if ((i + 1) % 2 == 0) {
            for (j = 0; j < arrLen; j++) {
                chkIndex = dblArray[i][j];

                for (k = 1; k + j < arrLen; k++)
                    if (chkIndex < dblArray[i][k + j])
                        swap(&dblArray[i][j], &dblArray[i][k + j]);
                    else
                        continue;
            }
        } else {
            for (j = 0; j < arrLen; j++) {
                chkIndex = dblArray[i][j];

                for (k = 1; k + j < arrLen; k++)
                    if (chkIndex >= dblArray[i][k + j])
                        swap(&dblArray[i][j], &dblArray[i][k + j]);
                    else
                        continue;
            }
        }
    }
}

int
main()
{
    unsigned int arrayLength;

    printf("Provide array size: \n");
    scanf("%d", &arrayLength);

    int doubleArray[arrayLength][arrayLength];

    for (int i = 0; i < arrayLength; i++) {
        for (int j = 0; j < arrayLength; j++) {
            scanf("%d", &doubleArray[i][j]);
        }
    }

    dblArraySort(doubleArray, arrayLength);

    for (int i = 0; i < arrayLength; i++) {
        for (int j = 0; j < arrayLength; j++) {
            printf("%d ", doubleArray[i][j]);
        }
        printf("\n");
    }

    return 0;
}

I tried the code mentioned above

chqrlie
  • 131,814
  • 10
  • 121
  • 189
moran a
  • 3
  • 1
  • 1
    _"expected 'int **' but argument is of type 'int *'"_ - You should make warnings into errors to not even let it compile such a mismatch. You also use the wrong format string in your `scanf`. Since you read into an `unsigned`, use `"%u"`, not `"%d"` – Ted Lyngmo Feb 06 '23 at 21:28
  • In order to gauge your level of understanding of your own code and make a suitable answer, please explain your understanding of this `&dblArray[i][j]` which you give as a parameter to swap(). Explain the semantic meaning and the syntactical aspects. Then compare it to the expectation of `**int`. – Yunnosch Feb 06 '23 at 21:57
  • 1
    Note that `int **` does **not** refer to any two-dimensional array. It refers to a *pointer* to an array of one or more `int *` *pointers*, each of which refer to individual and separate one-dimensional arrays of one or more actual `int` values. See [**Correctly allocating multi-dimensional arrays**](https://stackoverflow.com/questions/42094465/correctly-allocating-multi-dimensional-arrays) for a good explanation. – Andrew Henle Feb 06 '23 at 22:09

1 Answers1

0

Arrays in C can be confusing. The thing you need to worry about is element type.

  • The element type of int ** dblArray is int *. In other words, dblArray is an array of int *s.

  • However, the element type of int doubleArray[arrayLength][arrayLength] is int row_type[arrayLength]. That is not an int *, that is an array, which is a totally different thing.

Moreover, when you use an array⟶pointer conversion, as happens when you say:

dblArraySort(doubleArray, arrayLength);  // doubleArray is converted to a pointer

You get a pointer to the array, which in this case is a pointer to the innermost element type, an int — which is also not an int *.

tl;dr: You are trying to pass an array of array of int to a function taking an array of pointer to int. That won’t work.


I would like to comment on your variable naming as well. When you say “double” or “dbl”, as in doubleArray and dblArray the very first thing people will think is that you are handling a linear array of type double, which is also not what the array is.

You have there a two-dimensional array. Not a “double” array. Common naming for such thing would be array2D or matrix.


To make it work you need either C11, which allows you to pass a VLA as:

void sort_array2D( size_t rows, size_t columns, int array[rows][columns] )
{
  ...
  int value = array[i][j];
  ...
}

int main(void)
{
  int array2D[Array_Length][Array_Length];
  ...
  sort_array2D( Array_Length, Array_Length, array2D );

Or you need to simply assume you must compute the index manually. A little function will help:

size_t index2D( size_t rows, size_t columns, size_t r, size_t c )
{
  (void)rows; // (quiet the compiler about not using this argument)
  return r * columns + c;
}

Then you can write your function as:

void sort_array2D( int * array, size_t rows, size_t columns )
{
  ...
  int value = array[index2D( rows, columns, i, j )];
  ...
}

int main(void)
{
  int array2D[Array_Length][Array_Length];
  ...
  sort_array2D( (int *)array2D, Array_Length, Array_Length );

I haven’t bothered to analyze your sort function. It doesn’t look right to me, but honestly, I’ve barely glanced at it. Calling a value from the array chkIndex looks fishy, since the values of the array are not indices per se, at least not in the context of sorting them.

Remember, when messing with arrays in C you need to keep strict care to not mix up the type of the elements. (Or the types of things in general, whether syntactic or conceptual.)

Dúthomhas
  • 8,200
  • 2
  • 17
  • 39