prm
prm

Reputation: 25

Function using arrays as arguments in C

Just started learning pointers in C and I was faced with the following code:

#include <stddef.h>
#include <stdlib.h>

double *vec( double a[], double b[]);
int main(){
double v1[3]={1.0,1.0,1.0};
double v2[3]={1.0,-1.0,1.0};
double *v3 = NULL;
v3 = vec(v1,v2);

printf("v1 X v2 = (%f, %f, %f)\n",v3[0],v3[1],v3[2]);
free( v3);
return 0;
}
double *vec( double a[], double b[]){

    double *c=NULL;
    c = (double *)malloc(3*sizeof( double ));
    c[0]=  a[1]*b[2]-a[2]*b[1];
    c[1]=-(a[0]*b[2]-a[2]*b[0]);
    c[2]=  a[0]*b[1]-a[1]*b[0];
    return c;
 }

The question here is, when declaring the function the author used *function(parameters) instead of function(parameters). Why did he/she used a pointer in to declare the function vec?

Upvotes: 0

Views: 87

Answers (3)

Suman Tiwari
Suman Tiwari

Reputation: 40

Function is defined as

Return_type Name_of_function(argument_list)

Return_type can be int,float,double,int*(indicate return type is integer pointer ) , float*,char* etc.

here return type of function is double* means function will return a value of type double pointer (C is declared as double* as it is variable which is returned from function )

Upvotes: 0

SourceOverflow
SourceOverflow

Reputation: 2048

You read the syntax wrong. It is not *function it is a function returning double * aka a pointer to a double.

In C when you need dynamically allocated arrays you do this using pointers. Also C doesn't allow returning arrays from a function directly and therefore you have to return a pointer to its memory. (Watch out as local variables will get disposed)

Upvotes: 2

Kevmeister68
Kevmeister68

Reputation: 226

To answer your specific question: You cannot return a "true" array in C. You can return a pointer to the beginning of the array, however.

To be honest, the code you are facing/studying is not the best. I could understand if vec( ) returned a malloc'ed pointer because it didn't know how big the input arrays were going to be. But the [partial] give-away is that vec( ) receives no parameter describing how many elements are in the array (an array parameter by itself cannot tell you this). (I say partial because the array might have been self-terminating via eg. a 0 value, just like C-style strings use).

But the implementation of vec( ) shows that it expects 3-element arrays, so returning a malloc'ed pointer is a little unnecessary. It would be much faster (and safer) for vec to have a third argument (array) passed in to receive the result. That way there is no dynamic memory allocation or freeing, and no opportunity to forget to free.

There is still the possibility, however, of the arrays being the incorrect size. You could workaround this by having a struct that holds a 3-element array of double, and passing in the structs (via pointers) instead, eg:

struct DoubleArray3
{
   double values[ 3 ];
}

vec( const DoubleArray3* v1, const DoubleArray3* v2, DoubleArray3* result );

Upvotes: 1

Related Questions