0

I am trying to make my code more professional by using pointers. I am using C . My goal is to use pointers as much as it is efficient . I have function which shifts filtered signal values. there are 4 filtering steps and each od them hold 3 values. filta ,filtb,filtc, filtd and there is new coming data which also needs to be shifted.

here is my working code . Very bulky indeed

float32_t*  FilterShift_inCapsulated( float32_t   xfilt1a_0,  float32_t  xfilt1a_1,  float32_t xfilt1a_2) 
{
    //values area loading normal way

    float32_t xfilt1a[3],shiftedvalues[15];
    float32_t*  shiftedvaluesP;

   xfilt1a[0]=  xfilt1a_0;  // i am sending them to arrays 
    xfilt1a[1]= xfilt1a_1;  //bc i want use them in "for" loop.
    xfilt1a[2]=xfilt1a_2;


int8_t ix = 0;
for (ix = 0; ix <2; ix ++)
{
    xfilt1a[ix] = xfilt1a[ix+1];   //index 1 of array shifts to 0
 }                                 //index 2 of array shifts to 1


    shiftedvalues[0]=xfilt1a[0];  //need to  take that values back
    shiftedvalues[1]=xfilt1a[1];   
    shiftedvalues[2]=xfilt1a[2];


    shiftedvaluesP=shiftedvalues // take the adress of the array and return it; 

return  shiftedvaluesP;
}

and here what i image to be correct way of it.

float32_t*  FilterShift_inCapsulatedwithPointer(float32_t*   xfilt1a) 
{
    //xfilt1a is an array . 

    float32_t filt1a[3],shiftedvalues[15];
    float32_t*  shiftedvaluesP;

 filt1a[0]= *xfilt1a;   // should i use itlike this   *(xfilt1a+1)  or like this *xfilt1a+1   
     filt1a[1]= *xfilt1a+1;
        filt1a[2]=*xfilt1a+2;


int8_t ix = 0;
for (ix = 0; ix <2; ix ++)
{
    filt1a[ix] = filt1a[ix+1];
// i want to avoid using real values here . can i use pointers here ?
//memcpy(destination, source, sizeof(source)); 
}


    shiftedvalues[0]=       filt1a[0];
    shiftedvalues[1]=   filt1a[1];
    shiftedvalues[2]=       filt1a[2];



    shiftedvaluesP=shiftedvalues ;

}
7
  • Your functions are declared to return something, but you never do that. And if you return shiftedvaluesP then it points to a local variable which will go out of scope and disappear immediately when you return. Commented Dec 19, 2017 at 13:53
  • so how should i return my values ? Commented Dec 19, 2017 at 13:59
  • Regarding the loop, look into memmove. Commented Dec 19, 2017 at 14:00
  • Pass shiftedvalues as an argument to the function. Commented Dec 19, 2017 at 14:00
  • Lastly, your question is really to broad. One question per question please. Commented Dec 19, 2017 at 14:01

1 Answer 1

2

Because of operator precedence the expression *xfilt1a+1 is equal to (*xfilt1a)+1. That is, it takes the value pointed to by xfilt1a (which is the very first value) and adds 1 to that.

You want to use *(xfilt1a+1) instead. Which is exactly equal to xfilt1a[1].

Sign up to request clarification or add additional context in comments.

1 Comment

thanks for reply; i edited my queastion. can you take a look at comment lines .

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.