如何使用数组作为变量取消函数? [英] How do I decleare a function with array as variable ?

查看:72
本文介绍了如何使用数组作为变量取消函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

// Array testing
//=====================================================

#include <math.h>
#include <iostream>
#include <stdlib.h>
using namespace std;


float f(float x[10][10], float y[10][10])
{
      float temp ;
      temp = x[10][10] + y[10][10];
      return(temp);
      }
// ---------------------- Main ------------------------

int main()
{
float temp[10][10] ;
float p, x[10][10] , q ,y[10][10];
int i , t , j ;

for(i=1;i<=4;++i)
{
for(j=1;j<=4;++j)
{
x[i][j] = i+j;
y[i][j] = i-j;

temp[i][j] = f(x[i][j] , y[i][j]);
cout<<"temp["<<i<<"]["<<j<<"] = "<<temp[i][j]<<endl;
}
}return(0);
}





我的尝试:



我运行这个程序但是出现以下消息----



无法将`float'转换为`float(*)[10]' for参数`1'到`float f(float(*)[10],float(*)[10])'



What I have tried:

I run this programme but the following message occurs ----

cannot convert `float' to `float (*)[10]' for argument `1' to `float f(float (*)[10], float (*)[10])'

推荐答案

看看你的代码:

你声明f是一个有两个参数的方法,它们都是浮动的多维数组。

当你调用它时,你传递的是一个 de-引用数组值:x和y是数组,但x [a] [b]和y [a] [b]是浮点数。

所以要么直接传递x和y,要么改变f接受两个浮点值而不是数组。



数组x [10]中有多少个元素?什么是有效的索引范围?那你为什么要用x [10] [10]和y [10] [10]?
Look at your code:
You are declaring f as a method that has two parameters, both of them a multidimensional array of floats.
When you call it, you are passing a de-referenced array value: x and y are arrays, but x[a][b] and y[a][b] are floats.
So either pass x and y directly, or change f to accept two float values instead of arrays.

And how many elements are there in an array x[10]? And what are the valid range of indexes into it? So why are you using x[10][10] and y[10][10]?


这篇关于如何使用数组作为变量取消函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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