Function that returns vector

1

How to return a vector inside a function? And what do I call it in the main?

This return in case: (return vector;)

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

float UK (int mat[4][4], float w[3]){
float *vetor[4];
float res = 0;

    for(int l=0; l<4; l++){
      //printf("\n");
       float soma = 0;
        for(int c=0; c<3; c++){
          //printf("%tfd ", mat[l][c]);

          res = mat[l][c] * w[c];
          printf("Resultado: %f\n", res);

          soma = soma + res;

         }

         *vetor[l] = soma;
         printf("Vetor: %f\n", vetor[l]);
     }

    return vetor;
}

float *Limiar(float vet[]){

for(int c=0; c<4; c++){
    if(vet[c] >= 0)
    vet[c] == 1.0;
    else
    vet[c] == 0.0;
}
printf("Yl: %d", vet);
return vet;
}

float Delta(){

}


int main () {
int mat[4][4];
float w[3];

mat[0][0] = -1;
mat[0][1] = 0;
mat[0][2] = 0;
mat[0][3] = 0;
mat[1][0] = -1;
mat[1][1] = 0;
mat[1][2] = 1;
mat[1][3] = 0;
mat[2][0] = -1;
mat[2][1] = 1;
mat[2][2] = 0;
mat[2][3] = 0;
mat[3][0] = -1;
mat[3][1] = 1;
mat[3][2] = 1;
mat[3][3] = 1;

w[0] = 0.2;
w[1] = 0.2;
w[2] = 0.2;



Limiar(UK(mat, w));
return 0;
}
    
asked by anonymous 22.04.2018 / 00:19

2 answers

0

I've put an example of how you can manipulate your vector in the function I hope to help.

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

void alterarVetor(float vetor[], int tamanho){
    float *ponteiro;
    float *ultimoVetor = vetor+tamanho;

     for (ponteiro = vetor; ponteiro < ultimoVetor; ponteiro++) {
     *ponteiro = 6;
   }

 }

 int main(int argc, char const *argv[]) {
 float vetorOrigem[4]={0.2,0.1,0.0,0.1};

  alterarVetor(vetorOrigem, 4);


  for (int i = 0; i < 4; i++) {
  printf("Vetor: %0.1f ", vetorOrigem[i]);
 }
 return 0;
 }
    
22.04.2018 / 21:40
1

Problem

Let's start by noticing the problem:

float UK (int mat[4][4], float w[3]) {
    float *vetor[4];
    ... //instruções que não são relevantes para perceber o problema
    return vetor;
}

Here you have several problems, just looking at these lines of code.

  • The type of vetor is float** because it is a vector of pointers to float . So the return type of the function should be float** :

    float** UK (int mat[4][4], float w[3]) {
    //---^
    
  • If you created a static array inside the function:

    float *vetor[4];
    

    You can not return it and use it outside of it.

      

    But why?

    This array was allocated in the stack, in the space that was assigned to the function and all its variables. For this reason when the function ends this space is marked as free, and the values that are there are eliminated at any time. This implies that any access to one of these pointers will generate undefined behavior and sooner or later a Segmentation Fault .

  • Solutions

  • Allocate the array in the heap through malloc :

    float ** vetor = malloc(sizeof(float*) * 4);
    

    Be careful with this approach. Although it is useful and necessary in several cases, it has more implications than it seems. Not only does it give you the responsibility of having to free up allocated memory, when you no longer need it by calling free , as it will potentially further fragment your memory.

  • Pass the array already allocated to the results. In this scenario the function receives as a parameter the array to be processed and the one where the result is placed:

    void UK (int mat[4][4], float w[3], float *vetor[4]) {
        //              array de resultado aqui ----^
        //sem retorno
    }
    

    In this scenario the call of main , considering only this function in isolation would be like this:

    float *vetor[4];
    UK(mat, w, vetor);
    

    This solution makes memory management easier, but on the other hand forces callers to have to size the results deterministically.

  • 22.04.2018 / 04:36