mireille
mireille

Reputation: 11

2D allocated array (fixed number of columns) as return value of a function

I would like some help with pointers: in main function I have initialized variable that should point to the array:

int main() {

int n;
double (*array)[3];
array = fillArray(&n);

The function receives an integer argument, which counts number of rows. The return value of the function should be a pointer to the newly created array, which will be saved to the variable 'array' in main function:

double (*)[3] fillArray(int * n) {
    double (*array)[3] = NULL;
    int allocated = 0;
    *n = 0;

    while (1)
    {
        /*scanning input*/

        if (allocated <= *n)
        {
        allocated += 10;
        array = (double(*)[3]) realloc (array, sizeof(*array) * allocated)
        }
        array[*n][0] = value1;
        array[*n][1] = value2;
        array[*n][2] = value3;
        (*n)++;
    }
    return array;
}

However, the type of return value isn't right and I am kinda lost. Can anyone tell me what is wrong in this code?

Thank you in advance :)

Upvotes: 0

Views: 83

Answers (2)

user3629249
user3629249

Reputation: 16540

given some guessing about items not mentioned in the question.

I think this is what you are looking for.

Notice the checking for success of the call to realloc()

Notice the #define of the magic numbers

#include <stdlib.h> // realloc(), exit(), EXIT_FAILURE

#define ALLOCATION_INCREMENT (10)
#define NUM_DOUBLES (3)

struct tagArray
{
    double arrayEntry[ NUM_DOUBLES ];
};

struct tagArray *fillArray(int *n);

int main( void )
{

    int n = 0;
    struct tagArray *array;

    if( NULL == (array = fillArray(&n) ) )
    { // then array generation failed
        exit( EXIT_FAILURE );
    }

    // implied else, array generation successful

    ....

    free( array );
    return 0;    
} // end function: main


struct tagArray *fillArray(int *n)
{
    struct tagArray *array = NULL;
    int allocated =0;

    while( 1 )
    {
        /* scanning input,
         * to acquire 'value1, value2, value3'
         * with some key input causes execution of 'break;'
         * */

        if( allocated <= *n )
        {
            allocated += ALLOCATION_INCREMENT;
            struct tagArray *temp = realloc (array, sizeof( struct tagArray) * allocated );

            if( !temp )
            { // then realloc failed
                free( array );
                return( NULL );
            }

            array = temp;
        }

        array[*n][0] = value1;
        array[*n][1] = value2;
        array[*n][2] = value3;
        (*n)++;
    }

    return array;
} // end function: fillArray

Upvotes: 1

John Bollinger
John Bollinger

Reputation: 180306

Your code has an unrelated syntax error and some undeclared variables, but the problem you asked about has to do with the form of the declaration of function fillArray(). This alternative works for me:

double (*fillArray(int * n))[3] {
    double (*array)[3] = NULL;

    /* ... */

    return array;
}

Note the similarity in form to the declarations of your variables of the same type.

The problem is that although double (*)[3] is a perfectly valid type designator for use, say, in a cast, it is incorrect to use it quite as you tried to do to declare the type of an object.

Upvotes: 1

Related Questions