C用户输入数组的大小 [英] C User input size of Array
问题描述
我正在尝试编写一个程序,该程序可以根据用户输入创建具有大小的数组,然后将结构存储在其中. 该结构将包含两个int和两个float.
I am trying to write a program that can create an array with size based on the user input and then store structs inside. The struct will contain two ints and two floats.
我的主要问题是,如何根据用户输入创建具有大小的数组? 这是我到目前为止的内容:
My main problem is, how do I create an array with size based on the user input? This is what I have so far:
struct inventoryItem
{
int itemNumber;
float cost;
float retailPrice;
int itemsInStock;
}
int main()
{
printf("Enter the number of slots needed in the array: ");
int size;
scanf("%d", &size);
struct inventoryItem inventory[size]; //problem here
}
对于使用C语言进行编程,我是一个新手,因此不需太复杂的解决方案.
I am fairly new to programming in C so a solution that is not too complex would be appreciated.
因此,既然我已经解决了第一部分的问题,我现在正尝试创建第二个数组,该数组包含指向第一个数组的数据(索引)的指针. 现在的问题是我不知道如何创建一个for循环,该循环可以将指针指向第一个数组并将其存储到第二个数组中. 我将indexArray声明为"int"类型,不确定是否正确.
So now that I have the first part solved, I am now trying to create a second array that holds pointers to the first array's data (an index). Now the problem is I do not know how to create a for loop that can take the pointers to the first array and store them into the second. I declared the indexArray as type 'int' and am not sure if that is right.
这是我到目前为止所拥有的:
This is what I have so far:
struct inventoryItem
{
int itemNumber;
int itemsInStock;
float cost;
float retailPrice;
};
int main()
{
printf("Enter the number of slots needed in the array: ");
int size;
scanf("%d", &size);
//array of items
struct inventoryItem *inventory; //use pointer to item
inventory =(struct inventoryItem *) malloc(sizeof(struct inventoryItem)*size); //create array to store inventoryItem with size 'size'
//array of index
int *indexArray = (int*) malloc(sizeof(int)*size); //not sure if this is right
//fill array contents
for(int i = 0; i < size; i++)
{
printf("Enter item %d number: ", i);
scanf("%d", &inventory[i].itemNumber);
printf("Enter item %d stock: ", i);
scanf("%d", &inventory[i].itemsInStock);
printf("Enter item %d cost: ", i);
scanf("%f", &inventory[i].cost);
printf("Enter item %d price: ", i);
scanf("%f", &inventory[i].retailPrice);
}
for(int i = 0; i < size; i++)
{
printf("Item %d number: %d\n", i, inventory[i].itemNumber);
printf("Item %d stock: %d\n", i, inventory[i].itemsInStock);
printf("Item %d cost: %f\n", i, inventory[i].cost);
printf("Item %d retail price: %f\n", i, inventory[i].retailPrice);
}
//stuck here
//struct inventoryItem *header = inventory; //error here
for(int i = 0; i < size; i++)
{
//indexArray[i] = inventory[i];
}
}
推荐答案
您应该使用malloc动态分配数组的大小
You should use malloc to dynamically allocate the size of array
#include <stdlib.h>
int main()
{
struct inventoryItem *inventory; //use pointer to item
printf("Enter the number of slots needed in the array: ");
int size;
scanf("%d", &size);
//after you get the size input
inventory = malloc(sizeof(struct inventoryItem)*size);
}
最后,您应该使用free
释放内存
In the end you should use the free
to free the memory
这篇关于C用户输入数组的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!