I want to move from a matrix to a dynamic vector through a function. Then to verify that everything goes well, I want to print the vector elements.

Function:

    float **v;
    float ** matrix_to_vector(const float m[][2],size_t n){
    v = (float **)malloc(n * sizeof(float*));
        for(i=0;i<n;i++){
        v[i] = (float *)malloc(n * sizeof(float)); 
    for(j=0;j<MAX_COLS;j++){
        *(v[i]) = m[i][j];
    }
}
return v;
}

On main():
float ** vector;
vector = matriz_a_vector(m,cantidad);

for(i=0;i<cantidad;i++){
    for(j=0;j<MAX_COLS;j++){
        printf("%f\t",*(vector[i]));
    }
    printf("\n");
}

My problem is that I always keep the M [ I][ 1]element, since when the cycle is completed, it always holds value in v [ I]. When i print the vector only prints me the values of M [ I] [ 1] because it is the ones that keep the variable. How can I get to access all the elements and get them all printed out?

1 Answers

0
Arturo Aguilar Lopez On Best Solutions

Solved. Once memory was requested for v [i] in the first for, then it copied the Matrix values in the vector created. Then I did a function to print that Vector apart.

for(i=0;i<n;i++){
        for(j=0;j<MAX_COLS;j++){
            v[i][j] = m[i][j];  
        }
    }