1

So I have to write a program that sorts a 2d array and then returns it but I just found out you cant return an array from a function. I tried researching this but nothing fixes the problem. Any help is appreciated, thanks.

Below is the function I wrote to sort the vertical rows.

int vertSort(int matrix[][51], int size)
{
    for(c=0;c<size;c++)
    {

        for(r=0;r<size-1;r++)
        {
            if(matrix[c][r]>matrix[c][r+1])
            {
                printf("Swap row %d column %d with row %d column %d", r, c, r, c)
                int tmp=matrix[c][r];
                matrix[c][r]=matrix[c][r+1];
                matrix[c][r+1]=tmp;


            }

        }
    }
    return matrix;


}
6
  • 1
    Return? Why? You are passing this array into your function. Commented Oct 24, 2014 at 3:01
  • It can be returned as a 2D pointer. But still the r,c values are returned separately. Commented Oct 24, 2014 at 3:01
  • why dont you pass it as a reference. Commented Oct 24, 2014 at 3:17
  • Even if you could return an array from a function, it wouldn't do you any good, since you can't assign to arrays in C. Commented Oct 24, 2014 at 3:25
  • for(r=0;r<size-1;r++) does not match with 51 in the signature; replace size by 51 or vice versa Commented Oct 24, 2014 at 3:25

3 Answers 3

3

Your code does not need to return anything, it modifies the caller's copy of the array. (Arrays cannot be passed by value in C). Change the int return type to void and all will be well.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you for the help! I switched the int return type to void but now when I try and run the program I have an error at the bottom that says warning conflicting types for 'vertSort'
change it in the prototype also
0

The correct syntax is impossibly messy, so I suggest using a typedef as shown below

typedef int (*ptr51)[51];

ptr51 vertSort( int matrix[][51], int size )
{
    matrix[0][2] = 99;
    matrix[1][1] = 88;
    return( matrix );
}

int main( void )
{
    int test[2][51] = { { 10, 20, 30 }, { 40, 50, 60 } };

    ptr51 output = vertSort( test, 0 );

    for ( int row = 0; row < 2; row++ )
    {
        for ( int col = 0; col < 3; col++ )
            printf( "%d ", output[row][col] );
        printf( "\n" );
    }
}

Here is the impossibly messy syntax without a typedef

int (*(vertSort( int matrix[][51], int size )))[51]
{
    matrix[0][10] = 99;
    matrix[1][50] = 88;
    return( matrix );
}

int main( void )
{
    int test[2][51] = { { 10, 20, 30 }, { 40, 50, 60 } };

    int (*output)[51] = vertSort( test, 0 );

    for ( int row = 0; row < 2; row++ )
    {
        for ( int col = 0; col < 51; col++ )
            printf( "%d ", output[row][col] );
        printf( "\n" );
    }
}

Comments

-1

function's return type should be int ** and typecast matrix to int **. It should work for you.

int** vertSort(int matrix[][51], int size)
{
     for(c=0;c<size;c++)
     {

        for(r=0;r<size-1;r++)
        {
             if(matrix[c][r]>matrix[c][r+1])
             {
                printf("Swap row %d column %d with row %d column %d", r, c, r, c)
                int tmp=matrix[c][r];
                matrix[c][r]=matrix[c][r+1];
                matrix[c][r+1]=tmp;


              }

         }
    }
    return (int **)matrix;


}

4 Comments

I think C will show error when int [][51] is accessed as int **.
To be pedantic, it's a warning, not an error (with clang, anyway), but you are of course right; it is bad practice to ignore warnings, so the explicit case is nice. +1
It's bad practice to ignore warnings, but a worse practice to cast them away. If the thing passed to vertSort() actually is a two-dimensional array, returning it as an int ** and trying to access it via that pointer will be disastrous. If you actually are passing in an int **, you'll get the same problem in reverse. That warning is there for a good reason.
This is completely wrong, arrays are different to pointers. Silencing compiler warnings with a cast doesn't cause magic.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.