C ++通过阵列的功能问题 [英] c++ pass array to function question

查看:72
本文介绍了C ++通过阵列的功能问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这怎么可能给一个数组参数传递给一个函数,计算数组参数的大小?因为每次我试图来计算的话,返回值始终是4。

How is it possible to pass an array parameter to a function, and calculate the size of the array parameter? Because every time i try to calculate it, the return value is always 4.

由于快速的答复。我要传递数组的大小以及随后。或者我给​​了一枪载体。谢谢

Thanks the fast replies. I'm going to pass the size of the array as well then. Or i give a shot to vectors. Thanks

推荐答案

这是(之一)阵列和指针之间的差。取阵列结果的大小在它的大小在字节,而采取了指针的大小产生该系统上的指针的大小。 然而,只要你传递一个数组给一个函数,它衰变到一个指针,它的大小始终是相同的,无论是什么类型的指针(4 32位机器上)。

This is (one of) the difference between arrays and pointers. Taking the size of an array results in its size in bytes, whereas taking the size of a pointer yields the size of pointers on that system. However, whenever you pass an array to a function, it decays to a pointer, the size of which is always the same no matter what type of pointer it is (4 on a 32 bit machine).

所以从技术上说,这是不可能的数组传递到一个函数,因为每当你尝试,它成为一个指针。

So technically, it's impossible to pass an array into a function since whenever you try, it becomes a pointer.

您需要传递数组的大小,以功能为好,或者更好,如果你可以使用的std ::矢量的std ::阵列的std ::字符串如果你使用数组作为C风格的字符串。

You'll need to pass the size of the array in to the function as well, or better yet if you can, use std::vector or std::array, or std::string if you're using the array as a C-style string.

这篇关于C ++通过阵列的功能问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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