C语言 如何使用malloc在ANSI-C中声明动态整数数组并将输入整数放入其中?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16035952/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 06:03:30  来源:igfitidea点击:

How to declare a dynamic integer array in ANSI - C using malloc and put input integers into it?

carrayspointersmallocansi

提问by TomG

First I want the user to input what is the size of the desired array. So I am using:

首先,我希望用户输入所需数组的大小。所以我正在使用:

int size;
scanf("&d",&size);

Now I want to create an integer array using a pointer and the malloc function. This is what I did:

现在我想使用指针和 malloc 函数创建一个整数数组。这就是我所做的:

int *p1 = (int*)malloc(sizeof(int)*size);

According to my understanding, this is like using:

根据我的理解,这就像使用:

int p1[size];

But how do I use it like an array?

但是我如何像数组一样使用它?

Question1: Now I want the user to input as many integers as he wrote into this "array". But I can't use p[0] because it is not an array, it is a pointer.

问题1:现在我希望用户输入与他写入这个“数组”一样多的整数。但是我不能使用 p[0] 因为它不是一个数组,它是一个指针。

Question2: I want to "send" this array to a function that gets an array of integers. So again, this is not an array, how can I "give" it to the function?

问题2:我想将此数组“发送”给一个获取整数数组的函数。再说一次,这不是一个数组,我怎么能把它“给”给函数呢?

回答by A human being

Answer to first question:

回答第一个问题:

for(i = 0; i < size; i++ )
{
   scanf("%d",&p[i]); 
   /*p[i] is the content of element at index i and &p[i] is the address of element 
at index i */
}

Or

或者

for(i = 0; i < size; i++ )
{
   scanf("%d",(p+i)); //here p+i is the address of element at index i
}

Answer to second question:

回答第二个问题:

For sending this array to the function, just call the function like this:

要将这个数组发送到函数,只需像这样调用函数:

function(p); //this is sending the address of first index of p

void function( int *p ) //prototype of the function

回答by Lefteris E

  • Question 1: 1d arrays and pointers to properly allocated memory are pretty-much the same thing.
  • Question 2: When passing an array to a method you are actually passing the address of the 1st element of that array
  • 问题 1:一维数组和指向正确分配内存的指针几乎是一回事。
  • 问题 2:将数组传递给方法时,您实际上是在传递该数组的第一个元素的地址

An array is actually a pointer to the first element of the array

数组实际上是指向数组第一个元素的指针

回答by bash.d

You can use the subscript-syntax to access an element of your pointer.

您可以使用subscript-syntax 来访问指针的元素。

p1[3] = 5; // assign 5 to the 4th element

But, this syntax is actually converted into the following

但是,这个语法实际上被转换成以下

*(p1+3) = 5; // pointer-syntax

For your second question, define a function and pass a pointer

对于你的第二个问题,定义一个函数并传递一个指针

int* getarray(int* p1, size_t arraysize){ } //define the function

int* values = getarray(p1, size); // use the function

回答by Pulkit Agarwal

sorry for bothering everyone but Miss Upasana was right this is the correct method for dynamic array use. After declaring ur array through malloc u can directly use it exactly as array as follows::

抱歉打扰了大家,但 Upasana 小姐是对的,这是动态数组使用的正确方法。通过 malloc 声明你的数组后,你可以直接像数组一样使用它,如下所示:

       for(int i = 0; i < size; i++ )
    {
       scanf("%d",p+i); 
       /*p+i denoting address of memory allocated by malloc */
    }

Second Answer: Now simply pass this address to any function use address to find values like:

第二个答案:现在只需将此地址传递给任何函数使用地址来查找值,例如:

function(int *p)
/* access as &p for first value &p+2 for second p+4 for third and so on*/