Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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)

like image 848
cost Avatar asked Apr 29 '12 04:04

cost


People also ask

What happens when an array is passed to a function?

An array passed to a function decays into a pointer to its first element.

What is fixed size array?

A fixed array is an array for which the size or length is determined when the array is created and/or allocated. A dynamic array is a random access, variable-size list data structure that allows elements to be added or removed. It is supplied with standard libraries in many modern programming languages.

Why do we often need to pass the size of an array as a parameter to a function?

Passing the array size tells the function where the bounds are so you can choose not to go beyond them.

When array is passed as an argument it indicates the?

1) In C, if we pass an array as an argument to a function, what actually get passed? The correct option is (b). Explanation: In C language when we pass an array as a function argument, then the Base address of the array will be passed.


2 Answers

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]
like image 84
WeaselFox Avatar answered Oct 29 '22 00:10

WeaselFox


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
like image 21
Cody Gray Avatar answered Oct 29 '22 01:10

Cody Gray