C pointer and address question

Discussion in 'Mac Programming' started by larswik, Dec 21, 2010.

  1. larswik macrumors 68000

    Joined:
    Sep 8, 2006
    #1
    I am learning about the 'arrays' in my book right now. To better understand it I wrote my own test code but I am having a problem. I know this goes back to pointers and variable addresses in memory. Using a function, I am trying to add the total of the dice rolls and return that value to the 'hold' variable. Here is the code.

    Code:
    #include <stdio.h>
    #include <time.h>
    #include <stdlib.h>
    
    int dice (void);
    int total (int rollNum, int *newValue);
    
    int main (int argc, const char * argv[]) {
    
    	srand(clock()); // seed for random num gen
    	
    	int rolls [13], i, hold; // create my variables
    	
    	hold = 0;
    	
    	for (i=0; i<=12; i++)  // loop till all the rolls have a number
    		rolls[i] = dice(); // put a random number into each array var
    	
    	i = 0; // I rest the counter to 0
    	
    	for (i=0; i<=12; i++) // This loop adds all the dice rolls and adds the value to hold
    		total (rolls[i], &hold);
    	
    	i = 0; // I rest the counter to 0
    	
    	for (i=0; i<=12; i++) // loop that displays each dice roll
    		printf("Array #[%d] hold a %d value\n\n", i, rolls[i]);
    	
    	printf("the total added amount is %d", hold); // prints total of dice rolls
    	
        return 0;
    }
    
    int dice (void) {        // Rolls a random 6 sided die
    return (rand() % 6) + 1;
    }
    
    int total (int rollNum, int *newValue){
    
    	&hold = rollNum + *newValue;
    }
    
    
    I would be thankful for any help in understanding.

    Thanks.
     
  2. larswik thread starter macrumors 68000

    Joined:
    Sep 8, 2006
    #2
    Never mind I figured it out. I needed to change my prototype to
    Code:
     int total (int rollNum, int *hold);
    
    and then the function to this

    Code:
    int total (int rollNum, int *hold){
    
    	*hold = rollNum + *hold;
    Thanks though, its nice to solve these alone when I can!

    Thanks!
     
  3. firestarter macrumors 603

    firestarter

    Joined:
    Dec 31, 2002
    Location:
    Green and pleasant land
    #3
    What were your compiler errors?

    There are probably other errors in it, but the most glaring problems I can see are in 'total'.

    You're declaring 'total' as returning an int, yet you're not... you're treating it as a function that updates 'hold' in place and doesn't return anything.

    'total' won't be able to understand what you mean by '&hold' - since the variable name 'hold' only exists in the scope of the 'main' function.

    This may be a better definition of 'total':

    void total (int rollNum, int *newValue){

    *newValue = rollNum + *newValue;
    }


    There might be other stuff wrong too... but it's past my bedtime so this will have to do.


    Edit: OK, you pretty much got it. But you should still change 'total' to return void - because it isn't returning an int.
     
  4. larswik thread starter macrumors 68000

    Joined:
    Sep 8, 2006
    #4
    It kept giving me a error at the bottom about that. I switched it to void and it went away. I think I need a better understanding of VOID vs. INT in this case.

    To my understanding VOID is a 1 way trip. I am sending something and not expecting a return value. But in this case I am sending something back to the 'hold' variable using pointers. Is that not returning a value? or is it because it is not returning a value to the Function call in main(), but sending the new value to another place outside that Function call, in this case a different variable?

    Thanks!
     
  5. firestarter macrumors 603

    firestarter

    Joined:
    Dec 31, 2002
    Location:
    Green and pleasant land
    #5
    You're not returning anything. You're using a handle/pointer to mess with something that you've been passed.

    If you were returning an int, then you'd use the 'return' keyword in the function to make that return. You'd then assign what total returned to a variable in main().
     
  6. lee1210, Dec 21, 2010
    Last edited: Dec 21, 2010

    lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #6
    Code:
    #include <stdio.h>
    #include <time.h>
    #include <stdlib.h>
    
    int dice (void);
    void total (int rollNum, int *newValue);
    
    int main (int argc, const char * argv[]) {
    
    	srand(clock()); // seed for random num gen
    	
    	int rolls [13], i, hold; // create my variables
    	
    	hold = 0;
    	
    	for (i=0; i<=12; i++) *{// loop till all the rolls have a number
    		rolls[i] = dice(); // put a random number into each array var
    		total (rolls[i], &hold); //add the value to hold
    		printf("Array #[%d] hold a %d value\n\n", i, rolls[i]);
    	}
    	
    	printf("the total added amount is %d", hold); // prints total of dice rolls
    	
    	return 0;
    }
    
    int dice (void) { * * * *// Rolls a random 6 sided die
    	return (rand() % 6) + 1;
    }
    
    void total (int rollNum, int *newValue){
    	*newValue += rollNum;
    }
    
    the return type of a function says what the type of the single value returned is (that you can, say, assign to a variable). Changing variables when passing pointers is not related to the return type. You can only return one value, you can modify as many arguments passed via pointer as you want.

    -Lee

    P.S. The range of int is not evenly divisible by 6 so that's not really a fair roll. Not a big deal, just FYI.
     
  7. larswik thread starter macrumors 68000

    Joined:
    Sep 8, 2006
    #7
    I understand that concept now.

    Thank you.

    -Lars
     

Share This Page