In a function declaration, what does passing a fixed size array signify?

13,771

Solution 1

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 -

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

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

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

Solution 2

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.

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

See this sample on 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

Solution 3

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:

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?

Share:
13,771
cost
Author by

cost

Updated on June 05, 2022

Comments

  • cost
    cost about 2 years

    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

    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)

  • cost
    cost about 12 years
    What do you mean the function would know the address?
  • cost
    cost about 12 years
    Does WeaselFox's answer make any sense to you? If it's just decayed to a pointer, then what he said shouldn't be true. I'm a bit confused
  • Spidey
    Spidey about 12 years
    In the first one, sizeof(ar_dictionary) will return sizeof(void*) too?
  • WeaselFox
    WeaselFox about 12 years
    accessing the array at [x][y] means accessing the element at row y and column x. for this you have to know what is the length of the row. arr[1][2] is the same as arr[2*10 + 1].
  • Cody Gray
    Cody Gray about 12 years
    Arrays with multiple dimensions work differently than those with a single dimension. I'm not sure what that has to do with anything; the question only concerns one-dimensional arrays. @cos
  • Cody Gray
    Cody Gray about 12 years
    @Spidey: Yes. Added an example to my answer.
  • Keith Thompson
    Keith Thompson about 12 years
    There are actually two different rules in play here. One is that an expression of array type, in most contexts, is implicitly converted to ("decays" to) a pointer to the array's first element. The other is that a parameter declaration of array type is adjusted to a pointer to the element type. The language could have had either rule without the other, but it has both. They work together to -- well, to cause confusion.