c - Dynamic Memory Allocation to Reverse Print Array of Floating Point Numbers -


so i'm brand new c , playing around memory allocation arrays. i'm trying create program dynamically allocate space using malloc reverse array of floating point numbers.

    #include <stdio.h>      #include <stdlib.h>       struct rec {          float * x;          int size;     };      int main(){     struct rec a[50];      int i, y;       printf("enter number of floating point numbers: ");     scanf("%d", &y);     x = malloc(y * sizeof(struct));      printf("enter 5 floating point numbers: \n");     for(i = 0; < sizeof(struct); i++){     scanf("%.3f", &x[i]);     }      printf("the numbers in reverse order are: \n");     for(i = --sizeof(struct); >= 0; i--){     printf("%f \n", a[i]);     } } 

during compilation, following errors generated:

error: use of undeclared identifier 'x' *x = malloc(y * sizeof(struct); ^  test.c:14:25: error: declaration of anonymous struct must  definition *x = malloc(y * sizeof(struct);                        ^  test.c:14:32: error: type name requires specifier or qualifier *x = malloc(y * sizeof(struct);                               ^  test.c:14:31: error: type name requires specifier or qualifier x = malloc(y * sizeof(struct));                              ^  test.c:14:24: note: match '(' *x = malloc(y * sizeof(struct);                    ^  test.c:25:3: error: expected '}' }                ^  test.c:9:11: note: match '{' int main(){       ^ 

there lot of issues code. advise practice more c basics before attempting this. here approximation of might have wanted achieve code:

#include <stdio.h> #include <string.h>  // structure can hold array of floats - , size struct rec {      float * x;      int size; };  int main() {  // declare variable of type rec  struct rec a;    int i, y;     // how many floats store? stored in a.size instead of y  printf("enter number of floating point numbers: ");          scanf("%d", &y);     // create , populate dynamic array  a.x = malloc(y * sizeof(float));    printf("enter floating point numbers: \n");  for(i = 0; < y; i++)  {    scanf("%.3f", &a.x[i]);         }   // print  printf("the numbers in reverse order are: \n");  for(i = y-1; >= 0; i--)  {      printf("%f \n", a.x[i]);  }    free(a.x);   return 0;  } 

Comments

Popular posts from this blog

c++ - Difference between pre and post decrement in recursive function argument -

php - Nothing but 'run(); ' when browsing to my local project, how do I fix this? -

php - How can I echo out this array? -