Not returning pointer

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

  1. larswik macrumors 68000

    Joined:
    Sep 8, 2006
    #1
    Hello again. I tried to figure this out for the last few hours before I came here. I like trying to solve the problems myself if I can. it's not returning the *valuePtr from the function to change the int value.

    I stripped this out of my larger program to find out what was going on in a separate xcode window. The Function Prototype has 2 parameters a pointer and a char. It passes the Char 'J' and the &value to a Switch that finds the case for 'J' and assigns an int value to i. i is then passes on to *value but it never make is back to int value in main(), it's still assigned to zero.

    Code:
    #include <stdio.h>
    
    void charToInt(int *valuePtr, char card);
    int main (int argc, const char * argv[]) {
    	
    	int value;
    	char mixDeck[2];
    	
    	mixDeck[0] = 'J';
    	mixDeck[1] = '\0';
    	value = 0;
    
    	charToInt(&value, mixDeck[0]);
    	printf("The value is %d\n", value);
    	printf("The value is %s", mixDeck);
    	
    	return 0;
    }
    	
    void charToInt(int *valuePtr, char card)
    {
    	int i;
    	
    	switch (card) {
    		case '2':
    			i = 2;
    			break;
    		case '3':
    			i = 3;
    			break;
    		case '4':
    			i = 4;
    			break;
    		case '5':
    			i = 5;
    			break;
    		case '6':
    			i = 6;
    			break;
    		case '7':
    			i = 7;
    			break;
    		case '8':
    			i = 8;
    			break;
    		case '9':
    			i = 9;
    			break;
    		case 'T':
    			i = 10;
    			break;
    		case 'J':
    			i = 10;
    			break;
    		case 'Q':
    			i = 10;
    			break;
    		case 'K':
    			i = 10;
    			break;
    		case 'A':
    			i = 11;
    			break;
    		default:
    			i = 9999;
    			break;
    			
    			*valuePtr = i;
    			
    	}
    }
    
    Thanks.

    -Lars
     
  2. jiminaus macrumors 65816

    jiminaus

    Joined:
    Dec 16, 2010
    Location:
    Sydney
    #2
    In code you posted, the *valuePtr = i; line is inside before the closing brace of the switch. Swap over this line and the line below it.
     
  3. larswik thread starter macrumors 68000

    Joined:
    Sep 8, 2006
    #3
    OMG - Thank you. I can't believe I over looked that. It's the little things I over look.

    Thanks again!!!

    -Lars
     
  4. jiminaus macrumors 65816

    jiminaus

    Joined:
    Dec 16, 2010
    Location:
    Sydney
    #4
    Using XCode's indentation as a hint

    It takes experience, but before long you'll start see this.

    I saw this error immediately because of the indentation. The line was aligned with the code in the switch's default "block". If XCode is automatically indenting your code, you use the indentation as a hint.

    I often forget a close square bracket when coding Objective-C. When XCode starts over-indenting my lines, I look back the last correctly indented line for an error.
     

Share This Page