二维数组和指针. [英] 2-d Array and pointer.

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

问题描述

{
int a[10][10]={{1,2,3},{4,5,6},{7,8,9}};
int n;
n=FindSumLeavingOutRowCol(a, 3,3,1,2);
printf("%d",n);

}
int FindSumLeavingOutRowCol(int** arr, int m, int n, int k,int l)
{
int sum,i,j;
for(i=0;i<m;i++)
{
    for(j=0;j<n;j++)
    {
        if((i==k)&&(j==l))
        {
            ;
        }
        else{
            sum+=*(*(a+i)+j);
        }
    }
    }

我在传递 2 d 数组时遇到错误,并且它告诉 'a' 未声明.请帮我整理错误!

I am getting an error on passing the 2 d array also its telling 'a' undeclared. Please help me in sorting out the bug!

推荐答案

表达式中的数组,例如用作参数时,将转换为指向其第一个元素的指针.

Arrays in expressions as for example when used as arguments are converted to pointers to their first elements.

如果你有这样的数组

int a[10][10];

可以像这样重写

int ( a[10] )[10];

也就是说,它是一个由 int[10[ 类型的 10 个元素组成的数组,然后在表达式中它被转换为 int ( * )[10] 类型,

that is it is an array of 10 elements of the type int[10[ then in expressions it is converted to the type int ( * )[10],

例如你可以写

int ( a[10] )[10] = { {1,2,3}, {4,5,6}, {7,8,9} };
int (  *p   )[10] = a;

因此函数应该声明为

int FindSumLeavingOutRowCol( int arr[][10], int m, int n, int k,int l);

int FindSumLeavingOutRowCol( int ( *arr )[10], int m, int n, int k,int l);

如果编译器支持变长数组并且变量 mn 代表维度(不是范围),那么函数也可以像

If the compiler supports variable length arrays and the variables m and n represent the dimensions (not the ranges) then the function can be also declared like

int FindSumLeavingOutRowCol( int m, int n, int arr[m][n], int k,int l);

int FindSumLeavingOutRowCol( int m, int n, int arr[][n], int k,int l);

或者喜欢

int FindSumLeavingOutRowCol( int m, int n, int ( *arr )[n], int k,int l);

否则,您将需要添加另外两个参数来指定除维度之外的范围.

Otherwise you will need to add two more parameters that specify the ranges apart from the dimensions.

注意变量sum没有初始化

int sum,i,j;

所以函数会有未定义的行为.

So the function will have undefined behavior.

此外,参数被声明为具有名称 arr 而不是 a.

And moreover the parameter is declared as having name arr not a.

我希望这个函数有一个类似的返回语句

And I hope the function has a return statement something like

return sum;

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

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