Navigate through Array by Pointer to Pointer - pointer-to-pointer

I have tried to move through array by double pointer. Here is the Code.
void pptr (int **sptr2,int **ptr2)
{
**ptr2 = (**sptr2 + 7); //Works Fine
*sptr2++; *ptr2++; //Probable problem Statement
**ptr2 = (**sptr2 + 7); //Not Workign
}
void ppointer (int *sptr,int *ptr)
{
pptr (&sptr,&ptr);
}
main()
{
int sour[2];
sour[0] = 40;
sour[1] = 50;
int var[2];
var[0] = 10;
var[1] = 20;
printf("befor change %d %d\n",var[0],var[1]);
ppointer(&sour[0],&var[0]);
printf("pointer to pointer change %d %d\n",var[0],var[1]);
}
I wish to update var in pptr function. I can use double pointer (**sptr,**pptr) to reference pointer (sptr,ptr) (which are pointing to array) into function. I am able to update first one but second one has no change. I think problem is with statement *sptr++ & *ptr++.
Please help me understand how can i navigate through array by double pointer.
Thank you

I think it is the compiler just being silly with the "*sptr++;" and the "*ptr++;"
void pptr (int **sptr2, int **ptr2)
{
**ptr2 = (**sptr2 + 7);
///////////////////////////////////////////////////////
*sptr2++; //This is the problem these two statements
*ptr2++;
///////////////////////////////////////////////////////
**ptr2 = (**sptr2 + 7);
}
Now however if you change it to "*sptr2 += 1;" and "*ptr += 1;" it then works
void pptr (int **sptr2, int **ptr2)
{
**ptr2 = (**sptr2 + 7);
///////////////////////////////////////////////////////
*sptr2 += 1; //Now no problem
*ptr2 += 1;
///////////////////////////////////////////////////////
**ptr2 = (**sptr2 + 7);
}
I dont really know why the compiler does this due to lack of experience of using the "variable++" operator, I just generally use "variable += 1" instead.

Related

How I can solve this problem? "The argument type 'int?' can't be assigned to the parameter type 'num'." (Dart)

I'm trying to write a code which print the sum of odd and even numbers from a group of inputs, but it doesn't work and I think it's a null safety issue.
This is the code
void main() {
List numbers = [1, 2, 3, 4];
List odd = [];
List even = [];
for (int i = 0; i < numbers.length; i++) {
var z = numbers[i] % 2;
if (z == 0) {
even.add(numbers[i]);
} //end if
else {
odd.add(numbers[i]);
} //end else
} //end for loop
int sumOdd = 0;
int sumEven = 0;
for (int i = 0; i < odd.length; i++) {
sumOdd = sumOdd + odd[i];
}//end for
for (int i = 0; i < even.length; i++) {
sumEven = sumEven + even[i];
}//end for
print("sum of odds numbers = $sumOdd");
print("sum of even numbers = $sumEven");
} //end main
i think its because you not specify the type of list.
by default it will define as a list of number.
but int sumOdd = 0; you define as integer.
thats why you got error when run here : sumOdd = sumOdd + odd[i];
sumOdd is integer
odd[i] is number .
solution:
List<int> numbers = [1, 2, 3, 4];
List <int> even =[];
List<int> odd =[];
second solution
you can parse odd[i] to integer

double datatype behaving in an undefined manner

Even though I know turbo c is completely obsolete now, my instructor has put a condition to code in it.
I am having an issue that when I am trying to pass a double value to a function it is not behaving properly. I am getting fluctuating output several of the time, sometimes even weird. Let's see my code first:
#include<stdio.h>
#include<conio.h>
double func(double input) {
return input * input;
}
double simpson1By3(double initial, double final, double parts) {
double sum = 0;
double h = (final - initial) / parts;
double oddSum = 0, evenSum = 0;
int i;
printf("%f %f %f\n", initial, final, parts);
printf("%f\n", h);
printf("%f %f\n", evenSum, oddSum);
sum += func(initial) + func(final);
printf("%f %f\n", evenSum, oddSum);
for (i = 1; i < parts; i = i + 2) {
oddSum += func(initial + (i * h));
}
for (i = 2; i < parts; i = i + 2) {
evenSum += func(initial + (i * h));
}
oddSum *= 4;
evenSum *= 2;
printf("%f %f\n", evenSum, oddSum);
sum += evenSum + oddSum;
sum *= h / 3;
return sum;
}
int main() {
clrscr();
printf ("%f", simpson1By3(0, 6, 6));
getch();
return 0;
}
sample output:
What am I doing wrong there? Why are the arguments passed printed erroneously there along with other variables there and why is that -0 printing? Please help. I have tried finding something similar to it in forums but completely failed. Please help.

EXC_BAD_ACCESS on double array

writing an objective c (with c low level functionality) function to detect pitch. Getting a EXC_BAD_ACCESS code=2 on the line "w[i] = i;" in the first for loop. In our iteration, numberofSamples = 32768. The Error occurs in the first loop after i = 16332. For some reason after filling about half of the array, the pointer is no longer valid. Single threaded app, nothing else should be messing around with stuff while this is running. I was wondering if there were any fundamental errors in this setup that could cause this pointer error. I can provide more detail as requested. Thanks!
NSArray* getMainFrequency(NSMutableArray *input,int numberOfSamples){
const int log2n = log(numberOfSamples)/log(2);
const int n = 1 << log2n;
const int nOver2 = n / 2;
int sFreq=44100;
double fFreq=0,maxMag=0,realFreq=0,t,alpha=0.6;
double mag[n/2+1],dblInput[numberOfSamples],w[numberOfSamples];
FFTSetupD fftSetup = vDSP_create_fftsetupD (log2n, kFFTRadix2);
for(int i=0;i<numberOfSamples;i++){
dblInput[i]=[input[i] doubleValue];
w[i] = i;
}
DSPDoubleSplitComplex fft_data;
fft_data.realp = malloc(nOver2 * sizeof(double));
fft_data.imagp = malloc(nOver2 * sizeof(double));
vDSP_ctozD((DSPDoubleComplex *)dblInput, 2, &fft_data, 1, nOver2);
vDSP_fft_zripD (fftSetup, &fft_data, 1, log2n, kFFTDirection_Forward);
for (int i = 0; i < nOver2; ++i){
fft_data.realp[i] *= 0.5;
fft_data.imagp[i] *= 0.5;
}
for (int i = 48; i < 2974; ++i){
mag[i]=pow((pow(fft_data.realp[i],2)+pow(fft_data.imagp[i],2)),0.5);
}
for(int i=48;i <= 2974;i++){ //to get a max of 4000 hZ hopefully?
if (mag[i]>maxMag){
maxMag = mag[i];
fFreq = i;
}
}
realFreq=fFreq/(numberOfSamples/2+1)*sFreq/2;
return [NSArray arrayWithObjects: #(maxMag),#(realFreq),nil];
}
}

Get rid of these 3 Cocos2D analyzer warnings?

I hate warnings anywhere in my app so I fixed all of the other Cocos2D analyzer warnings except these three which I do not know how to solve.
Anyway, hopefully somewhat here can help me resolve these 3 warnings! The line with the warning is the one with the comment before it saying "This Line"
One Function call argument is an uninitialized value
-(void)update:(ccTime)time
{
int i, j;
Tile *tileArray = (Tile*)tiles;
for( i = 0; i < gridSize_.x; i++ )
{
for( j = 0; j < gridSize_.y; j++ )
{
//This Line tileArray->position = ccpMult( ccp(tileArray->delta.x, tileArray->delta.y), time);
[self placeTile:ccg(i,j) tile:*tileArray];
tileArray++;
}
}
}
Two The left operand of '*' is a garbage value
// color
p->color.r += (p->deltaColor.r * dt);
p->color.g += (p->deltaColor.g * dt);
//This Line p->color.b += (p->deltaColor.b * dt);
p->color.a += (p->deltaColor.a * dt);
Three Assigned value is garbage or undefined
void cc_pointerswap(void* a, void* b, size_t width)
{
void* tmp;
tmp = *(void**)a;
//This Line *(void**)a = *(void**)b;
*(void**)b = tmp;
}

Line scanning openCV

below is a very simple code segment, but I am not able to understand why it is cribbing, can you please tell me what the error means:
CvSize iSize;
iSize= cvGetSize(I1);
CvLineIterator *iter ;
CvPoint p1,p2;
long *arrH = new long[iSize.height + 1];
long *arrV = new long [iSize.width + 1];
for( int i=0; i<=iSize.height;i++)
{
p1.y = i; p2.y=i;
p1.x = 0; p2.x=iSize.width;
arrH[i] =0;
int l = cvInitLineIterator(I1,p1,p2,iter,4,0);
for( int j=0;j<l;j++)
{
arrH[i]+=iter.ptr;
CV_NEXT_LINE_POINT(iter);
}
fprintf(f1,"%d \n",arrH[i]);
}
Errors of the form:
left of '.ptr' must have class/struct/union
how do I tackle them ?
I think this:
CvLineIterator *iter ;
Should be:
CvLineIterator iter ;
And this:
cvInitLineIterator(I1,p1,p2,iter,4,0);
Should be:
cvInitLineIterator(I1,p1,p2,&iter,4,0);

Resources