使用sizeof()的数组长度 - 问题 [英] length of an array using sizeof() - problem

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

问题描述

以下C代码实现了一个简单的FIR滤波器:


//实时滤波器演示

#include< stdio.h> ;

#include< stdlib.h>


//函数defination

float rtFilter1(float * num,float * den,float * xPrev,float * yPrev);


void main()

{

float sig_in [30],分子[3],分母[3],x [3],y [2],s_out [30];

int i;

int kk;


//单位冲动

sig_in [0] = 1.0;

printf(单位冲动...... \ n) ;

printf("输入元素编号:1 =%f \ n",sig_in [0]);


for(i = 1; i< 30; i ++)

{

sig_in [i] = 0.0;

printf("输入元素编号:%d =% f \ n",i + 1,sig_in [i]);

}


//初始化以前的输入和输出数组

for(i = 0; i< 3; i ++)

{

x [i] = 0.0;

}

$ (b = b;(i = 0; I&2; i ++)

{

y [i] = 0.0;

}

//设置滤波器系数


分子[0] = 1;

分子[1] = 1.71;

分子[2] = 0.94;


//测试

kk = sizeof(分子)/ sizeof(浮动);


分母[0] = 1;

分母[1] = -1.865;

分母[2] = 0.889;


printf(" \ n \ noutput信号值... \ n");


//打印输出信号

for(i = 0; i< 30; i ++ )

{

x [0] = sig_in [i];

s_out [i] = rtFilter1(分子,分母,x,y) ;


printf("输出元素编号:%d =%f \ n",i + 1,s_out [i]);

}

}

// a(1)* y(n)= b(1)* x(n)+ b(2)* x(n-1) + ... + b(nb + 1)* x(n-nb) - a(2)* y(n-1) - ... - a(na + 1)* y(n-na)


//实时过滤器 - 直接形式I - 单输入(xPrev [0])/单输出(sig_out)

float rtFilter1(f loat * num,float * den,float * xPrev,float * yPrev)

{

//正数

浮动psum = 0.0;

//否定金额

float nsum = 0.0;


float sig_out;


//得到分子系数数组的长度

int numLen = sizeof(num)/ sizeof(float);

//得到分母系数数组的长度

int denLen = 3;

int i;


// calc。积分和

for(i = 0; i< numLen; i ++)

{

psum = psum + num [i] * xPrev [我];


}


//计算。为了绕过第一个分母数组元素,den [0](= 1)

{

nsum = nsum + den [i] * yPrev [i-1];

}


//移位内存


//在前一个输入数组中移位值


for(i = 2; i< = numLen; i ++)// xPrev中的最后一个元素是xPrev [numLen-1],即长度(xPrev)== length(num)

{

xPrev [(numLen-i)+1] = xPrev [numLen-i]; //将每个数组值向上移动一个元素,例如xPrev [1] = xPrev [0]


}


//移动前一个值输出数组


for(i = 3; i< = denLen; i ++)// i = 3因为yPrev比denLen少1个元素

{

yPrev [(denLen-i)+1] = yPrev [denLen-i]; //将每个aeeay值向上移动一个元素,例如yPrev [1] = yPrev [0]


}


// calc。输出信号

yPrev [0] = psum - nsum;

sig_out = yPrev [0];


返回sig_out; < br $>

} //结束rtFilter1


我的问题如下:


以下代码计算数组中元素的数量(num):

int numLen = sizeof(num)/ sizeof(float);


此方法在rtFilter中使用查找数组长度,但是它会产生错误的结果。它在主函数中使用时会产生正确的结果。





//测试

kk = sizeof(numerator)/ sizeof(float);


有谁知道为什么我得到两个不同的结果基本相同的代码行?


无论如何,我可以在函数rtFilter()中找到数组的长度(分子)?


注意:我在这里使用了4个元素的数组测试程序。通常数组长度是未知的。


谢谢。

The following C-code implements a simple FIR filter:

//realtime filter demo

#include <stdio.h>
#include <stdlib.h>

//function defination
float rtFilter1(float *num, float *den, float *xPrev, float *yPrev);

void main()
{
float sig_in[30], numerator[3], denominator[3], x[3], y[2], s_out[30];
int i;
int kk;

//unit impulse
sig_in[0] = 1.0;
printf("unit impulse... \n");
printf("input element number: 1 = %f \n", sig_in[0]);

for(i = 1; i<30; i++)
{
sig_in[i] = 0.0;
printf("input element number: %d = %f\n", i+1, sig_in[i]);
}

//initialise previous input and output arrays
for(i = 0; i<3; i++)
{
x[i] = 0.0;
}

for(i = 0; i<2; i++)
{
y[i] = 0.0;
}
//set filter coefficients

numerator[0] = 1;
numerator[1] = 1.71;
numerator[2] = 0.94;

// test
kk = sizeof(numerator)/sizeof(float);

denominator[0] = 1;
denominator[1] = -1.865;
denominator[2] = 0.889;

printf("\n\noutput signal values...\n");

//print output signal
for(i = 0; i<30; i++)
{
x[0] = sig_in[i];
s_out[i] = rtFilter1(numerator, denominator, x, y);

printf("output element number: %d = %f\n", i+1, s_out[i]);
}
}
//a(1)*y(n) = b(1)*x(n) + b(2)*x(n-1) + ... + b(nb+1)*x(n-nb) - a(2)*y(n-1) - ... - a(na+1)*y(n-na)

// realtime filter - direct form I - single input(xPrev[0]) / single output(sig_out)
float rtFilter1(float *num, float *den, float *xPrev, float* yPrev)
{
//positive sum
float psum = 0.0;
//negitive sum
float nsum = 0.0;

float sig_out;

//get length of numerator coefficient array
int numLen = sizeof(num)/sizeof(float);
//get length of denominator coefficient array
int denLen = 3;
int i;

//calc. positive sum
for(i = 0; i<numLen; i++)
{
psum = psum + num[i]*xPrev[i];

}

//calc. negitive sum
for(i = 1; i<denLen; i++) // i=1 in order to bypass first denominator array element, den[0] (=1)
{
nsum = nsum + den[i]*yPrev[i-1];
}

//shift memory

//shift values in previous input array

for(i = 2; i<=numLen; i++) //the last element in xPrev is xPrev[numLen-1], ie length(xPrev) == length(num)
{
xPrev[(numLen-i)+1] = xPrev[numLen-i]; // shifts each array value up a element eg xPrev[1] = xPrev[0]

}

//shift values in previous output array

for(i = 3; i<=denLen; i++) // i=3 because yPrev has 1 less element than denLen
{
yPrev[(denLen-i)+1] = yPrev[denLen-i]; // shifts each aeeay value up a element eg yPrev[1] = yPrev[0]

}

//calc. output signal
yPrev[0] = psum - nsum;
sig_out = yPrev[0];

return sig_out;

} //end rtFilter1

My problem is as follows:

The following code calculates the number of elements in an array(num):

int numLen = sizeof(num)/sizeof(float);

This method of finding array length is used in rtFilter, however it produces the wrong result. It produces the correct result when used in the main function though.

i.e.

// test
kk = sizeof(numerator)/sizeof(float);

Does anyone know why I am getting two different results for basicly the same line of code?

Is there anyway I can find the length of the array(numerator) inside the function rtFilter()?

Note: I''ve used arrays of 4 elements here just to test the program. Usually the array lengths are unknown.

Thanks.

推荐答案

" fdunne2" < FD ***** @ nospam.yahoo.ie>写在

新闻:6e ****************************** @ localhost.ta lkaboutprogramming.com :
"fdunne2" <fd*****@nospam.yahoo.ie> wrote in
news:6e******************************@localhost.ta lkaboutprogramming.com:
void main()/ * AGHHH !!! * /
int main(无效)


[snip]我的问题如下:

以下代码计算一个元素的数量array(num):
int numLen = sizeof(num)/ sizeof(float);


首先,写一个这样的宏:


#define NUM_OF(a)(sizeof(a)/ sizeof *(a))


并使用它像:


int numLen = NUM​​_OF(num);

这种查找数组的方法length在rtFilter中使用,但它会产生错误的结果。它在主要功能中使用时会产生正确的结果。
void main() /* AGHHH!!! */ int main(void)

[snip] My problem is as follows:

The following code calculates the number of elements in an array(num):

int numLen = sizeof(num)/sizeof(float);
First, write a macro like this:

#define NUM_OF(a) (sizeof (a) / sizeof *(a))

and use it like:

int numLen = NUM_OF(num);
This method of finding array length is used in rtFilter, however it
produces the wrong result. It produces the correct result when used in
the main function though.




接下来,了解一旦你通过一个数组到另一个函数作为一个

参数,它衰减到一个指向数组第一个元素的指针,

sizeof运算符将无法正常工作。你需要将

sizeof num作为附加参数传递给函数rtFilter()。


-

- 马克 - >

-



Next, understand that once you "pass" an array to another function as a
parameter it decays in to a pointer to the first element of the array and
the sizeof operator will not work as you wish it to. You need to pass the
sizeof num to the function rtFilter() as an additional parameter.

--
- Mark ->
--


fdunne2< fd ***** @ nospam.yahoo.ie>写道:
fdunne2 <fd*****@nospam.yahoo.ie> wrote:
以下C代码实现了一个简单的FIR滤波器:
//实时滤波器演示
#include< stdio.h>
#include<文件stdlib.h>
//函数defination
float rtFilter1(float * num,float * den,float * xPrev,float * yPrev);
void main()
{/ s>浮动sig_in [30],分子[3],分母[3],x [3],y [2],s_out [30];
int i;
int kk;


< snipped>

kk = sizeof(numerator)/ sizeof(float);


< snipped>

s_out [i] = rtFilter1(分子,分母,x,y);
我的问题如下:
以下代码计算数组中的元素数(num):
int numLen = sizeof(num)/ sizeof(float);
这种查找数组长度的方法在rtFilter中使用,但它会产生错误的结果。它在主要功能中使用时会产生正确的结果。


这是因为在main()中你将sizeof应用于一个真正的数组(分子)但是rtFilter1()中的
只是一个浮点指针。因此在main()中你得到数组的大小

,而在rtFilter1()中你得到一个浮点指针的大小。

无论如何我能找到长度
函数rtFilter()中的数组(分子)?
The following C-code implements a simple FIR filter: //realtime filter demo #include <stdio.h>
#include <stdlib.h> //function defination
float rtFilter1(float *num, float *den, float *xPrev, float *yPrev); void main()
{
float sig_in[30], numerator[3], denominator[3], x[3], y[2], s_out[30];
int i;
int kk;
<snipped>
kk = sizeof(numerator)/sizeof(float);
<snipped>
s_out[i] = rtFilter1(numerator, denominator, x, y); My problem is as follows: The following code calculates the number of elements in an array(num): int numLen = sizeof(num)/sizeof(float); This method of finding array length is used in rtFilter, however it produces
the wrong result. It produces the correct result when used in the main
function though.
That''s because in main() you apply sizeof to a real array (numerator) but
in rtFilter1() to just a float pointer. Thus in main() you get the size
of the array while in rtFilter1() you get the size of a float pointer.
Is there anyway I can find the length of the array(numerator) inside the
function rtFilter()?




你必须将数组的长度与指向
数组到rtFilter1()。你无法从指针本身找出指针所指向的

数组的长度。


问候,Jens
-

\ Jens Thoms Toerring ___ Je***********@physik.fu-berlin.de

\ __________________________ http://www.physik.fu-berlin.de/~toerring



You have to pass the length of the array together with the pointer to the
array to rtFilter1(). There is no way you can find out the length of the
array the pointer is pointing to just from the pointer itself.

Regards, Jens
--
\ Jens Thoms Toerring ___ Je***********@physik.fu-berlin.de
\__________________________ http://www.physik.fu-berlin.de/~toerring


fdunne2写道:
fdunne2 wrote:
以下C代码实现了一个简单的FIR滤波器:

//实时滤波器演示

#include< stdio.h>
#include< stdlib.h>

//函数defination
float rtFilter1(float * num,float * den,float * xPrev,float * yPrev );

void main()
`main''返回`int''!!!!!!!! {
浮点sig_in [30],分子[3],分母[3],x [3],y [2],s_out [30];
int i;
int kk;
[snip]
// test
kk = sizeof(numerator)/ sizeof(float);


这是有效的,因为分子是一个在范围内的数组。

[snip]
}
// a(1)* y(n)= b(1)* x(n)+ b(2)* x(n-1)+ ... + b(nb + 1)* x(n-nb) - a(2)* y(n-1) - ... - a(na + 1)* y(n-na)
//实时滤波器 - 直接表格I - 单输入(xPrev [0])/单输出(sig_out)
float rtFilter1(float * num,float * den,float * xPrev,float * yPrev)
{
/ /正总和
浮动psum = 0.0;
//否定总和
浮动nsum = 0.0;

浮动sig_out;

//得到分子系数数组的长度
int numLen = sizeof(num)/ sizeof(float);


在这种情况下,`num''参数已经衰减为指向它已经传递的数组的第一个

元素的指针;它的大小丢失了。因此,

值将始终是指向浮点数的大小除以浮点数的大小

。不是你想要的。 ;-(


[snip]
} // end rtFilter1

我的问题如下:

下面的代码计算数组中元素的数量(num):
int numLen = sizeof(num)/ sizeof(float);

这种查找数组长度的方法是在rtFilter中使用,但它会产生错误的结果。虽然在主函数中使用它会产生正确的结果。



//测试
kk = sizeof(numerator)/ sizeof(float);

有谁知道为什么我得到两个不同的结果基本相同的代码行?

无论如何我能找到函数rtFilter()中数组的长度(分子)?

注意:我这里只使用4个元素的数组来测试程序。通常数组长度未知。
The following C-code implements a simple FIR filter:

//realtime filter demo

#include <stdio.h>
#include <stdlib.h>

//function defination
float rtFilter1(float *num, float *den, float *xPrev, float *yPrev);

void main() `main'' returns `int''!!!!!!!! {
float sig_in[30], numerator[3], denominator[3], x[3], y[2], s_out[30];
int i;
int kk; [snip]
// test
kk = sizeof(numerator)/sizeof(float);
This is valid, as `numerator'' is an array that''s in scope.
[snip]
}
//a(1)*y(n) = b(1)*x(n) + b(2)*x(n-1) + ... + b(nb+1)*x(n-nb) - a(2)*y(n-1) - ... - a(na+1)*y(n-na)

// realtime filter - direct form I - single input(xPrev[0]) / single output(sig_out)
float rtFilter1(float *num, float *den, float *xPrev, float* yPrev)
{
//positive sum
float psum = 0.0;
//negitive sum
float nsum = 0.0;

float sig_out;

//get length of numerator coefficient array
int numLen = sizeof(num)/sizeof(float);
In this case `num'' an argument has decayed into a pointer to the first
element of the array it has been passed; its size is lost. Hence the
value will always be the size of a pointer-to-float divided by the size
of a float. Not what you want. ;-(

[snip]
} //end rtFilter1

My problem is as follows:

The following code calculates the number of elements in an array(num):

int numLen = sizeof(num)/sizeof(float);

This method of finding array length is used in rtFilter, however it produces the wrong result. It produces the correct result when used in the main function though.

i.e.

// test
kk = sizeof(numerator)/sizeof(float);

Does anyone know why I am getting two different results for basicly the same line of code?

Is there anyway I can find the length of the array(numerator) inside the function rtFilter()?

Note: I''ve used arrays of 4 elements here just to test the program. Usually the array lengths are unknown.



你不能(直接)在C中传递数组。当你传递一个数组时,你确实会将指针传递给它的第一个元素。


请求请参阅 http://www.eskimo.com/~ scs / C-faq / s6.html 进一步

解释。


HTH,

--ag


-

Artie Gold - 德克萨斯州奥斯汀


You can''t (directly) pass arrays in C. When you do pass one, you''re
really passing a pointer to its first element.

Please see http://www.eskimo.com/~scs/C-faq/s6.html for further
explanations.

HTH,
--ag

--
Artie Gold -- Austin, Texas


这篇关于使用sizeof()的数组长度 - 问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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