0

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 :)

5
  • explain "the type of return value isn't right ", what error are you getting? Commented Feb 1, 2016 at 19:22
  • Compiling errors: expected unqualified-id before ‘)’ token and error: expected initializer before ‘fillArray’ Commented Feb 1, 2016 at 19:24
  • What kind of type do you think a double (*)[3] is? Commented Feb 1, 2016 at 19:26
  • Shouldn't it be the 2D array? Commented Feb 1, 2016 at 19:28
  • @Mirelle, technically, double (*)[3] is a pointer to an array of 3 doubles, which might or might not be the first element of a 2D array of doubles. It is the type that a 2D array of double will decay to if its second dimension is 3. And it is, to be sure, a valid type. Commented Feb 1, 2016 at 19:35

2 Answers 2

1

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.

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

4 Comments

I see, so I should just pass the whole array as an argument to the function? Would that be a better solution? The code you posted doesn't work for me.. and I don't really understand how you dereferenced.. a function? I am sorry I am still a newbie to C so..
@mireille, you can read the declaration as saying "fillValue is a function accepting one argument of type int *; dereferencing its return value yields an array of 3 doubles." As I pointed out, the form is quite parallel to that of your variable declarations.
@mireille, since inside your function you (re)allocate space for the pointer to point to, it would not be sufficient to pass your pointer as an argument. You would need to pass a pointer to the pointer, for the same reason that you now pass a pointer to the int. I think you're already dealing with enough type complexity -- and admirably so for a self-proclaimed newbie, if I may say so. Do make sure you've copied my declaration correctly, and if you're still getting errors then make sure that they don't refer to something else.
Think of it this way: you can replace a variable name with a function name and parameter list, and you'll get a function returning that type, instead of a variable. int (*array_ptr)[3] -> int (*foo(int *n))[3] by replacing array_ptr with foo(int *n) (Likewise, you get the array pointer in the first place by replacing array with (*array_ptr) in int array[3];)
1

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

Comments

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.