简体   繁体   English

将数组传递给函数时,是否需要提供元素个数作为参数?

[英]When passing an array to a function, is it necessary to provide the number of elements as an argument?

I'm currently studying C programming using the textbook C: How to program 6/e . 我目前正在使用教科书C:How to 6 / e进行 C编程研究。

Now, in the book when discussing arrays, it states that when passing an array, (other than a character array, due to the '\\0' character) to a function, it is necessary to pass the number of elements in the array to the function as well 现在,在这本书中,当讨论数组时,它指出将数组(由于字符\\ 0而不是字符数组)传递给函数时, 必须将数组中的元素数传递给功能也一样

So given my functions 所以鉴于我的职能

int myfunct(int myary[], int numelements);

int myfunct(int *myary, int numelements);

Assume that ary has been declared with 10 elements and that we are now in the main function. 假设ary已经用10个元素声明,并且我们现在在main函数中。

myfunct(ary, 10); 

Is the ( int numelements necessary [in either case], and if so why). (在任何情况下都是int numelements ,如果是,为什么)。 The book states that it is for the compiler to know the correct number of elements to process, however the book then goes on to not do this in almost all its examples, and is very very inconsistent. 该书指出,应该由编译器知道要处理的元素的正确数量,但是该书随后在几乎所有示例中都没有这样做,并且非常不一致。

Also, 也,

int myfunct(int myary[][10], int numrows, int numcolumns);

Same question about numrows and numcolumns. 关于数字和数字列的相同问题。

To illustrate further: 为了进一步说明:

#include <stdio.h>
void test(const int ary[]);

int main(void){

    int myary[10] = {1,2,3,4,5,6,7,8,9,10};
    test(myary);

    return 0;
}

void test (const int ary[]){
    int i;

    for(i = 0; i<10; i++){
        printf("The number in array element %d is:    %d\n", i, ary[i] );
    }
}

This code seems to compile correctly without the additional parameter, but I do not understand why the book would say I need them, and so do many sites. 这段代码似乎可以在没有附加参数的情况下正确编译,但是我不明白为什么书会说我需要它们,许多站点也是如此。 HELP! 救命!

Is it necessary? 有必要吗? Will my code compile if I don't pass array size as argument? 如果不传递数组大小作为参数,我的代码会编译吗?

It is not necessary & your code will compile even if you do not pass array size as argument to the function. 没必要,即使您不将数组大小作为函数的参数传递,您的代码也将编译。
But it is a good practice to do so. 但这是一个好习惯。

Rationale: 理由:

C does not provide you means of bounds checking for arrays. C没有为您提供边界检查数组的方法。 You can access beyond the bounds of array through array indexing or pointer arithmetic( Under the hood,both are same ) and the compiler does not need to warn you about it. 您可以通过数组索引或指针算术( 实际上是相同的 )访问超出数组范围的内容并且编译器无需对此发出警告。 But you will end up getting an Undefined Behavior . 但是您最终将获得未定义的行为
So it is important that you only access memory which belongs to your array. 因此,仅访问属于您阵列的内存很重要。 To be able to do this you need to keep track of the size of the array.It is the user's responsibility to do so. 为此,您需要跟踪数组的大小,这是用户的责任。

When you pass an array as argument it decays as pointer to first element of the array. 当您将数组作为参数传递时,它会衰减为指向数组第一个元素的指针。
So sizeof(arg) inside the function will give you the size of the pointer not the array. 因此,函数内部的sizeof(arg)将为您提供指针的大小,而不是数组的大小。

There is no way you can know the size of the array inside the function( exception of null terminated string, who's size can be obtained by strlen ) unless you pass it as an argument or through some other bookeeping(like global variable etc). 除非您将其作为参数传递或通过其他布尔值(例如全局变量等)传递,否则您将无法知道函数内部数组的大小( 以null结尾的字符串例外,其大小可以通过strlen获得 )。

Passing the size as an argument is the most obvious way to do this. 传递大小作为参数是最明显的方法。

Is it necessary to provide the number of elements as an argument? 是否需要提供元素个数作为参数?

Most of the time, yes. 大多数时候,是的。 It is not necessary when passing a null-terminated C string , as you already noted. 正如您已经指出的那样,在传递以N结尾的C字符串时 ,这是不必要的。

If so, why? 如果是这样,为什么?

Because there is no other way for your function to know how many elements are there in your array. 因为您的函数没有其他方法可以知道数组中有多少个元素。 So if your function needs to know that, you need to give it that information. 因此,如果您的职能部门需要知道这一点,则需要为其提供相关信息。

A better statement is that the function needs to know what to do. 更好的说法是该功能需要知道该怎么做。 In particular, it needs to know how many elements to use in an array. 特别是,它需要知道在数组中使用多少个元素。

You can tell it by passing the number in a parameter, by building it into the function with a constant, or by marking a final element in the array in some way (eg, the '\\o' character in a string), or by other means (eg, a parameter from which the number can be calculated, or in a global variable [which is usually a poor design choice]). 您可以通过将数字传递到参数中,通过使用常数将其构建到函数中,以某种方式标记数组中的最后一个元素(例如,字符串中的'\\ o'字符)或通过其他方式(例如,可以从中计算数字的参数,或在全局变量中[通常是较差的设计选择])。

In short, this is simply about necessity: The code must have the information it needs to do its job. 简而言之,这仅与必要性有关:代码必须具有完成其工作所需的信息。 It is not about C in particular. 它与C无关。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM