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;
}
You cannot return a statically created vector within a function. Using this pointer outside the function represents undefined behavior
– Isac
I didn’t understand very well, could exemplify?
– Heloisa Diniz
vector will cease to exist when the function is closed, as a common local variable
– Sveen
Like I call it in the main?
– Heloisa Diniz
Could use a reference maybe
– Maurício Z.B
or use float *vector = (float*)malloc(sizeof(float)*3), for example to create a pointer with 3 positions
– Sveen