传递数组的大小 [英] size of passed array

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

问题描述

大家好,


我需要建立一个函数来插入一个程序(我没有做或

可以改变)应该是叫做这样的东西:

浮动someFunction(浮动x [])

{

...


}

问题是,我不能使用someFunction(float x [],int sizex)所以我

无法知道数组的大小。

有没有办法可以从里面计算静态数组的大小

函数例如sizeof(x)/ sizeof(浮动)或类似的东西?


谢谢

V.


- -

通过 http://www.teranews的免费Usenet帐户发布。 com

解决方案

Vasileios Zografos写道:


大家好,


我需要构建一个函数来将它插入一个程序(我没有做或

可以更改)应该被称为类似的东西这个:


浮动someFunction(浮动x [])

{

...


}


问题是,我不能使用someFunction(float x [],int sizex)所以我

无法知道的大小数组。

有没有办法可以从里面计算静态数组的大小

函数例如sizeof(x)/ sizeof(浮点)或类似的东西?


谢谢

V.



否。这是一个不使用数组的好理由,而是使用向量。


BTW in''float someFunction(float x [])''x is * not *一个数组,它是一个

指针,它与你写的''浮动

someFunction(float * x)''完全相同。你应该更喜欢第二种形式,因为它没有尝试假装x是一个阵列,当它不是时。


你需要在一本好的C ++书中读到数组和指针。


john


Vasileios Zografos写道:


大家好,


我需要构建一个函数将它插入程序中(我没有制作或

可以改变)应该被称为这样的东西:


浮动someFunction(浮动x [])

{

。 ..


}


问题是,我不能使用someFunction(float x [],int sizex)所以我

无法知道数组的大小。

有没有办法可以从里面计算静态数组的大小

函数例如sizeof(x)/ sizeof(浮点)或类似的东西?


谢谢

V.



如果我理解这一点,你必须编写一个函数,可以从别人的代码中的各个地方调用,并且可以被称为

具有不同大小的阵列,但是你不能知道阵列的大小是什么。


这样的功能是不可能编写的。要么你误解了你应该做什么,要么写了其他代码的人是一个非常差的程序员。


john


John Harrison写道:


如果我理解这一点,你必须写一个函数,可以从别人的代码中的各个地方调用,并可以用不同大小的数组调用

,但你不能知道是什么

数组的大小是。


这样的函数是不可能编写的。要么你误解了你应该做什么,要么写了其他代码的人是一个非常差的程序员。


john



嗨约翰,

是的我觉得你可能是对的。也许我误解了

的要求,因为这听起来确实不可能。我会再次检查。

谢谢

V.


-

发表于来自 http://www.teranews.com 的免费Usenet帐户


Hi everyone,

I need to build a function to plug it in a program (that I didnt make or
can change) that should be called something like this:
float someFunction(float x[])
{
...

}
the problem is, I cannot use someFunction(float x[], int sizex) so I
cannot know the size of the array.
Is there a way I can calculate the size of the static array from inside
the function e.g. sizeof(x)/sizeof(float) or something similar?

Thank you
V.

--
Posted via a free Usenet account from http://www.teranews.com

解决方案

Vasileios Zografos wrote:

Hi everyone,

I need to build a function to plug it in a program (that I didnt make or
can change) that should be called something like this:
float someFunction(float x[])
{
...

}
the problem is, I cannot use someFunction(float x[], int sizex) so I
cannot know the size of the array.
Is there a way I can calculate the size of the static array from inside
the function e.g. sizeof(x)/sizeof(float) or something similar?

Thank you
V.

No. This is a good reason not to use arrays, use vectors instead.

BTW in ''float someFunction(float x[])'' x is *not* an array, it is a
pointer, it is exactly the same as if you had written ''float
someFunction(float* x)''. You should prefer the second form because it
doesn''t try to pretend that x is an array when it isn''t.

You need to read up on arrays and pointers in a good C++ book.

john


Vasileios Zografos wrote:

Hi everyone,

I need to build a function to plug it in a program (that I didnt make or
can change) that should be called something like this:
float someFunction(float x[])
{
...

}
the problem is, I cannot use someFunction(float x[], int sizex) so I
cannot know the size of the array.
Is there a way I can calculate the size of the static array from inside
the function e.g. sizeof(x)/sizeof(float) or something similar?

Thank you
V.

If I understand this right, you have to write a function, which can be
called from various places in someone else''s code, and could be called
with different size arrays, but you are not allowed to know what the
size of the array is.

Such a function is impossible to write. Either you have misunderstood
what you are supposed to do, or the person who wrote the other code is a
very poor programmer.

john


John Harrison wrote:

If I understand this right, you have to write a function, which can be
called from various places in someone else''s code, and could be called
with different size arrays, but you are not allowed to know what the
size of the array is.

Such a function is impossible to write. Either you have misunderstood
what you are supposed to do, or the person who wrote the other code is a
very poor programmer.

john

Hi John,
yes I think you are probably right. Maybe I misunderstood the
requirements because indeed it sounds impossible. I will check again.
Thank you
V.

--
Posted via a free Usenet account from http://www.teranews.com


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

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