从指针 C++ 获取数组的大小 [英] getting size of array from pointer c++

查看:188
本文介绍了从指针 C++ 获取数组的大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个返回数组中最大整数的简单函数.我遇到的问题是找到数组中的元素数.

I am writing a simple function that returns the largest integer in an array. The problem I am having is finding the number of elements in the array.

这是函数头:

int largest(int *list, int highest_index)

如何获取数组list"中的整数个数.

How can I get the number of integers in the array 'list'.

我尝试了以下方法:

int i = sizeof list/sizeof(int); //returns incorrect value
int i = list.size(); // does not compile

任何帮助将不胜感激!

推荐答案

C++ 基于 C 并继承了 C 的许多特性.关于这个问题,它继承了称为数组/指针等价"的规则,该规则允许数组衰减为指针,尤其是在作为函数参数传递时.这并不意味着数组一个指针,它只是意味着它可以衰减为1.

C++ is based on C and inherits many features from it. In relation to this question, it inherits something called "array/pointer equivalence" which is a rule that allows an array to decay to a pointer, especially when being passed as a function argument. It doesn't mean that an array is a pointer, it just means that it can decay to one.

void func(int* ptr);

int array[5];
int* ptr = array; // valid, equivalent to 'ptr = &array[0]'
func(array); // equivalent to func(&array[0]);

最后一部分与您的问题最相关.您不是传递数组,而是传递第 0 个元素的地址.

This last part is the most relevant to your question. You are not passing the array, you are passing the address of the 0th element.

为了让您的函数知道传入数组的大小,您需要将该信息作为参数发送.

In order for your function to know how big the incoming array is, you will need to send that information as an argument.

static const size_t ArraySize = 5;
int array[ArraySize];
func(array, ArraySize);

因为指针不包含大小信息,所以不能使用sizeof.

Because the pointer contains no size information, you can't use sizeof.

void func(int* array) {
    std::cout << sizeof(array) << "\n";
}

这将输出 "int*" 的大小 - 根据 32 位和 64 位,它是 4 或 8 个字节.

This will output the size of "int*" - which is 4 or 8 bytes depending on 32 vs 64 bits.

相反,您需要接受尺寸参数

Instead you need to accept the size parameters

void func(int* array, size_t arraySize);

static const size_t ArraySize = 5;
int array[ArraySize];
func(array, ArraySize);

即使你试图传递一个固定大小的数组,事实证明这是语法糖:

Even if you try to pass a fixed-sized array, it turns out this is syntactic sugar:

void func(int array[5]);

http://ideone.com/gaSl6J

还记得我说过数组不是指针,而是等价的吗?

Remember how I said that an array is NOT a pointer, just equivalent?

int array[5];
int* ptr = array;

std::cout << "array size " << sizeof(array) << std::endl;
std::cout << "ptr size " << sizeof(ptr) << str::endl;

数组大小为 5 * sizeof(int) = 20ptr 大小将是 sizeof(int *),它是 4 或 8 个字节.

array size will be 5 * sizeof(int) = 20 ptr size will be sizeof(int *) which will be either 4 or 8 bytes.

sizeof 返回所提供类型的大小,如果您提供一个对象,那么它会推导出类型并返回该类型的大小

如果你想知道数组中有多少个元素,当你有数组而不是指针时,你可以写

If you want to know how many elements of an array are in the array, when you have the array and not a pointer, you can write

sizeof(array) / sizeof(array[0])

或sizeof(array)/sizeof(*array)

or sizeof(array) / sizeof(*array)

这篇关于从指针 C++ 获取数组的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆