Vectors, pointers and memory storage

Asked

Viewed 199 times

-5

I need to solve an exercise where I need to use pointers, vectors and memory storage:

Create a vector with n elements, and each vector position will match a pointer to a value of type float. Make the read n values and store them in memory.

I believe it is not very complex, but I am still studying the subject and it would be good to have this exercise solved to see how things work.

1 answer

1


From what I understand it’s this.

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

    int main () {
       int  n, i;
       float *v; // Declaração do vetor
       scanf ("%d", &n); 
       v = malloc (n * sizeof (float)); // Alocação do vetor em n elementos
       for (i = 0; i < n; i++) scanf ("%f", &v[i]); // Leitura e armazenamento dos valores na memoria
       for (i = 0; i < n; i++) printf ("%f ", v[i]); // Teste
       return 0;
    }
  • Thanks for helping me out, man! I was in doubt about the use of malloc and how he would like you to do the pointer, because after so many slides explaining, I still missed catching the ~essence~ of the business. With this example things seem clearer. I have 24 more exercises to continue now, thank you!

Browser other questions tagged

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