Function that returns vector

Asked

Viewed 17,219 times

1

How to return a vector within a function? And how do I call it in main?

This Return in the case: (vector Return;)

#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;
}
  • 3

    You cannot return a statically created vector within a function. Using this pointer outside the function represents undefined behavior

  • I didn’t understand very well, could exemplify?

  • vector will cease to exist when the function is closed, as a common local variable

  • Like I call it in the main?

  • 1

    Could use a reference maybe

  • or use float *vector = (float*)malloc(sizeof(float)*3), for example to create a pointer with 3 positions

Show 1 more comment

2 answers

1

Problem

Let’s start by understanding 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 are already several problems, just looking at these lines of code.

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

    float** UK (int mat[4][4], float w[3]) {
    //---^
    
  2. A static array has been created within the function:

    float *vetor[4];
    

    Can’t return it and use it outside of it.

    But why ?

    This array was allocated in the stack, 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 can be 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

  1. Allocate the array to the heap via malloc:

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

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

  2. Pass the array already allocated to the results. In this scenario the function receives as 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 so:

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

    This solution makes memory management easier, but in contrast forces those who call to have to define the size of the results deterministically.

  • I did as it is in the explanation, but this problem appeared: [Error] declaration of 'float** vector

  • @Heloisadiniz It’s because you didn’t apply it correctly. I suspect you’re applying the version with malloc. If that’s the case the instruction I gave replaces the old one that was float *vetor[4];, assuming it also corrects the type of return

  • I couldn’t get...

0


I’ve put together 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;
 }
  • Thank you so much, now I got :D

Browser other questions tagged

You are not signed in. Login or sign up in order to post.