2 Replies - 1663 Views - Last Post: 19 May 2012 - 12:21 AM Rate Topic: -----

#1 erkant  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 108
  • Joined: 26-October 10

Using static array in function?

Posted 18 May 2012 - 10:25 AM

Well, let me tell you my situation shortly and see if you can help me. I have a program which is composed of about 20 functions. In one of the functions I'm doing some calculations and I'm storing them in a normal short int array. Then there goes about 10 more functions and after them I have another function that again makes some calculations and stores them in a normal short int array. Please note that both functions will store bits, meaning of 1s and 0s. And at the end somewhere I want to have another function which will check each element of the two arrays and compare whether they are equal or not, if not it will increment the counter and show the bit error rate. Therefore, I was planing to use static array for making the results stay permanent until the program runs, not function. But the problem is that I cannot use static variables/arrays as a function parameter, because I get this: error: storage class specified for parameter.

Therefore, I wanted to ask here, what can I do. Below you can find the last versions of the two functions, and my bit error rate calculation function. I simply need some method to remove those static keyword and instead using something else which will do the exact same thing.

void interleaver(short *input, short *output, static short *ber_input, int N_CBPS,int N_BPSC)
{
  int i;
  int t;
  int k;
  int s;
  short first_permutuation[N_CBPS - 1];

  for (k = 0; k < N_CBPS; k++)
  {
    i = (N_CBPS/16)*(k % 16) + (k/16);
    first_permutuation[i] = input[k];
  }

  s = fmax(N_BPSC/2,1);

  for(i = 0; i < N_CBPS;i++)
  {
    t = s*(i/s) + (i + N_CBPS - ((16*i)/N_CBPS)) % s;
    output[t] = first_permutuation[i];
    ber_input[t] = first_permutuation[i];
  }
}

// more functions are goes here, and they are executed

void BPSKdemapping(short* input, short* output, static short* ber_output, int nbits)
{
    int i;

    for(i = 0; i < nbits; i++)
    {
        if(input[i] == -1)
        {
            output[i] = 0;
            ber_output[i] = 0;
        }
        else
        {
            output[i] = 1;
            ber_output[i] = 1;
        }
    }
}

// then my bit error rate function comes

int ber(static short* input, static short* output, int nbits)
{
    int i, count = 0;

    for(i = 0; i < nbits; i++)
    {
        if(input[i] != output[i])
            count++;
    }

    return count;
}


This post has been edited by erkant: 18 May 2012 - 10:28 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Using static array in function?

#2 nuclearfroggy  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 82
  • View blog
  • Posts: 167
  • Joined: 04-August 08

Re: Using static array in function?

Posted 18 May 2012 - 10:44 AM

Surely you can just have the arrays in main() and pass them to every function that needs the array? These arrays would be around for the duration of the program.
Was This Post Helpful? 0
  • +
  • -

#3 turboscrew  Icon User is offline

  • D.I.C Addict

Reputation: 100
  • View blog
  • Posts: 644
  • Joined: 03-April 12

Re: Using static array in function?

Posted 19 May 2012 - 12:21 AM

Don't try to make the function parameters (formal parameters) static. It's enough if the arrays, used as actual parameters, are static.

In C/C++ arrays are special kinds of data: when you pass an array as parameter, the whole array is not pushed into the stack - just the pointer is.

BUT, if you pass a struct as a parameter, the whole struct is pushed.

This post has been edited by turboscrew: 19 May 2012 - 12:24 AM

Was This Post Helpful? 2
  • +
  • -

Page 1 of 1