0

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?

1
  • The function vec is a function returning a pointer to double. 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 favor double* vec(double a[], double b[]); to emphasize that the pointer is associated with the return value. Commented Nov 11, 2017 at 0:41

3 Answers 3

2

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)

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

1 Comment

@DavidBowling You're right, that statement was a gross generalization. I think I have clarified it a bit, but feel free to expand/ clarify/ point out problems.
1

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

1 Comment

"an array parameter by itself cannot tell you this" -- for an array such as v1[], you can use sizeof v1 / sizeof *v1, but this will not work on the pointer a within the function. Not a bad idea to use the struct, but you still have to assign the array values or initialize them before passing to the function to avoid UB; since this must be done anyway, I'm not sure that the struct is much of a gain. It does increase complexity.
0

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 )

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.