将指针传递给函数时出错了
Something went wrong with passing pointer to a function
#include <cstdlib>
#include <iostream>
#include <Math.h>
using namespace std;
int stepCount,i,x,y,z,j,array1Size,array2Size,tester;
int numstring[10] = {0,1,2,3,4,5,6,7,8,9};
int numstringTest[10] = {0,1,2,3,4,5,6,7,7,9};
int* numbers;
int* differentNumbers;
void stepCounter(int a){
// determines the step number of the number
if(a/10 == 0)
stepCount = 1;
else if(a/100 == 0)
stepCount = 2;
else if(a/1000 == 0)
stepCount = 3;
else if(a/10000 == 0)
stepCount = 4;
else if(a/100000 == 0)
stepCount = 5;
else if(a/1000000 == 0)
stepCount = 6;
else if(a/10000000 == 0)
stepCount = 7;
else if(a/100000000 == 0)
stepCount = 8;
else if(a/1000000000 == 0)
stepCount = 9;
}
void stepIndicator(int b){
// indicates each step of the number and pass them into array 'number'
stepCounter(b);
numbers = new int [stepCount];
for(i=stepCount; i>0; i--){
//
/*
x = (round(pow(10,stepCount+1-i)));
y = (round(pow(10,stepCount-i)));
z = (round(pow(10,stepCount-i)));
*/
x = (int)(pow(10,stepCount+1-i)+0.5);
y = (int)(pow(10,stepCount-i)+0.5);
numbers[i-1] = (b%x - b%y)/y;
}
}
int sameNumberCheck(int *array, int arraySize){
//checks if the array has two or more of same integer inside return 1 if same numbers exist, 0 if not
for(i=0; i<arraySize-1; i++){
//
for(j = i+1; j<arraySize; j++){
//
if(array[i]==array[j]){
//
return 1;
}
}
}
return 0;
}
void sameNumberCheckOfTwoArrays(int* array1, int* array2){
//
array1Size = sizeof(array1)/sizeof(array1[0]);
array2Size = sizeof(array2)/sizeof(array2[0]);
cout << array1Size << endl;
}
int main(int argc, char *argv[])
{
stepCounter(999999);
cout << stepCount << endl;
stepIndicator(826424563);
for(j=0; j<9; j++){
//
cout << numbers[j] << endl;
}
cout << sameNumberCheck(numstringTest, 10) << " must be 1" << endl;
cout << sameNumberCheck(numstring, 10) << " must be 0" << endl;
//cout << sameNumberCheckOfTwoArrays(numstring, numstringTest) << " must be 10" << endl;
sameNumberCheckOfTwoArrays(numstring, numstringTest);
tester = sizeof(numstringTest)/sizeof(numstringTest[0]);
cout << tester << endl;
system("PAUSE");
return EXIT_SUCCESS;
}
我的代码就像上面一样,但我的问题很简单。您一定见过sameNumberCheckOfTwoArrays函数。获取整型指针(在这个程序数组中)并查找数组的大小。方法很简单:
array1Size = sizeof(array1)/sizeof(array1[0]);
array2Size = sizeof(array2)/sizeof(array2[0]);
cout << array1Size << endl;
如你所见。但是当我用numstring和numstringTest调用函数时,每个函数都有10个元素,它会计算数组大小为1?!您可以执行代码。但是当我不使用函数计算时,就像你在代码底部看到的那样,我正确地得到了10。为什么会发生这种情况?我想我调用函数并将值传递给函数,对吧?或者我没有?
将数组作为指针传递给sameNumberCheckOfTwoArrays。在这个过程中,"数组性"被去掉了。你的函数只看到一个int*,并相应地报告大小。
相关文章:
- #定义c-预处理器常量..我做错了什么
- 努力将整数转换为链表。不知道我在这里做错了什么
- 首要问题的答案让值班员搞错了
- 看起来is_nothrow_constructible_v()在MSVC中被破坏了,我错了吗
- .h 和.cpp文件分离时出错,但仅使用 .h 文件时没有错误.我做错了什么?
- 我的C++线程做错了什么?
- 如何正确使用 >=?(a+f()+c)>=0 错了吗?
- 谁能告诉我我用 getline 做错了什么 (cpp) 格式
- 没有输出的合并排序我做错了什么?
- 我正在尝试使用 while 循环从字符串中删除字母,直到没有字母。我在这里做错了什么?
- 在C++中使用 AKS 素数测试计算双胞胎素数 我做错了什么?
- 指针相关的UE4崩溃.我的指针哪里错了?
- 我一直试图弄清楚我在这个链表程序中做错了什么
- 我正在尝试学习如何在 c++ 中传递指针,但出现错误:没有用于调用"test"的匹配函数。我做错了什么?
- FFMPEG,C++,内存泄漏,我做错了什么?
- 我做错了什么?反向字符串 C++
- 我在这个课上做错了什么?
- 我在尝试将多个值push_back向量时出错。我做错了什么?
- 过载时出错 <<。我在这里做错了什么?
- 使用针对Win32的英特尔C++编译器编译Qt时出错,我做错了什么