如何确定数组的大小?

时间:2015-03-18 05:15:18

标签: c

我在制作简单程序时遇到了麻烦。程序请求收集的输入值的数量(用于创建适当大小的数组),收集值,并计算位置> = 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;
}

2 个答案:

答案 0 :(得分:1)

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

aNumberArray变量是指针而不是数组。因此它的大小是指针的大小(在你的情况下是四个)。

指针不包含对象的基础数组的大小信息,您只能获得指针的大小或一个对象指针的大小。< / p>

如果您想要回传尺码,可以使用以下内容:

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

但你可能也想考虑成为一个真实的&#34; C ++开发人员而不是混合C +开发人员(一个从未完全过渡的奇怪品种)。您可以使用标准库附带的集合来完成此操作,例如 带有大小的向量:

#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;
}

它看起来不那么简单,但随着你的程序变大,它会变得如此,知道如何使用它会让你的生活变得更加轻松运行

答案 1 :(得分:1)

这是一个关于获取数组大小的快速教程: http://www.cplusplus.com/faq/sequences/arrays/sizeof-array/