为什么 void 排序(int *[], int) 会导致"Unable to read memory"?
Why does void sorting(int *[], int) result in "Unable to read memory"?
我正在使用动态分配内存的C 程序中的程序。功能int *getNumbers(int)
正常工作。
我现在要做的就是获取这些信息,然后对其进行排序。当我将其发送到void sorting(int *[], int)
时,我没有收到任何错误消息,而是"无法读取内存"。这发生在:
if (*(sort[index]) < *minElem) // Around line 122
我并不是对分类的新手,而是使用指针。为什么不正确运行?
#include <iostream> //preprocessor directive Pg. 28 and Ch. 1
#include <string> // Same
#include <cmath>
#include <iomanip>
#include <ctime>
#include <fstream>
#include <cstdlib>
#include <cctype>
#include <algorithm>
#include <vector>
using namespace std;
int *getNumbers (int);
void sorting (int *[], int); //function that I am looking into
//does not work
int main ()
{
int *numbers_2 = nullptr;
int *numbers = nullptr; //pointer that I use
int num;
cout << "How mamy numberst";
cin >> num;
while (num < 5 || num > 21) {
cout << "nPlease try again - between 5 and 20n";
cout << "How mamy numt";
cin >> num;
}
numbers = getNumbers (num);
cout << "nThe numbers are:n";
for (int index = 0; index < num; index++) {
cout << numbers[index] << " ";
}
sorting (&numbers, num); //sorting function does not work
cout << "nLet's try this againn";
cout << "nThe numbers are:n";
for (int index = 0; index < num; index++) {
cout << numbers[index] << " ";
}
delete[] numbers;
delete[] numbers_2;
numbers = nullptr;
numbers_2 = nullptr;
cout << "nFinally Done!!!";
cout << "nn";
system ("PAUSE");
return 0;
}
int *getNumbers (int num)
{
int *array_Num = nullptr;
array_Num = new int[num];
for (int index = 0; index < num; index++) {
cout << "Please enter " << index + 1 << " numbert";
cin >> array_Num[index];
}
return array_Num;
}
void sorting (int *sort[], int size)
{
int startScan, minIndex;
int *minElem;
for (startScan = 0; startScan < (size - 1); startScan++) {
minIndex = startScan;
minElem = sort[startScan];
for (int index = (startScan + 1); index < size; index++) {
if (*(sort[index]) < *minElem) //part that I had problems
{
minElem = sort[index];
minIndex = index;
}
}
sort[minIndex] = sort[startScan];
sort[startScan] = minElem;
}
}
您的问题是,您将指针传递给数组(指向INT指针指针)到sorting
,您应该简单地将数组本身传递。例如。删除您在main()
中调用sorting
中的'&'
,然后将sorting
功能更改为:
void sorting (int *sort, int size)
{
int startScan, minIndex;
int minElem;
for (startScan = 0; startScan < (size - 1); startScan++) {
minIndex = startScan;
minElem = sort[startScan];
for (int index = (startScan + 1); index < size; index++) {
if (sort[index] < minElem) //part that I had problems
{
minElem = sort[index];
minIndex = index;
}
}
sort[minIndex] = sort[startScan];
sort[startScan] = minElem;
}
}
示例使用/输出
$ ./bin/array_sorting
How mamy numbers 4
Please try again - between 5 and 20
How mamy num 6
Please enter 1 number 9
Please enter 2 number 2
Please enter 3 number 12
Please enter 4 number 5
Please enter 5 number 1
Please enter 6 number 3
The numbers are:
9 2 12 5 1 3
Let's try this again
The numbers are:
1 2 3 5 9 12
Finally Done!!!
相关文章:
- 为什么在全局范围内使用"extern int a"似乎不行?
- int(c) 和 c-'0' 之间的区别。C++
- 从"int*"强制转换为"unsigned int"会丢失精度错误
- 为什么野牛仍在使用"int yylex(void)",却找不到"int yylex(YYS
- 有符号的int和int-有没有一种方法可以在C++中区分它们
- 请解释这句话(cout<<1+int((a<b)^((b-a)&1) )<<endl
- 是否可以从int转换为enum类类型
- 不能在初始值设定项列表中将非常量表达式从类型 'int' 缩小到'unsigned long long'
- 向量 <int> a {N, 0} 和 int arr a[N] = {0} 的时间复杂度有什么区别
- 'short int'持有的值溢出,但"自动"不会溢出?
- 如何在C++中将一个无符号的 int 转换为两个无符号的短裤?
- 调用'begin(int [n])'没有匹配函数
- 没有显式声明的int[]中的foreach
- 在c++中访问int到类对象的映射时出错
- 为什么我无法更改"set<set>"循环中的值<int>
- 长 长 int 不要 长 int 好
- C++程序在循环后给出奇怪的int值
- 如何计算数据类型的范围,例如int
- 如果"new int"返回"int*",那么为什么"new int[n]"不返回"int**"?
- 为什么 void 排序(int *[], int) 会导致"Unable to read memory"?