Returning an Array from a Function in C

AndroidDev

I'm just making a mess out of this. I have a function that is supposed to take a one-dimensional array, do some calculations with its values, and then return a similar array with the results of the calculation. I don't necessarily care whether it returns the same array (with new values) or if it creates a new array at a different memory location and returns that. Here is what I've got at the moment. There are errors all over this, but I don't know what I am doing wrong. Can anyone help?

double s  = 10;
double b  = 2.6666;
double r  = 28;

double (*newVertex(double vtx[3] )) [] {

    static double newVtx[3];
    /*  Coordinates  */
    double x = vtx[0];
    double y = vtx[1];
    double z = vtx[2];

    double dt = 0.001;

    double dx = s*(y-x);
    double dy = x*(r-z)-y;
    double dz = x*y - b*z;
    newVtx[0] = x + dt*dx;
    newVtx[1] = y + dt*dy;
    newVtx[2] = z + dt*dz;

    return &newVtx;
}

int main(int argc, char *argv[]) {
    int i;

    /* Arrays to hold the coordinates */
    double thisPt[3] = {1, 1, 1};
    double nextPt[3];

    for (i=0;i<1000;i++) {
        printf("%5d %8.3f %8.3f %8.3f\n", i, thisPt[0], thisPt[1], thisPt[2]);
        nextPt = newVertex(&thisPt);
        thisPt = nextPt;
    }
    return 0;
} 
mpenkov

First of all, your function declaration looks unnecessarily complex to me.

If you're not planning to create a new array, then it should be something like:

void function_name(double *parameter) {
    // code to change the parameter in place here    
}

or, if you want to be explicit about the length of the array (see comments for additional info):

#define ARRAY_SIZE 3
void function_name(double parameter[ARRAY_SIZE]) {
    // code to change the parameter in place here    
}

If you're planning to create a new array, then you could do something like:

double * function_name(double *parameter) {
    double *result = (double *)malloc(sizeof(double * number_of_elements));
    // read parameter, write into result
    return result;
}

The above snippet assumes the number_of_elements is fixed and known. If it is not, then you need to handle them as additional arguments.

Next, this is bad for several reasons:

double (*newVertex(double vtx[3] )) [] {    
    static double newVtx[3];
    // update newVtx    
    return &newVtx;
}

The return statement returns the address of a local variable. In this particular case, the variable is static, so the variable won't be overwritten once the function exits. But does it really need to be static in the first place? And is it sufficient for it to be static? Think about code like this:

double *v1 = newVertex(old_vertex);
double *v2 = newVertex(old_vertex);

You may be tempted to think you can handle the two vertices individually, but they're pointing to the exact same spot in memory: the location of the static variable. It's much more common practice to allocate space for the array dynamically (malloc, calloc) and return a pointer to the allocated memory.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Returning Array from function in C

From Dev

Returning Array from C++ Function

From Dev

returning array from function in c++

From Dev

Returning an item from an array in a C function

From Dev

Returning integer array values from a function in c

From Dev

Returning an array element from an array created within the function in C?

From Dev

Preferred way of returning a char array from a function in C

From Dev

Returning an array address from a function to a pointer (C++)

From Dev

Returning a char array from C++ function to tcl

From Dev

Returning an array address from a function to a pointer (C++)

From Dev

Returning a 2D array from c function

From Dev

Returning a mutable array from a function

From Dev

Returning an array from a function in VBA

From Dev

Returning a mutable array from a function

From Dev

Returning an array from function in nodejs

From Dev

Returning char array in C function

From Dev

C - Returning array as function argument

From Dev

Returning string from C function

From Dev

Returning malloc from a function in C

From Dev

function returning pointer vs function returning array in C

From Dev

Returning foreach value from function to array

From Dev

Returning array address from function did not work

From Dev

Returning a populated array from a function that uses SwiftyJSON

From Dev

returning array of string from function not working as expected

From Dev

returning array from function and assigning it to variable

From Dev

Type mismatch when returning an array from a function

From Dev

Issue with returning an array of type int from function

From Dev

Returning an array from a higher order function

From Dev

C Function returning Array and pointer without a cast

Related Related

  1. 1

    Returning Array from function in C

  2. 2

    Returning Array from C++ Function

  3. 3

    returning array from function in c++

  4. 4

    Returning an item from an array in a C function

  5. 5

    Returning integer array values from a function in c

  6. 6

    Returning an array element from an array created within the function in C?

  7. 7

    Preferred way of returning a char array from a function in C

  8. 8

    Returning an array address from a function to a pointer (C++)

  9. 9

    Returning a char array from C++ function to tcl

  10. 10

    Returning an array address from a function to a pointer (C++)

  11. 11

    Returning a 2D array from c function

  12. 12

    Returning a mutable array from a function

  13. 13

    Returning an array from a function in VBA

  14. 14

    Returning a mutable array from a function

  15. 15

    Returning an array from function in nodejs

  16. 16

    Returning char array in C function

  17. 17

    C - Returning array as function argument

  18. 18

    Returning string from C function

  19. 19

    Returning malloc from a function in C

  20. 20

    function returning pointer vs function returning array in C

  21. 21

    Returning foreach value from function to array

  22. 22

    Returning array address from function did not work

  23. 23

    Returning a populated array from a function that uses SwiftyJSON

  24. 24

    returning array of string from function not working as expected

  25. 25

    returning array from function and assigning it to variable

  26. 26

    Type mismatch when returning an array from a function

  27. 27

    Issue with returning an array of type int from function

  28. 28

    Returning an array from a higher order function

  29. 29

    C Function returning Array and pointer without a cast

HotTag

Archive