Reply by Tim Wescott July 18, 20042004-07-18
Peter Choi wrote:

> Hello, > Ah I see. So if it is the in the "void main()" function and I have no > printf statements to make use of it or any other means of making use of it, > the compiler will generate a warning. But if it's in a function, then it is > okay. > > Now what is declaring something static? I guess I'd have to read my C book > to figure this out. Sorry I come from the Matlab world. We don't have to > do any declaration of anything. > > > "Tim Wescott" <tim@wescottnospamdesign.com> wrote in message > news:10flf604168k820@corp.supernews.com... > >>Yes, but y is never read, and aggressive compilers will figure this out. >> >>If you define y outside the function and don't declare it static the >>compiler won't know if you are planning on using it outside the file, so >>it won't optimize it away. > > > >
Declaring something static is a convenience for structured sorta-object-like programming in C: if you say static float y[20]; you're telling the compiler to make an array of 20 floats that is only to be referenced from that one file -- it won't export the name, so you wouldn't be able to reference y from another file. If you just say float y[20]; then the name 'y' is exported, and any other file can use it -- and it'll collide with any other thing that's global and named 'y'. For what you're doing DO NOT WORRY. Later it'll be an issue, but by then you won't be swimming in a sea of new information. -- Tim Wescott Wescott Design Services http://www.wescottdesign.com
Reply by Peter Choi July 18, 20042004-07-18
Hello,
Ah I see.  So if it is the in the "void main()" function and I have no
printf statements to make use of it or any other means of making use of it,
the compiler will generate a warning.  But if it's in a function, then it is
okay.

Now what is declaring something static?  I guess I'd have to read my C book
to figure this out.  Sorry I come from the Matlab world.  We don't have to
do any declaration of anything.


"Tim Wescott" <tim@wescottnospamdesign.com> wrote in message
news:10flf604168k820@corp.supernews.com...
> Yes, but y is never read, and aggressive compilers will figure this out. > > If you define y outside the function and don't declare it static the > compiler won't know if you are planning on using it outside the file, so > it won't optimize it away.
Reply by Tim Wescott July 18, 20042004-07-18
Peter Choi wrote:
> Hello, > > "Tim Wescott" <tim@wescottnospamdesign.com> wrote in message > news:10fl94bsms23972@corp.supernews.com... > >>In the case where it's in main you never do anything with y so the >>compiler tidily optimizes it away. > > Isn't the line " y[j] = temp;" doing something with y? > >
Yes, but y is never read, and aggressive compilers will figure this out. If you define y outside the function and don't declare it static the compiler won't know if you are planning on using it outside the file, so it won't optimize it away. -- Tim Wescott Wescott Design Services http://www.wescottdesign.com
Reply by Richard Dobson July 18, 20042004-07-18
But you don't read it anywhere. That is what the compiler means by "unused".
Try printing the contents of the array to the console using printf. Not much 
point in the program unless you can read the output!


Richard Dobson

Peter Choi wrote:

> Hello, > > "Tim Wescott" <tim@wescottnospamdesign.com> wrote in message > news:10fl94bsms23972@corp.supernews.com... > >>In the case where it's in main you never do anything with y so the >>compiler tidily optimizes it away. > > Isn't the line " y[j] = temp;" doing something with y? > >
Reply by Peter Choi July 18, 20042004-07-18
Hello,

"Tim Wescott" <tim@wescottnospamdesign.com> wrote in message
news:10fl94bsms23972@corp.supernews.com...
> In the case where it's in main you never do anything with y so the > compiler tidily optimizes it away.
Isn't the line " y[j] = temp;" doing something with y?
Reply by Tim Wescott July 18, 20042004-07-18
Peter Choi wrote:

> Hello, > > Just trying my first program. It's a FIR filter below. For some reason, if > it is put in "void main()" without the "return y" line, it spits out a > compile warning on the line "float y[20]" saying the variable y was never > referenced. If I put it into its own function, "float fir()", and then call > it from "void main()", it produces a compile error on the line "return y" > saying "return value type does not match the function type" > > int i, j; > int datapoints = 10, length = 10; > float temp; > float x_d[10] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; > float y[20]; > int taps[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; > float x[10] = {0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1}; > > for (j=0;j<(datapoints-1);j++) > { > x_d[0] = x[j]; > temp = 0; > for (i=(length-1);i>0;i--) > { > temp = temp + x_d[i] * taps[i]; > if (i != 0) > x_d[i] = x_d[i-1]; > } > y[j] = temp; > } > return y; > >
In the case where it's in main you never do anything with y so the compiler tidily optimizes it away. In the case of your function you're returning an array of floats as a float. You need to (a) create y outside of your function and pass it in as a pointer & modify the parts, and (b) re-check your basic C programming skills. -- Tim Wescott Wescott Design Services http://www.wescottdesign.com
Reply by Peter Choi July 18, 20042004-07-18
Hello,

Just trying my first program.  It's a FIR filter below.  For some reason, if
it is put in "void main()" without the "return y" line, it spits out a
compile warning on the line "float y[20]" saying the variable y was never
referenced.  If I put it into its own function, "float fir()", and then call
it from "void main()", it produces a compile error on the line "return y"
saying "return value type does not match the function type"

 int i, j;
 int datapoints = 10, length = 10;
 float temp;
 float x_d[10] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
 float y[20];
 int taps[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
 float x[10] = {0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1};

 for (j=0;j<(datapoints-1);j++)
 {
  x_d[0] = x[j];
  temp = 0;
  for (i=(length-1);i>0;i--)
  {
   temp = temp + x_d[i] * taps[i];
   if (i != 0)
    x_d[i] = x_d[i-1];
  }
  y[j] = temp;
 }
 return y;