C 如何允许用户输入没有初始大小的数组?
我一直在阅读有关如何在事先不知道数组大小的情况下声明数组的内容。据我所知,这样做的方法是为用户输入的数组分配一些内存,然后根据需要重新分配或释放。对于一组字符,我是这样做的:C 如何允许用户输入没有初始大小的数组?,c,arrays,C,Arrays,我一直在阅读有关如何在事先不知道数组大小的情况下声明数组的内容。据我所知,这样做的方法是为用户输入的数组分配一些内存,然后根据需要重新分配或释放。对于一组字符,我是这样做的: #include <stdio.h> #include <stdlib.h> #include <string.h> int main() { //allocation of memory char *a = malloc(200 * sizeof(char));
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
//allocation of memory
char *a = malloc(200 * sizeof(char));
//allow user input
fgets(a, 200, stdin);
int i = 0;
//find the length of the array
int lstring = strlen(a);
printf("%d", lstring-1);
free(a);
return 0;
}
#包括
#包括
#包括
int main()
{
//内存分配
char*a=malloc(200*sizeof(char));
//允许用户输入
fgets(a,200,标准DIN);
int i=0;
//查找数组的长度
int lstring=strlen(a);
printf(“%d”,lstring-1);
免费(a);
返回0;
}
在这里,用户不输入字符数组的实际大小,但可以通过查看字符串末尾的位置来知道
另一方面,对于整数数组,我有:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
//allocation of memory
int *b = malloc(200 * sizeof(int));
b[0] = 2;
b[1] = 76;
b[2] = 123;
printf("%d", b[0]);
free(b);
return 0;
}
#包括
#包括
#包括
int main()
{
//内存分配
int*b=malloc(200*sizeof(int));
b[0]=2;
b[1]=76;
b[2]=123;
printf(“%d”,b[0]);
免费(b);
返回0;
}
我的问题是,如何允许用户输入整数
我想到的第一件事是使用scanf()
为创建一个循环,但除非允许用户首先输入数组的大小,否则这是行不通的。有没有一种方法(与char数组中注意到的方法相同或不同)可以在不让用户显式输入大小的情况下执行此操作
我想到的第一件事是使用scanf()
为创建一个循环,但除非允许用户首先输入数组的大小,否则这是行不通的。有没有一种方法(与char数组中注意到的方法相同或不同)可以在不让用户显式输入大小的情况下执行此操作
你在正确的轨道上使用一个特殊的/标志值,以了解用户输入的结束。例如,这里我使用-1
表示用户输入的结束
//allocating memory
int *b = malloc(200 * sizeof *b);
//notify the user with a message:
printf("enter -1 to stop entry\n");
//loop to scan user entries
for(int i = 0; i < 200; i++){
//scanning user's input
scanf("%d", &b[i]);
//break when user enters -1
if(b[i] == -1){
break;
}
}
如果用户输入的整数数超过预期值怎么办?
您可以使用realloc
并在每次数组达到最大值时将其大小加倍
例如:
int length = 0; //variable to know length of array
//printing all the values of int array and knowing its length:
for(int i = 0; b[i] != -1; i++){
//for printing array elements
printf("%d ", b[i]);
//to know the length
length++;
}
int max_entry = 100; //instead you can initialise max_entry by taking user's input
//allocating memory
int *b = malloc(max_entry * sizeof *b);
//notify the user with a message:
printf("enter -1 to stop entry\n");
//loop to scan user entries
for(int i = 0; ; i++){
scanf("%d", &b[i]);
if( i == max_entry - 1 && b[i] != -1){
//increasing max entry
max_entry *= 2;
//reallocating to increase size of array
int *temp = NULL;
temp = realloc(b, max_entry * sizeof *b);
if(temp == NULL){
//allocation failed, handle it
}
else{
b = temp;
}
}
//break when user enters -1
if(b[i] == -1){
break;
}
}
是的,您可以为此使用realloc()
。一种常见的方法是创建一个可以扩展的结构,它将表示您的数组。结构可以如下所示:
typedef struct
{
int allocated; // Here you can save how many elements you have allocated space for
int used; // Here is how many elements are in the array
int * array; // here is pointer to the array
} Array;
然后,您应该创建一个函数,该函数将初始化此数组结构:
void initArray(Array * a)
{
a->allocated = INIT_SIZE;
a->used = 0;
a->array = malloc(INIT_SIZE * sizeof(int));
}
现在数组已经初始化,您已经为INIT\u SIZE
元素分配了内存。现在是最重要的部分-实际添加元素。您还可以为此创建一个函数:
bool insertEl(Array * a, int el)
{
if(a->used == a->allocated) //If there is no more space, then we need to realloc
{
int * temp = realloc(a->array, 2 * a->allocated * sizeof(int));
if(!temp) // Just check if realloc succeeded
{
printf("Reallocation failed!\n");
return false;
}
a->array = temp;
a->allocated *= 2;
}
a->array[a->used] = el;
a->used++;
return true;
}
由于重新分配是一项非常昂贵的操作,因此最好增加分配元素的数量,而不仅仅是一些常量。我通常把空间扩大一倍。由于您使用了malloc()
和realloc()
来分配内存,因此在使用完内存后,应该free()
释放内存:
void freeArray(Array * a)
{
free(a->array);
a->array = NULL;
a->allocated = 0;
a-> used = 0;
}
一旦有了这样的数组结构,就可以循环运行scanf()
,直到出现EOF
,在每个循环中,您scanf()
输入并使用insertEl()
函数将其添加到数组中。@WeatherVane-Ups!我的意思是int*b=malloc(200*sizeof(int))代码>请查看使用realloc
扩展数组。暂时忽略代码,从用户的角度看,您希望它如何工作?是否希望他们在同一行中输入所有数字?或者他们是否应该键入某种类型的“我完成了”指示器?也许Ctrl-D/Ctrl-Z表示文件结束?@JohnKugelman事实上,我的实际任务是反转用户输入的整数顺序。因此,如我的家庭作业示例所示,用户在同一行上输入数字1 7 5
,单击enter,然后在下面打印序列5 7 1
。我的问题不是整数的实际交换,而是用户取消输出(:)。