Reputation: 2090
I have a huge 2d array that I am trying to pass to a function. Here's the array:
int image[13][13] =
{
{ 72, 72, 72, 72, 72, 72, 72, 72, 72, 72, 72, 72, 72 },
{ 72, 72, 71, 70, 70, 70, 70, 70, 70, 70, 70, 72, 72 },
{ 72, 107, 116, 145, 137, 130, 154, 118, 165, 111, 173, 116, 72 },
{ 72, 126, 150, 178, 158, 175, 163, 169, 170, 160, 176, 163, 70 },
{ 72, 130, 192, 195, 197, 186, 129, 185, 196, 196, 193, 195, 70 },
{ 72, 126, 187, 166, 85, 75, 106, 185, 191, 191, 189, 188, 69 },
{ 72, 121, 183, 111, 100, 51, 137, 188, 187, 186, 184, 180, 69 },
{ 72, 117, 177, 143, 58, 77, 137, 180, 171, 183, 178, 173, 69 },
{ 72, 111, 172, 108, 101, 110, 115, 67, 49, 120, 175, 165, 68 },
{ 72, 107, 145, 105, 145, 120, 85, 51, 51, 56, 138, 157, 68 },
{ 72, 103, 147, 158, 155, 131, 115, 114, 114, 115, 121, 152, 68 },
{ 72, 79, 146, 161, 163, 165, 168, 167, 164, 162, 158, 114, 70 },
{ 72, 69, 53, 49, 49, 49, 49, 49, 49, 49, 50, 61, 72 }
};
I have this function declared like this:
int max_2d(int p_valeurs[13][13]);
int max_2d(int p_valeurs[13][13])
{
int valeur_max;
for (int i = 0; i < 13; i++)
{
int max_local = max(p_valeurs[i], LARGEUR);
if (valeur_max < max_local)
{
valeur_max = max_local;
}
}
return valeur_max;
}
int max(int p_valeurs[]);
int max(int p_valeurs[], int p_taille)
{
int valeur_max;
for (int i = 0; i < p_taille; i++)
{
if (valeur_max > p_valeurs[i])
{
valeur_max = p_valeurs[i];
}
}
return valeur_max;
}
My problem is that when I pass the image
2d array in the max_2d
function, the array becomes an int(*)[13]. I don't understand what is happening and what is wrong. Can anyone help me?
EDIT
Keep in mind, this is a student work. I need to understand what's wrong and how can I fix this. Thanks!
Upvotes: 1
Views: 80
Reputation: 114579
I see a couple of logic errors in the code:
valeur_max
is not initialized in those functions: this is not allowed (you cannot compare with a not-initialized value).
In the second function the variable is named valeur_max
but the comparison is keeping the minimum value instead (it updates valeur_max
if it's bigger).
As for the title of your question in C++ arrays are implicitly converted to a pointer to the first element when passed to a function. If you really want to pass the array by value you need to wrap it up in a structure instead (note that passing by copy just to find the maximum seems a nonsense).
A 2D array is in C++ just an array of arrays, thus when passing it to a function the passed vale becomes a pointer to an array (the row). That's what int(*)[13]
means... a pointer to an array of 13 integers.
For the eyes of a C++ compiler the two declarations
void foo(int x[30]);
and
void foo(int *x);
are absolutely identical (yes, the number is completely ignored).
Note that this implicit conversion (or "decay" as the standard describes it) doesn't impact element access but just the fact that the array is not copied; for example a version doing the 2d processing in just one function could be:
int max_2d(int p[13][13]) {
int max_val = INT_MIN;
for (int i=0; i<13; i++) {
for (int j=0; j<13; j++) {
if (max_val < p[i][j]) {
max_val = p[i][j];
}
}
}
return max_val;
}
Upvotes: 2
Reputation: 27183
It's the classic problem in C and C++, where arrays cannot be passed by value. They can however be passed by reference in C++:
int max_2d(int (&p_valeurs)[13][13]);
p_valuers
has the correct type. But bear in mind that this is a reference. Any changes you make, for example p_valuers[3][7]++;
will be reflected at the call site.
(In C, I would pass in a pointer to the array int max_2d(int (*p_valeurs)[13][13]);
for a typesafe solution.)
But this isn't a problem for you, as you are not modifying p_valuers
inside the max_2d
function. In fact, you should explicitly mark it as const
for clarity.
int max_2d(const int (&p_valeurs)[13][13]);
Finally, I said arrays cannot be passed by value in C or C++. When you attempt to pass an array by value, it will instead pass a pointer to the first element of the array. Also, when the compiler sees arrays in the list of parameters of a function, it automatically converts the "top level" array into a pointer, int x[13][13]
to int (*x)[13]
. This means a function that appears to take an array actually takes a pointer. But we can block this behaviour in a number of ways, for example using a reference in C++.
Upvotes: 1