通过二维数组中的功能:收到的二维数组指针数组 [英] Pass 2d array in function: 2d array received is pointer array

查看:165
本文介绍了通过二维数组中的功能:收到的二维数组指针数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我试图传递给函数一个巨大的二维数组。这里的数组:

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

我的问题是,当我通过了图片 max_2d 函数二维数组,数组成为一个int (*)[13]。我不明白发生了什么,什么是错。谁能帮我?

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?

修改

请记住,这是一个学生的工作。我需要了解什么是错的,我怎么能解决这个问题。谢谢!

Keep in mind, this is a student work. I need to understand what's wrong and how can I fix this. Thanks!

推荐答案

我看到一对夫妇的逻辑错误,在code:

I see a couple of logic errors in the code:


  1. valeur_max 在这些功能未初始化:这是不允许的(你不能与未初始化的值进行比较)

  1. valeur_max is not initialized in those functions: this is not allowed (you cannot compare with a not-initialized value).

在第二个函数的变量名为 valeur_max 但比较是保持最低值,而不是(它更新 valeur_max ,如果是更大的)。

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

至于你的问题在​​C ++中数组的标题传递给一个函数时,隐式转换为指针的第一要素。
如果你真的想通过值,你需要在结构上包起来,而不是阵列(注意,副本传递只是为了找到最大的似乎是废话)。

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

一个二维数组在C ++中只是一个数组的数组,从而将它传递给函数时传递的山谷变成一个指针数组(行)。这就是 INT(*)[13] 表示......一个指向13整数数组。

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.

对于C眼中++编译器,这两个声明

For the eyes of a C++ compiler the two declarations

void foo(int x[30]);

void foo(int *x);

是绝对相同的(是的,这个数字是完全忽略)。

are absolutely identical (yes, the number is completely ignored).

请注意,此隐式转换(或腐朽为标准将其描述)不会影响元素访问只是该阵列是不可复制的事实;例如版本只在一个函数做2D处理可能是:

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

这篇关于通过二维数组中的功能:收到的二维数组指针数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆