在函数声明中,传递一个固定大小的数组意味着什么?

时间:2021-12-28 21:42:31

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 ) {...

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

作为一个c#程序员(我在大学里学过C和c++),我甚至不知道你能做什么。我总是被告知,从那以后我就开始阅读了

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

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)

我被告知教授给了他们这个,它是有效的,那么像这样声明一个固定大小的数组意味着什么呢?c++没有一种本地的方法知道要传递给它的数组的大小(即使我认为在最新的规范中可能已经更改了)

3 个解决方案

#1


11  

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];
}

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 -

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

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

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

是不允许的,会是编译错误(g++):

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

#2


5  

The 25 in the parameter declaration is ignored by the compiler. It's the same as if you'd written string ar_dictionary[]. This is because a parameter declaration of array type is implicitly adjusted to a pointer to the element's type.

参数声明中的25被编译器忽略。就像你写了字符串ar_dictionary[]一样。这是因为数组类型的参数声明被隐式地调整为元素类型的指针。

So the following three function declarations are equivalent:

因此,以下三个函数声明是等价的:

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

Even in the case of the first function, with the size of the array explicitly declared, sizeof(ar_dictionary) will return the same value as sizeof(void*).

即使在第一个函数中,使用显式声明的数组大小,sizeof(ar_dictionary)也将返回与sizeof(void*)相同的值。

See this sample on Codepad:

请参阅Codepad上的示例:

#include <string>
#include <iostream>

using namespace std;

void read_dictionary(string ar_dictionary[25], int& dictionary_size)
{
    cout << sizeof(ar_dictionary) << endl;  
    cout << sizeof(void*) << endl;  
}

int main()
{
    string test[25];
    int dictionary_size = 25;
    read_dictionary(test, dictionary_size);

    return 0;
}

Output (the exact value is, of course, implementation-dependent; this is purely for example purposes):

输出(确切的值当然是依赖于实现的;这纯粹是为了举例):

4
4

#3


1  

I always though that passing fixed size C++ arrays was a "half baked" feature of C++. For example, ignored size matching or only being able to specify the first index size, etc... Until recently I learn this idiom:

我一直认为传递固定大小的c++数组是c++的“半成品”特性。例如,忽略大小匹配,或者只能指定第一个索引大小,等等……直到最近我才知道这个习语:

template<size_t N1, size_t N2> // enable_if magic can be added as well
function(double(&m)[N1][N2]){
  ... do something with array m...knowing its size!
}

Reference: Can someone explain this template code that gives me the size of an array?

引用:有人能解释一下这个模板代码吗?

#1


11  

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];
}

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 -

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

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

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

是不允许的,会是编译错误(g++):

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

#2


5  

The 25 in the parameter declaration is ignored by the compiler. It's the same as if you'd written string ar_dictionary[]. This is because a parameter declaration of array type is implicitly adjusted to a pointer to the element's type.

参数声明中的25被编译器忽略。就像你写了字符串ar_dictionary[]一样。这是因为数组类型的参数声明被隐式地调整为元素类型的指针。

So the following three function declarations are equivalent:

因此,以下三个函数声明是等价的:

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

Even in the case of the first function, with the size of the array explicitly declared, sizeof(ar_dictionary) will return the same value as sizeof(void*).

即使在第一个函数中,使用显式声明的数组大小,sizeof(ar_dictionary)也将返回与sizeof(void*)相同的值。

See this sample on Codepad:

请参阅Codepad上的示例:

#include <string>
#include <iostream>

using namespace std;

void read_dictionary(string ar_dictionary[25], int& dictionary_size)
{
    cout << sizeof(ar_dictionary) << endl;  
    cout << sizeof(void*) << endl;  
}

int main()
{
    string test[25];
    int dictionary_size = 25;
    read_dictionary(test, dictionary_size);

    return 0;
}

Output (the exact value is, of course, implementation-dependent; this is purely for example purposes):

输出(确切的值当然是依赖于实现的;这纯粹是为了举例):

4
4

#3


1  

I always though that passing fixed size C++ arrays was a "half baked" feature of C++. For example, ignored size matching or only being able to specify the first index size, etc... Until recently I learn this idiom:

我一直认为传递固定大小的c++数组是c++的“半成品”特性。例如,忽略大小匹配,或者只能指定第一个索引大小,等等……直到最近我才知道这个习语:

template<size_t N1, size_t N2> // enable_if magic can be added as well
function(double(&m)[N1][N2]){
  ... do something with array m...knowing its size!
}

Reference: Can someone explain this template code that gives me the size of an array?

引用:有人能解释一下这个模板代码吗?