Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/55.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
当数组长度未知时,如何用用户输入值填充C中的数组_C_Arrays - Fatal编程技术网

当数组长度未知时,如何用用户输入值填充C中的数组

当数组长度未知时,如何用用户输入值填充C中的数组,c,arrays,C,Arrays,可能重复: 我试图用用户输入的值填充数组。但是,我事先不知道我的数组将有多少值,或者用户将键入多少值。用户一次键入一个值,该值存储在数组中,然后再次提示用户键入另一个值,依此类推,直到用户键入一个负数。当用户输入负数时,程序将打印出用户迄今为止输入的所有正值(不是负值,因为它基本上只用于终止程序) 我的问题是: 1) 如何在事先不知道阵列的大小的情况下声明阵列 2) 如何扫描用户输入?例如,我正在考虑这样的方法来扫描输入并将值分配给数组(这只是代码的一部分,不是全部,我只是想知道当我完成程序后

可能重复:

我试图用用户输入的值填充数组。但是,我事先不知道我的数组将有多少值,或者用户将键入多少值。用户一次键入一个值,该值存储在数组中,然后再次提示用户键入另一个值,依此类推,直到用户键入一个负数。当用户输入负数时,程序将打印出用户迄今为止输入的所有正值(不是负值,因为它基本上只用于终止程序)

我的问题是:

1) 如何在事先不知道阵列的大小的情况下声明阵列

2) 如何扫描用户输入?例如,我正在考虑这样的方法来扫描输入并将值分配给数组(这只是代码的一部分,不是全部,我只是想知道当我完成程序后,这部分代码是否可以工作):


这段代码正确吗(当然,它不是一个完整的程序)?另外,我如何声明数组,而不知道它有多大(我无法预先知道用户在键入负值之前将键入多少正值)

更好地使用链表,数组在这里对您没有帮助


如果您不熟悉它,请查看此项-

您可以为用户输入分配一个初始数组,并将其大小保存到一个变量中,以便在数组已满时重新分配数组。或者您可以使用链表保存输入,以便以后可以计算所需的元素计数并分配数组

int arraySize = 256; // Or whatever
int *array = malloc(arraySize * sizeof(int));
if (!array)
{
    fprintf(stderr, "Master, please buy more RAM, I can't allocate memory\n");
    return;
}

int numberOfElements = 0;
for(;;)
{          
    printf("Enter a positive value:\n");
    scanf("%d",&x);

    if (x >= 0)
    {
        if (numberOfElements == arraySize)
        {
            arraySize *= 2; // Or whatever strategy you need
            array = realloc(array, arraySize * sizeof(int));
            if (!array)
            {
                fprintf(stderr, "Master, please buy more RAM, I can't allocate memory\n");
                break;
            }
        }
        array[numberOfElements++] = x;
    }
    else
    {
        printf("You have entered a negative number \n");
        break;
    }
}

差不多。对于可能出现的错误,请不要检查。

您可以使用链表结构。有很多可能的实现(简单linkedlist、双linkedlist等),如果你在谷歌上搜索,你可以找到很多关于这个的页面。下面是一个例子(不一定是最优化的表单,但只是给你一个想法)

   int dataarray [2];
   int no;
   int count =0;

   while(1)
   {
        printf("Enter No's = ");
        scanf("%d",&no);
        if(no<0)
             break;
       *(dataarray+count)=no;
       count++;
   }
#包括
#包括
结构数据表
{
int值;
结构数据列表*下一步;
};
类型定义结构数据列表*链接列表;
void addToList(linkedList*param_valueList,const int param_newValue)
{
如果(*param_valueList==NULL)
{
linkedList newItem=(linkedList)malloc(sizeof(struct datalist));
newItem->value=param_newValue;
newItem->next=NULL;
*param_valueList=(linkedList)malloc(sizeof(linkedList));
*参数值列表=新项目;
}
其他的
{
linkedList newList=(linkedList)malloc(sizeof(struct datalist));
newList->value=param_newValue;
newList->next=NULL;
linkedList tmpList=*参数值列表;
while(tmpList->next!=NULL)
tmpList=tmpList->next;
linkedList*listPtr=&tmpList;
(*listPtr)->next=newList;
}
}
无效打印列表(常量链接列表参数值列表)
{
linkedList tmpList=参数值列表;
while(tmpList!=NULL)
{
printf(“%d\n”,tmpList->value);
tmpList=tmpList->next;
}
}
int main(int argc,char*argv[])
{
int inputNmbr=0;
linkedList numberList=NULL;
而(1)
{
printf(“打印一个数字:”);
scanf(“%d”,&inputNmbr);
如果(inputNmbr>0)
addToList(&numberList,inputNmbr);
其他的
打破
}
printf(“以下是您输入的数字:\n”);
打印列表(数字列表);
返回0;
}

关于,

我认为您需要提供一个预先分配的数组,然后在输入数据超过其大小时以块(比如200个元素)的形式进行realloc。请参见:谢谢@BigMike和其他-关于malloc/realloc的链接似乎很有趣,我不知道(我刚刚开始学习C)。还有,你觉得我的代码怎么样?它会工作吗?+1用于mallocing,然后重新定位数组if(!array)语句做什么?我以前从未见过它。如果您愿意,您可以将它想象成
if(array==0)
if(array==NULL)
。它检查我们是否可以分配内存,因为如果不能,则
malloc
/
realloc
返回
0
(或者
NULL
,如果需要)。好的,谢谢!顺便说一句,你觉得我的代码怎么样?它能用吗?我使用了一个“while do”语句,看起来像在工作(现在无法检查)<代码>对于(;)对我来说更漂亮更清晰。选一个你喜欢的。好的,非常感谢!
   int dataarray [2];
   int no;
   int count =0;

   while(1)
   {
        printf("Enter No's = ");
        scanf("%d",&no);
        if(no<0)
             break;
       *(dataarray+count)=no;
       count++;
   }
no = *(dataarray+count)
#include <stdio.h>
#include <stdlib.h>

struct datalist
{
    int value;
    struct datalist *next;
};
typedef struct datalist *linkedList;


void addToList(linkedList *param_valueList, const int param_newValue)
{
    if (*param_valueList == NULL)
    {
        linkedList newItem = (linkedList)malloc(sizeof(struct datalist));
        newItem->value = param_newValue;
        newItem->next = NULL;
        *param_valueList = (linkedList)malloc(sizeof(linkedList));
        *param_valueList = newItem;
    }
    else
    {
        linkedList newList = (linkedList)malloc(sizeof(struct datalist));
        newList->value = param_newValue;
        newList->next = NULL;

        linkedList tmpList = *param_valueList;
        while (tmpList->next != NULL)
            tmpList = tmpList->next;
        linkedList *listPtr = &tmpList;
        (*listPtr)->next =  newList;
   }
}


void printList(const linkedList param_valueList)
{
    linkedList tmpList = param_valueList;
    while (tmpList != NULL)
    {
        printf("%d\n", tmpList->value);
        tmpList = tmpList->next;
    }
 }


int main(int argc, char *argv[])
{
    int inputNmbr = 0;
    linkedList numberList = NULL;
    while (1)
    {
        printf("print a number: ");
        scanf("%d", &inputNmbr);
        if (inputNmbr > 0)
            addToList(&numberList, inputNmbr);
        else
            break;
    }

    printf("Here are the numbers you entered:\n");
    printList(numberList);

    return 0;
}