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?
vecis a function returning a pointer todouble. I find it more clear to add spaces on both sides of the asterisk in such function definitions:double * vec(double a[], double b[]);Some, especially C++ programmers, seem to favordouble* vec(double a[], double b[]);to emphasize that the pointer is associated with the return value.