当我在函数参数中使用指针时,我遇到了错误

when i was using pointers in function arguments i faced with an error

本文关键字:遇到 错误 指针 函数 参数      更新时间:2023-10-16

错误是这样:无法转换int*' to INT*',用于参数1' to BOOL Permition(INT *,INT,INT)'在代码中,我有一个int板[n],用户给了'n'...我想在此数组中赋予我的验证功能,所以我必须用指针给它,因为未指定它的长度...所以我该如何解决此问题这是我的代码:

#include <cstdlib>
#include <iostream>
#include <cmath>
using namespace std;
bool permition(int* board[],int place,int n_){
 int m=place;
 while(m!=0){          
             m--;
             if(abs(abs(board[m]-board[place])-abs(m-place))==1 
                && abs(m-place)<3 && abs(board[m]-board[place]))
                 return false;
             }
 return true;     
 }
 void printBoard(int* board[],int n){
 for(int i=0;i<n;i++)
         cout << board[i]<< " ";
 cout << endl;
 }
int main()
{
int p=0;
int n;
cout << "plz: ";
cin >> n;
int board[n];
for(int i=0;i<n;i++)
        board[i]=0;
while(p<n){
           while((board[p]<n) && permition(board,p,n)==false)
               board[p]+=1;
           if(board[p]<n)
               p++;
           else{
                p--;
                board[p]+=1;
                }
           if(p==n && board[0]<n-1)
    //it means the first number is not max so we should         
    //print and continue from fist again
           {
                   printBoard(board,n);
                   p=0;
                   board[0]+=1;
           }
           }
system("PAUSE");
return 0;
}

在验证的功能定义中摆脱[]:

bool permition(int* board,int place,int n_)

看看是否有帮助!

更改

bool permition(int* board[],int place,int n_)

to

bool permition(int const board[],int place,int n_)

当前的参数声明(上面首先)说 boardint的指针数组,不是。


[]最终是指针,因此您可以写

bool permition(int const* board,int place,int n_)

此表格的优势是您也可以在指针上使用const,而使用[],您有一个可以更改的指针,但看起来像一个数组。

缺点是声明不再向读者传达"数组"。

正如其他人所指出的,原始代码将int的数组(被视为int *)传递到被声明将A Pointer 的函数传递给一个函数int的数组。这是一个更正的版本,我希望有一些格式化更改。

#include <cstdlib>
#include <iostream>
#include <cmath>
using namespace std;
// Removed unused parameter n_.
bool permission(int board[], int place) {
  int m = place;
  // The original code could return false only if (place - m) < 3,
  // so no need to test when (place - m) >= 3.
  while (m-- > max(0, place - 3) { 
    int board_diff = abs(board[m] - board[place];
    int index_diff = place - m;  // Always >= 0
    if (abs(board_diff - index_diff) == 1 && board_diff != 0) {
      return false;
    }
 }
 return true;     
}
void printBoard(int board[], int n) {
  for (int i = 0; i < n; i++) {
    cout << board[i] << " ";
  }
  cout << endl;
}
int main() {
  int p = 0;
  int n;
  cout << "plz: ";
  cin >> n;
  int board[n] = { 0 };  // Zeroes entire array.
  while (p < n) {
    // Never compare bool to true or false; just use !bool_var.
    while ((board[p] < n) && !permission(board, p)) {
      board[p]++;
    }
    if (board[p] < n) {
      p++;
    } else {
      board[--p]++;
    }
    if (p == n && board[0] < (n - 1)) {
      // The first number is not max so we should         
      // print and continue from first again
      printBoard(board, n);
      board[p = 0]++;  // Assign and increment.
    }
  }
  system("PAUSE");
  return 0;
}
int board[];

相同
 int *board; 

所以我猜测的解决方案是写

bool permition(int board[],int place,int n_)
void printBoard(int board[],int n)

您需要了解并在指针上进行更多工作!