gpt4 book ai didi

c - 如何确定数组的大小?

转载 作者:太空宇宙 更新时间:2023-11-04 08:29:19 25 4
gpt4 key购买 nike

我在创建一个简单的程序时遇到了问题。程序请求收集输入值的数量(用于创建适当大小的数组),收集值,并计算位置 >= 1 的所有数字的乘积。问题是无论大小如何指定(即控制创建数组的大小)数组的大小始终报告为 4。例如,如果收集了 10 个输入,则创建大小为 10 的数组,但检查数组的大小结果为 4。

这是代码:

int main() {

double *aNumberArray = 0;
aNumberArray = askNumbers();

int position = 0;
position = sizeof(aNumberArray);

for (int i = 0; i < position; i++) {

cout << aNumberArray[i] << endl;
}

cout << "The product of your numbers is " << productOfArray(aNumberArray, position) << endl;

delete[] aNumberArray;
return 0;
}


double *askNumbers() {
int size;
double *numbers ;

cout << "Product of the numbers which positions are n >= 1" << endl;
cout << "How many numbers would you like to multiply? ";
cin >> size;

numbers = new double[size];

cout << "Type in your numbers: " << endl;

for (int i = 0; i < size; i++) {

cout << "Number " << i + 1 << ": ";
cin >> numbers[i];

}

return numbers;
}

double productOfArray(const double anArray[], int size) {

const double number = 1;
double product = 0;

if (size >= 1) {

product = anArray[size] * (productOfArray(anArray, size - 1));

}
else if (size == 0)
return number;

return product;
}

最佳答案

double *aNumberArray = 0;
position = sizeof(aNumberArray);

aNumberArray 变量是一个指针 而不是数组。因此它的大小是一个指针的大小(在你的例子中是四个)。

指针不携带对象的底层数组的大小信息,您只能获取指针的大小或一个对象指针的大小。

如果你想传回尺寸,你可以使用类似的东西:

double *askNumbers(int &size) {
double *numbers ;

cout << "Product of the numbers which positions are n >= 1" << endl;
cout << "How many numbers would you like to multiply? ";
cin >> size;

numbers = new double[size];

//get numbers, blah blah blah

return numbers;
}

并调用它:

double *aNumberArray = 0;
int position;
aNumberArray = askNumbers(position);

可以通过下面的代码看到效果:

#include <iostream>
#include <iomanip>

double *getArray (int &sz) {
std::cout << "What size? ";
std::cin >> sz;
double *array = new double[sz];
for (int i = 0; i < sz; i++)
array[i] = 3.14159 * i;
return array;

}

int main(int argc, char *argv[]){
int count;
double *xyzzy = getArray(count);
for (int i = 0; i < count; i++)
std::cout << std::fixed << std::setw(6) << xyzzy[i] << '\n';
delete [] xyzzy;
return 0;
}

当你编译并运行它时,你可以看到大小确实被传回了,使用建议的方法:

What size? 4
0.000000
3.141590
6.283180
9.424770

但您可能还想考虑成为一名“真正的”C++ 开发人员,而不是一名混合 C+ 开发人员(一种似乎从未完全完成转变的奇怪品种)。您可以使用标准库附带的集合来执行此操作,例如 携带大小的 vector :

#include <iostream>
#include <iomanip>
#include <vector>

std::vector<double> getArray () {
int sz;
std::cout << "What size? ";
std::cin >> sz;
std::vector<double> vect = std::vector<double>(sz);
for (int i = 0; i < sz; i++)
vect[i] = 3.14159 * i;
return vect;

}

int main(int argc, char *argv[]){
std::vector<double> xyzzy = getArray();
for (int i = 0; i < xyzzy.size(); i++)
std::cout << std::fixed << std::setw(6) << xyzzy[i] << '\n';
return 0;
}

看起来并没有那么简单,但随着您的程序变大,它会变得如此简单,从长远来看,知道如何使用它会让您的生活变得更轻松。

关于c - 如何确定数组的大小?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29114599/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com