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 ;
}
shiftedvaluesPthen it points to a local variable which will go out of scope and disappear immediately when you return.memmove.shiftedvaluesas an argument to the function.