动态内存分配以反向打印浮点数数组
Dynamic Memory Allocation to Reverse Print Array of Floating Point Numbers
所以我是 C 的新手,正在研究数组的内存分配。我正在尝试创建一个程序,该程序将使用 malloc 动态分配 space 来反转浮点数数组。
#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]);
}
}
编译过程中出现以下错误:
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(){
^
您的指针 x 是存储在数组中的结构的一部分。您可能想通过该结构访问您的 "x"。所以而不是
x = malloc(y * sizeof(struct));
你可能想要
a[some index].x = malloc(y * sizeof(struct));
上面这行代码可以编译,但很可能会给您不正确的结果。既然你想分配它,你希望它是你计划存储在那里的变量的大小,而不是结构的大小。
我应该提到还有其他问题。您不能以这种方式遍历结构。您想改为遍历数组(结构)的长度。
您的代码有很多问题。我建议你在尝试这样做之前多练习 C 基础知识。以下是您可能希望通过代码实现的近似值:
#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;
}
所以我是 C 的新手,正在研究数组的内存分配。我正在尝试创建一个程序,该程序将使用 malloc 动态分配 space 来反转浮点数数组。
#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]);
}
}
编译过程中出现以下错误:
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(){
^
您的指针 x 是存储在数组中的结构的一部分。您可能想通过该结构访问您的 "x"。所以而不是
x = malloc(y * sizeof(struct));
你可能想要
a[some index].x = malloc(y * sizeof(struct));
上面这行代码可以编译,但很可能会给您不正确的结果。既然你想分配它,你希望它是你计划存储在那里的变量的大小,而不是结构的大小。
我应该提到还有其他问题。您不能以这种方式遍历结构。您想改为遍历数组(结构)的长度。
您的代码有很多问题。我建议你在尝试这样做之前多练习 C 基础知识。以下是您可能希望通过代码实现的近似值:
#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;
}