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

double * aloca(int n){
    double *v;   
    v = malloc(n * sizeof(double)); // alocação da região de memória

    if (v == NULL){ // verifica se a memória acabou
        printf("Memória insuficiente\n");
        exit(1);
    }
    
    for(int i = 0; i < n; i++){
        v[i] = 0.0;
    }
    return v;
}

int main() {
    int n = 5; // tam vetor
    double * v = aloca(n); // alocação do vetor de tamanho n

    for(int i = 0; i < n; i++){
        printf("v[%d] = %f\n", i, v[i]); // imprime os elementos do vetor
    }

    free(v); // libera memória alocada
    
    return 0;
}

Embed on website

To embed this project on your website, copy the following code and paste it into your website's HTML: