在函数声明中,传递固定大小的数组表示什么? [英] In a function declaration, what does passing a fixed size array signify?

查看:177
本文介绍了在函数声明中,传递固定大小的数组表示什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这听起来像是一个很愚蠢的事情,但是我让某人参加编程课,请我帮忙做某事,我在他们的代码中看到了这一点(请不要对匈牙利表示法发表评论):

This feels like a really stupid thing to ask, but I had someone taking a programming class ask me for some help on an assignment and I see this in their code (no comments on the Hungarian notation please):

void read_dictionary( string ar_dictionary[25], int & dictionary_size ) {...

作为一个主要的C#程序员(我在大学里学习过C和C ++),我什至不知道您能做到.我总是被告知,并且已经读过,因为你应该拥有

Which, as mainly a C# programmer (I learned about C and C++ in college) I didn't even know you could do. I was always told, and have read since that you're supposed to have

void read_dictionary( string ar_dictionary[], int ar_dictionary_size, int & dictionary_size ) {...

有人告诉我教授给了他们这个方法,并且它起作用了,那么声明一个固定大小的数组到底意味着什么呢? C ++没有本地方法可以知道传递给它的数组的大小(即使我认为在最新的规范中可能已对此进行了更改)

I'm told that the professor gave them this and that it works, so what does declaring a fixed size array like that even mean? C++ has no native way of knowing the size of an array being passed to it (even if I think that might've been changed in the newest spec)

推荐答案

在一维数组中,它没有意义,并且被编译器忽略.在二维或二维数组中,此函数很有用,并由函数用作确定矩阵(或多维数组)的行长的一种方式.例如:

In a one dimensional array It has no significance and is ignored by the compiler. In a two or more dimensional array It can be useful and is used by the function as a way to determine the row length of the matrix(or multi dimensional array). for example :

int 2dArr(int arr[][10]){
   return arr[1][2];
}

此函数将根据指定的长度知道arr[1][2]的地址,并且编译器不应为此函数接受不同大小的数组-

this function would know the address of arr[1][2] according to the specified length, and also the compiler should not accept different sizes of arrays for this function -

int arr[30][30];
2dArr(arr);

不允许,将导致编译器错误(g ++):

is not allowed and would be a compiler error(g++) :

error: cannot convert int (*)[30] to int (*)[10]

这篇关于在函数声明中,传递固定大小的数组表示什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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