-2

I know this question has been asked before but cannot find it in the same manner as I will describe it here:

Its all about returning an one-dimensional array in c-language. In java its very easy:

 double[] myFunction() {

   double[] v = new double[10];

   return v;
 }

I know that the implementation in c is not the same. But as an array element can be considered as a pointer to the first element in that array I thought one could do the following implementation:

 double (*myFunction()) {

    double v[10];

    return v;
 }

This compiles fine in gcc but when I make a call of the function I get a compilation error.

SO my Question - how does one return a one-dimensional vector in c-language?

Thanks

  • 5
    Your `v` array will be deallocated as soon as the function returns and the pointer will be garbage. This question has already been asked before [here](http://stackoverflow.com/q/11656532/1576996) – Harry Dec 25 '13 at 18:59
  • possible duplicate of [Declaring a C function to return an array](http://stackoverflow.com/questions/1453410/declaring-a-c-function-to-return-an-array) – OldProgrammer Dec 25 '13 at 19:00
  • 1
    "This compiles fine in gcc but when I make a call of the function I get a compilation error." - You either do XOR do not get a compilation error, this doesn't make sense. (In this case, you probably do get an error.) And this has been asked many, many times. Not to mention the almost-blasphemic beginner tutorial, which you should have read. –  Dec 25 '13 at 19:13

4 Answers4

3

sample code

#include <stdio.h>
#include <stdlib.h>

double *myFunction1(){
    return malloc(10*sizeof(double));
}

double (*myFunction2())[10]{
    double (*p)[10];
    p = malloc(sizeof(double[10]));
    return p;
}

typedef struct d10 {
    double vec[10];
} D10;

D10 myFunction3(){//Make little sense
    D10 v = {{0}};
    v.vec[9]=1.25;

    return v;
}

int main(){
    double *v1 = myFunction1();
    double (*v2)[10] = myFunction2();
    D10 v3= myFunction3();
    //do something
    printf("%lf\n", v3.vec[9]);
    v1[0] = 3.14;
    (*v2)[0] = 3.14 * 2;
    free(v1);
    free(v2);
    return 0;
}
BLUEPIXY
  • 39,699
  • 7
  • 33
  • 70
  • 1
    Great code, but some explanation would be nice, pointer to an array (`double (*v2)[10]`) is usually uncommon to a newbie. – this Dec 25 '13 at 19:15
  • Only a thing like this. pointer to double[10]. it think when you dereference. – BLUEPIXY Dec 25 '13 at 19:20
  • This guy is getting downvote just because of his poor English, not able to explain his answer well :(. By the way +1 for such a nice code. – haccks Dec 25 '13 at 20:45
  • Oh thank you dear. It is a short sample, so that does not require a description almost. – BLUEPIXY Dec 25 '13 at 20:59
3

Corrected:

You can't pass an array and you can't return an array you only pass an address to it or return a pointer to it first element:

double *someFunction()
{
    double *output;
    p = (double *)malloc(size*sizeof(double)); // size should be defined
    //... do some process with the content pointed to by output
    return output; // return the address pointed to by output
}

Or pass a pointer to it first element and do some process on the content pointed to.

void someFunction(double *output, int size)
{
    //... do some process with the content pointed to by output
}
rullof
  • 7,124
  • 6
  • 27
  • 36
2

For one, declaring v in the function makes the array live only in that function. Once the function returns, that array is popped off the stack, and is likely to be modified after successive function calls.

The proper C solution is to use malloc to allocate an amount of space that you can use as an array, and return the pointer to that array.

double * myFunc(){
    double * v = malloc(10*size of(double));
    return v;
}

In Java, it was safe to return an array because the garbage collector could detect that the array was still in use, after it was popped off the stack.

millinon
  • 1,528
  • 1
  • 20
  • 31
  • @rullof - thanks. cannot uppvote due to my low reputation. ... but if I have declared a new variable of type double in the main-function and assign the return-value to it -shouldn't that be ok?` I understand that the array is removed from the stack but that occurs after that double array in the main-function have received the array. Or? – user3135077 Dec 26 '13 at 01:48
  • Since the type of myFunc is a `double *`, the function doesn't return an array, but a pointer to a double. The fact that the type of a variable is a `double *` doesn't guarantee that it actually points to a double - it could be invalid memory, NULL, a single double, an array of doubles, or who knows what. In this case, `malloc` allocates a block of memory large enough to hold 10 doubles. If you return a pointer to an array that lives on the stack in myFunc(), there's no way for the compiler to know not to pop the array off the stack - all it knows is to return a pointer. – millinon Dec 26 '13 at 19:44
0

how does one return a one-dimensional vector in c-language?

Technically speaking, one doesn't. C functions cannot return array types.

The main problem you are running into is that, in the C code, v only exists for the lifetime of the function; once the function exits, v no longer exists, so any pointer you return will not be valid.

For this to work, you'll have to do something similar to what the Java code is doing; you'll need to allocate the memory for the array from the heap (i.e., allocate it dynamically):

double *myfunction() 
{
  double *v = malloc( sizeof *v * 10 );
  return v;
}

Note that C does not clean up after you, so you'll have to explicitly free that memory when you're done with it, such as:

int main( void )
{
  double *arr = myfunction();
  /**
   * do stuff with arr
   */
  free( arr );
  return 0;
}
John Bode
  • 119,563
  • 19
  • 122
  • 198