1

So I'm brand new to C and playing around with memory allocation for arrays. I'm trying to create a program that will dynamically allocate space using malloc to reverse an 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 the number of floating point numbers: ");
    scanf("%d", &y);
    x = malloc(y * sizeof(struct));

    printf("Enter 5 floating point numbers: \n");
    for(i = 0; i < sizeof(struct); i++){
    scanf("%.3f", &x[i]);
    }

    printf("The numbers in reverse order are: \n");
    for(i = --sizeof(struct); i >= 0; i--){
    printf("%f \n", a[i]);
    }
}

During compilation, the following errors are generated:

error: use of undeclared identifier 'x'
*x = malloc(y * sizeof(struct);
^

test.c:14:25: error: declaration of anonymous struct must be 
a definition
*x = malloc(y * sizeof(struct);
                       ^

test.c:14:32: error: type name requires a specifier or qualifier
*x = malloc(y * sizeof(struct);
                              ^

test.c:14:31: error: type name requires a specifier or qualifier
x = malloc(y * sizeof(struct));
                             ^

test.c:14:24: note: to match this '('
*x = malloc(y * sizeof(struct);
                   ^

test.c:25:3: error: expected '}'
}       
        ^

test.c:9:11: note: to match this '{'
int main(){
      ^
1
  • x is a field of a structure, but you are trying to access it as a stand-alone variable. Commented May 14, 2015 at 18:57

2 Answers 2

1

Your pointer x is part of the structure which is stored in an array. You probably want to access your "x" through the structure. So instead of

x = malloc(y * sizeof(struct));

You probalby want

a[some index].x = malloc(y * sizeof(struct));

This above line will compile but will most likely give you incorrect results. Since you want to allocate it, you want it to be the size of the variable that you are planning to store there, not the size of the struct.

I should mention that there are other problems. You can't iterate through a structure that way. You want to instead iterate over the length of the array (of structs) instead.

Sign up to request clarification or add additional context in comments.

Comments

0

There are a lot of issues with your code. I would advise you to practice more with C basics before attempting to do this. Here is approximation of what you might have wanted to achieve with your code:

#include <stdio.h>
#include <string.h>

// This structure can hold array of floats - and their size
struct Rec
{
     float * x;
     int size;
};

int main()
{
 // Declare variable of type rec
 struct Rec a;  
 int i, y;  

 // How many floats to store? This could also be stored in a.size instead of y
 printf("Enter the number of floating point numbers: ");        
 scanf("%d", &y);  

 // Create and populate dynamic array
 a.x = malloc(y * sizeof(float));  
 printf("Enter floating point numbers: \n");
 for(i = 0; i < y; i++)
 {
   scanf("%.3f", &a.x[i]);       
 }

 // Print
 printf("The numbers in reverse order are: \n");
 for(i = y-1; i >= 0; i--)
 {
     printf("%f \n", a.x[i]);
 }

  free(a.x);
  return 0;
 }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.