# Thread: Incrementing pointers assigned to arrays

1. ## Incrementing pointers assigned to arrays

Hello all. I am needing to do a mpg calculation using pointers in both the calculations and the print statement. I have the basic code set up but I am not sure how to set up the pointers so they go through the values assigned in the arrays and then print the received values stored in the mpg array. Help would be great

Code:
```#include <stdio.h>
int main (){
#define max 10
double miles [max] = {240.5, 300.0, 189.6, 310.6, 280.7, 216.9, 199.4, 160.3, 177.4, 192.3};
double gallons [max] = {10.3, 15.6, 9.7, 14, 16.3, 15.7, 14.9, 10.7, 9.3, 9.4};
double mpg [max];
double *gPtr, *mPtr, *nPtr;
int i;

gPtr= &gallons;
mPtr= &miles;
nPtr= &mpg;

for (i=0; i<max; i++){
*(nPtr)= *(mPtr)/ *(gPtr);
}

for (i=0; i<max; i++){

printf ("The mpg is %f\n", *nPtr);
}```

2. Originally Posted by Magi
Code:
```
gPtr= &gallons;
mPtr= &miles;
nPtr= &mpg;```
Remove the '&'s here.

3. I removed the '&'s and it still is just printing the calculation from the first location in the array.

4. You are not incrementing the pointers
Code:
```    for (i=0; i<max; i++, nPtr++, mPtr++, gPtr++)
{
*(nPtr)= *(mPtr)/ *(gPtr);
}

for (i=0; i<max; i++, nPtr++)
{
printf ("The mpg is %f\n", *nPtr);
}```

5. Not sure why you need to use those pointers... In your first for loop you assign the 10 mpg's to their respective place in the mpg array just by changing *nptr to mpg[i]. Same goes for the mptr and qptr except you use the i increment to acess their respective array elements. You would have to change the print for loop as well.

6. Technically I don't have to use the pointers but the assignment I'm trying to do says to use pointers. Also the increment of the pointers being placed within the for loop header leads to the print statement printing out the gallons array instead of the results from the mpg calculation.

8. Code:
```  #define max 10    double miles [max] = {240.5, 300.0, 189.6, 310.6, 280.7, 216.9, 199.4, 160.3, 177.4, 192.3};
double gallons [max] = {10.3, 15.6, 9.7, 14, 16.3, 15.7, 14.9, 10.7, 9.3, 9.4};
double mpg [max];
double *gPtr, *mPtr, *nPtr;
int i;

gPtr= gallons;
mPtr= miles;
nPtr= mpg;

for (i=0; i<max; i++){
*nPtr++= *mPtr++/ *gPtr++;
}

for (i=0; i<max; i++){
printf ("The mpg is %f\n", *nPtr);
}```

9. Code:
```for (i=0, nPtr=mpg; i<max; i++){
printf ("The mpg is %f\n", *nPtr++);```

10. Thank you so much, that did the trick