C 编译时两个错误

c++ two errors while compiling

本文关键字:两个 错误 编译      更新时间:2023-10-16

我是C 的初学者,我在代码中遇到了两个错误,我不知道如何修复它们...第一个

非法间接

第二个是

'='左操作数必须是i值。(在线:(arrayptr i) j)= rand()%55 1)

有人知道如何修复它们吗?那是我的代码:

#include <iostream>
#include <math.h>
#include <time.h>
#include<iomanip>
#include<array>
#include <algorithm>
using namespace std;
const int AS = 6;
void FillingRandomly(int (*)[AS]);
void printing(int (*)[AS]);
int c;
int main()
{
    int funny = 0;
    int timpa = 0;
    int counter = 0;
    int Array[AS][AS];
    srand(time(0));
    FillingRandomly(Array);
    cout << "The unsorted array is" << endl << endl;
    printing(Array);
    cout << "The sorted array is" << endl << endl;
    printing(Array);
    system("PAUSE");
    return 0;
}
void FillingRandomly(int *ArrayPtr)
{
    for(int i=0;i<AS;i++)
    {
        for (int j=0;j<AS;j++)
        {
            *(*(ArrayPtr +i)+j)=rand()%55+1;
        }
    }
}
void printing(int *Array)
{
    for(int i=0;i<AS;i++)
    {
        for (int j=0;j<AS*AS;j++)
        {
            int counter = 0;
            cout<<((Array[i] +j))<<setw(5);
            if ((Array[i] +j)%AS == 0)
            cout << endl << endl;
        }
    }
}
void forsorting(int *Brray, int funny)
{
    int dice = 0;
    int super = 0;
    int space=0;
    //Sorting Array[][] which is treated like Array[]
    {
        for (int pass = 0; pass < AS - 1; pass++) {
            for (int k = 0; k < AS - 1; k++) {
                int temp;
                if(*(Brray+k)==*(Brray+k+1))
                {
                    temp=*(Brray+k);
                    *(Brray+k)=*(Brray+k+1);
                    *(Brray+k+1)=temp;
                }
            }
        }
    }
}

*(*(ArrayPtr +i)+j)=rand()%55+1;

看来您想要

ArrayPtr[i][j] = (rand() % 55) + 1;

您可以尝试

的线
int const offset = AS * i + j;
int const elem = (rand() % 55) + 1;
*(ArrayPtr + offset) = elem;

您的功能签名是:

void FillingRandomly(int *ArrayPtr)

您要向编译器告诉您要通过一个简单的指针的地方,但在这条线上:

*(*(ArrayPtr +i)+j)=rand()%55+1;

您正在进行双重差异,这是非法的,并导致编译器抱怨

补充

我在另一个答案中看到了评论,而且由于我需要写的内容比保留的评论空间更大,因此我决定补充自己的答案。

您将Array定义为:

int array [as] [as];

的确,您正在做的是保证编译器,您将使用定义的数组,但是编译器不太相信您,因此任何时候使用Array,编译器都会确保它正在确保它正在用作声明的。

当您声明FillingRandomly功能时,出现了问题。在这里,您正在促进您的诺言,并试图通过声明其他类型来使用Array。注意如何声明您的功能:

void FillingRandomly(int *ArrayPtr)

由于C 支持函数过载,因此编译器在无法找到签名为:

的函数时不会警告您。
void FillingRandomly(int ArrayPtr[][AS])

请注意,两者都不一样。

一旦成为初学者,正确保留程序的最佳方法就是保持诺言不可变。Bellow我向您展示了您自己的代码,为FillingRandomly函数纠正这些问题(您也必须为其他功能纠正它):

const int AS = 6;
void FillingRandomly(int [][AS]); // Note that I've changed your prototype here
....
void FillingRandomly(int ArrayPtr[][AS]) // Keep your function signature the same as your prototype signature
{
    for(int i=0;i<AS;i++)
    {
        for (int j=0;j<AS;j++)
        {
            ArrayPtr[i][j]=rand()%55+1;  // Note how ArrayPtr is being used exactly as your promised early
        }
    }
}