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!
int *" (even usingint *p = &p_valeurs[0][0];) because that would violate the strict aliasing rule when iteration reaches the 2nd row.int *p = &arr[0][0];– why would you ever do the casting thing?), then you can only use that pointer to access the elements of the first row. Anything else invokes undefined behavior (as discussed here, here and here).arr[0]in C. (nothing to do with strict aliasing; and also it's unclear whether it is UB in C++). However if you writeint *p = (int *)&arr;orint *p = (int *)arrthen there is definitely no out of bounds access becausepis bounded to all ofarr, not just the first row.