Thread: Incrementing pointers assigned to arrays

  1. #1
    Registered User
    Join Date
    Oct 2012
    Posts
    43

    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. #2
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Quote Originally Posted by Magi View Post
    Code:
        
        gPtr= &gallons;
        mPtr= &miles;
        nPtr= &mpg;
    Remove the '&'s here.

  3. #3
    Registered User
    Join Date
    Oct 2012
    Posts
    43
    I removed the '&'s and it still is just printing the calculation from the first location in the array.

  4. #4
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    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);
        }
    Fact - Beethoven wrote his first symphony in C

  5. #5
    Registered User
    Join Date
    Sep 2011
    Posts
    81
    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. #6
    Registered User
    Join Date
    Oct 2012
    Posts
    43
    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.

  7. #7
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    Post your code
    Fact - Beethoven wrote his first symphony in C

  8. #8
    Registered User
    Join Date
    Oct 2012
    Posts
    43
    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. #9
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    Code:
    for (i=0, nPtr=mpg; i<max; i++){   
        printf ("The mpg is %f\n", *nPtr++);
    Fact - Beethoven wrote his first symphony in C

  10. #10
    Registered User
    Join Date
    Oct 2012
    Posts
    43
    Thank you so much, that did the trick

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 01-24-2012, 11:28 PM
  2. incrementing character strings? pointers? HELP!
    By ominub in forum C Programming
    Replies: 10
    Last Post: 05-02-2009, 09:03 PM
  3. Passing pointers to arrays of char arrays
    By bobthebullet990 in forum C Programming
    Replies: 5
    Last Post: 03-31-2006, 05:31 AM
  4. Incrementing Pointers Along arrays
    By Red Force in forum C Programming
    Replies: 2
    Last Post: 04-16-2003, 06:45 PM
  5. incrementing pointers!?!
    By pmit2 in forum C Programming
    Replies: 2
    Last Post: 03-17-2002, 12:32 PM

Tags for this Thread