C 编译时两个错误
c++ two errors while compiling
我是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
}
}
}
相关文章:
- 如何确保在使用基于布尔值的两个方法之一调用方法时避免分支预测错误
- 两个线程一个使用流 Api,另一个线程创建文件失败并出现错误ERROR_SHARING_VIOLATION
- C++哈希表中,两个相同的实现,但一个给出错误
- 使用 SET(C++) 检查两个给定字符串是否是字谜时出现运行时错误
- C++ 分段错误:11 错误,同时编码将两个数字相加的链接列表
- 在 C++ 中创建两个不同的二叉树时出现分段错误
- 交换两个字符串时访问正确的内存时,我遇到了分段错误
- 试图添加两个矢量,但添加顺序错误
- pair的两个构造函数几乎相同,为什么不生成构建错误?
- 纠正C++学校图书馆管理程序中的两个错误
- C 编译时两个错误
- 在主函数c++中使用类时出现两个错误
- 评分程序中的两个错误
- 使用类和标头的两个错误
- 在编译程序之前,我不确定如何修复我的两个错误
- 我正在做一个学校项目,我遇到了两个错误,我不知道如何纠正它们,任何想法
- 我得到两个错误,但我不能识别它们
- 运算符重载 - 知道为什么我在 c++ 程序中遇到这两个错误吗?
- 与使用类有关的两个错误
- 需要一些帮助的两个错误(c++)