Array Passing Problem

closed account (zb0S216C)
Basically, I have a function that requires 2 parameters:
1) An index.
2) The array to access.

What the function is supposed to do is: access the specified index in the given array and print it's value to the console screen.

Here is the function deceleration:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <stdio.h>

// The function.
void Function( unsigned uIndex = 0, float *p_pArray[ ] = NULL )
{
    if( p_pArray != NULL )
    {
        // Print the value at the specified index.
        printf( "The value at %i is: %f.\n", uIndex, *p_pArray[ uIndex ] );
        return;
    }
}

// This is the array that will be passed.
float t_fArray[ 3 ] = { 5.0f, 2.3f, 8.9f };

int main( )
{
    // Call the function.
    Function( 0, &t_fArray );  // Error points to here.

    return 0;
}


The error message:
cannot convert 'float (*)[3]' to 'float**' for argument '2' to 'void Function(unsigned int, float**)'

Any idea on what's going on?

Note: I know that a return statement in a void function is pointless but I
use them as a personal preference.
I think it's this: in your function definition, you define Function() as taking an unsigned int and an array of pointer to floats, while on line 20 in main() you pass it the address of an array of floats.
closed account (zb0S216C)
Thanks Zhuge!. I changed the code and now it now works.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <stdio.h>

// The function.
void Function( unsigned uIndex = 0, float p_pArray[ ] = NULL )
{
    if( p_pArray != NULL )
    {
        // Print the value at the specified index.
        printf( "The value at %i is: %f.\n", uIndex, p_pArray[ uIndex ] );
        return;
    }
}

// This is the array that will be passed.
float t_fArray[ 3 ] = { 5.0f, 2.3f, 8.9f };

int main( )
{
    // Call the function.
    Function( 0, t_fArray );

    return 0;
}
Topic archived. No new replies allowed.