0

I get the following errors:

expected 'float **' but argument is of type 'float (*)[3]' and expected 'float * (*)[3]' but argument is of type 'float (*)[3][3]'

when I run the following code

#include <stdio.h>
#include <math.h>

// Should be determined from datasheet
#define SD 0.1

float verticalSlope(int *size, float *velocity[*size],float *position[*size][3]) 
{
    if (*size<2)
    {
        return 0;
    }
    int teller=0;
    while (fabsf(*velocity[*size-1-teller])<SD)
    {
        teller=teller+1;
    }
    float horizontal=sqrtf(pow(*position[*size-1-teller][0]-*position[*size-2-teller][0],2)+pow(*position[*size-1-teller][1]-*position[*size-2-teller][1],2));
    float vertical=*position[*size-1-teller][2]-*position[*size-2-teller][2];
    float slope=vertical/horizontal;
    return slope;
}

int main()
{
printf("Hello, this is a little script that illustrates the working of the function verticalSlope. (0=default value) \n");
int lengte=3;
float snelheid[3]={1.0, 1.0, 1.0};
float positie[3][3]={{0.0, 0.0, 0.0},{0.0, 0.0, 0.0},{3.0, 4.0, 5.0}};
printf("The return for verticalSlope(3,{1 1 1},{{0 0 0},{0 0 0},{3 4 5}) is: %f \n", verticalSlope(&lengte,&snelheid,&positie));
int lengte2=2;
float snelheid2[3]={0.0, 0.0, 0.0};
float positie2[3][3]={{1.0, 2.0, 1.0},{1.0, 2.0, 1.0},{1.0, 2.0, 1.0}};
printf("The return for verticalSlope({0 0 0},{{1 2 1}{ 1 2 1}{1 2 1})) is: %f \n", verticalSlope(&lengte2,&snelheid2,&positie2));
return 0;
}

The error is in the main when I try to call the function verticalSlope. I have been thinking about the datatypes that I give in but my ideas about them do not match the error. Could somebody hint at me in the right direction?

ilkkachu
  • 6,221
  • 16
  • 30
  • 1
    Study pointers and arrays. `float *velocity[*size],float *position[*size][3]` is just gibberish. You can't get these things right with trial & error. – Lundin Mar 10 '21 at 13:08
  • Does this answer your question? [How to pass a 2D array by pointer in C?](https://stackoverflow.com/questions/16724368/how-to-pass-a-2d-array-by-pointer-in-c) –  Mar 10 '21 at 13:32
  • 2
    @Lundin - `float *velocity[*size],float *position[*size][3]` are valid VLA declarations, even though in the context of parameters' implicit array-to-pointer conversion the `[*size]` specification has only documentary value, so I don't think _gibberish_ is an appropriate term here. – Armali Mar 10 '21 at 13:33
  • `*size` is never modified in `verticalSlope`. Consider passing `size` by value and replace all `*size` with `size`. It would be easier to read – tstanisl Mar 10 '21 at 13:53
  • @Armali No it's gibberish, a correct definition with VLA in this case would be `(size_t size, float velocity[size],float position[size][3])`. Also look at how the variables in main() are declared, it's obvious that the OP is just wildly guessing. – Lundin Mar 10 '21 at 13:59
  • Well, it seems that our interpretations of the term _gibberish_ differ, but that's of course okay. (`int *size` is the correct parameter declaration for the given argument `&lengte`.) – Armali Mar 10 '21 at 14:02

1 Answers1

1

In float verticalSlope(int *size, float *velocity[*size],float *position[*size][3]), float *velocity[*size] declares velocity to be an array of pointers to float, and float *position[*size][3] declares position to be an array of arrays of pointers to float. You could fix this by changing *velocity and *position to (*velocity) ands (*position) everywhere they appear in the function definition, including the parameters.

However, a more conventional solution is to change the declaration to float verticalSlope(int *size, float velocity[*size], float position[*size][3]). This declares velocity to be an array of float, but it will be automatically adjusted to be a pointer to float. And it declares position to be an array of array of float, but it will be automatically adjusted to a pointer to an array of float.

When calling the function, pass snelheid instead of &snelheid and positie instead of &positie and similarly for snehlheid2 ands positie2.

In the body of the function, remove the * from before each use of velocity and position.

(You can pass an array by its address, but C’s semantics for pointers and subscripts make it unnecessary because a pointer to the first element of an array suffices for most purposes.)

Eric Postpischil
  • 195,579
  • 13
  • 168
  • 312