动态内存分配和动态数组 [英] dynamic memory allocation and dynamic array

查看:97
本文介绍了动态内存分配和动态数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要编写一个程序,这将要求用户输入一个数字,他们希望有多少整数进入..所以输出看起来像

I need to write a program, that will ask a user to enter a number of how many ints they would like to enter.. so the output would look like

int型的输入数字(必须大于1)

Enter number of Ints (must be greater then 1)

和他们将投入2之间一个数字 - 无限远(如果他们真的想要走那么远)

and they would input a number between 2 - infinity (if they really wanted to go that far)

在这一点上,我会scanf函数这一数字,并将其设置为变量

at that point i would scanf that number and set it to a variable

现在用这个数字,我想运行一个for循环来要求他们开始进入他们的INTS

now with that number, i want to run a for loop to ask them to begin entering their Ints

for (count = 0; count < numofInts; count++)
{
    printf("    Enter an integer: ");
   scanf("%d", &Number);
}

有问题IM是,我需要确保它记录了每一个他们进入数字,所以我需要有存储到一个数组的值,但是数组的元素数量必须是动态的,这样它可以取决于numofInts变化,我应该使用malloc()来创建一个动态内存分配的空间,我的理解,它创建与什么都我设置的malloc一个内存空间中的变量,但我不知道如何存储系列变量空间,然后叫他们回来,因为我需要他们。

the problem im having is that i need to make sure that it records every number that they enter, so i need to have those values stored to an array, but the number of elements of the array must be dynamic so that it can change depending on the numofInts, I'm supposed to use Malloc() to create a dynamic memory allocated space, and i understand that it creates a variable with a memory space of what ever i set the malloc to, but i don't know how to store a series a variables to that space, and then call them back as i need them.

该计划的最终结果应该是采取通过存储intergers为最大的int数里一个像123456789,和周期,然后吐出这int是最大的,所以像X = 1234567890,X %10中,x = 0,最大= X,X / 10中,x%10中,x = 9,如果x>最大,最大= x和只是循环,直到它循环通过整个数,并存储该号码的最后。我有部分下降,而是因为我必须采取一系列的数字,并运行这个循环对所有这些数字,我需要能够存储和调用这些值,并将其放置在循环能够存储最大这些数字的位数

The end result of the program is supposed to take a number like 123456789, and cycle through the number storing the intergers as the "largest" int, and then spit out which int is the largest, so like x = 1234567890, x % 10, x = 0, largest = x, x / 10, x % 10, x = 9, if x > largest, largest = x, and just loop that till it cycles through the whole number, and store that number at the very end. I have that part down, but because i have to take a series of numbers and run this loop for all of those numbers, i need to be able to store and recall those values and place them in the loop to be able to store the largest digits of those numbers

这个问题的任何帮助将不胜AP preciated,我只是一直无法弄清楚如何使用malloc或创建一个动态数组和大多数教程的香港专业教育学院的在线阅读或YouTube上观看即将C ++和我需要不仅仅是C做到这一点...

any help with this problem would be greatly appreciated, i just have not been able to figure out how to use malloc or to create a dynamic array and most of the tutorials ive read online or watched from youtube are about C++ and i need to do this with just C...

http://pastebin.com/PZyvEQ4J

是我迄今为止

推荐答案

在你读 numInts ,你分配数组,像这样:

After you read numInts, you allocate the array like so:

int* arr = malloc(numInts*sizeof(int));

现在您需要用您的现有功能的阵列和分配的值读取到数组中。

Now you populate the array with your already existing function and assigning the values read to the array.

我不打算给你一个完整的解决方案,因为这是功课,不会帮助你,但你访问 I '个数组元素与 [] 运营商:

I'm not going to give you a full solution, since this is homework and wouldn't help you, but you access the i'th element of the array with the [] operator:

arr[i];

这篇关于动态内存分配和动态数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆