Thread: Increment a pointer variable

  1. #1
    Registered User
    Join Date
    Jul 2010
    Posts
    37

    Increment a pointer variable

    I am calling a function that contains an int pointer as an argument :

    function (int *p)

    How can I increase the pointer value by 1?

    *p++ is not working;

    Thank you.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,834
    That should work, though the dereference is redundant. ++p; should also work.

    ... unless you actually want to increment the object that the pointer is pointing to, in which case ++*p or (*p)++ would be correct.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jul 2010
    Posts
    37
    It doesn't seem to work.
    Here's the complete code list :
    Code:
    //Classifies a poker hand
    
    #include <stdio.h>
    #include <stdlib.h>
    #include <stdbool.h>
    
    #define NUM_RANKS 13
    #define NUM_SUITS 4
    #define NUM_CARDS 5
    
    //external variables
    
    
    
    //prototypes
    void read_cards(int a[], int b[]);
    void analyze_hand(int a[], int b[], bool *straight, bool *flush, bool *four, bool *three, int *pairs);
    void print_result(bool *straight, bool *flush, bool *four, bool *three, int *pairs);
    
    
    /***********************************************************
     * main: Calls read_cards, analyze_hand, and print_result  *
     *		 repeatedly.									   *
     ***********************************************************/
    int main (int argc, const char * argv[]) {
        
    	int num_in_rank[NUM_RANKS];
    	int num_in_suit[NUM_SUITS];
    	
    	bool straight, flush, four, three;
    	int pairs;	//can be 0, 1, or2
    	
    	
    	for (;;)
    	{
    		read_cards(num_in_rank, num_in_suit);
    		analyze_hand(num_in_rank, num_in_suit, &straight, &flush, &four, &three, &pairs);
    		print_result(&straight, &flush, &four, &three, &pairs);
    	}
    }//end main
    
    
    /***********************************************************
     * read_cards: Reads the cards into the external		   *
     *			   variables num_in_rank and num_in_suit;	   *
     *			   checks for bad cards and duplicate cards.   *
     ***********************************************************/
    void read_cards(int num_in_rank[], int num_in_suit[])
    {
    	bool card_exists[NUM_RANKS][NUM_SUITS];
    	char ch, rank_ch, suit_ch;
    	int rank, suit;
    	bool bad_card;
    	int cards_read = 0;
    	
    	for (rank = 0; rank < NUM_RANKS; rank++) {
    		num_in_rank[rank] = 0;
    		for (suit = 0; suit < NUM_SUITS; suit++) {
    			card_exists[rank][suit] = false;
    		}//end inner for
    	}//end outer for
    	
    	for (suit = 0; suit < NUM_SUITS; suit++)
    		num_in_suit[suit] = 0;
    	
    	while (cards_read < NUM_CARDS) {
    		bad_card = false;
    		
    		printf("Enter a card: ");
    		
    		rank_ch = getchar();
    		switch (rank_ch) {
    			case '0':	exit(EXIT_SUCCESS);
    			case '2':	rank = 0;	break;
    			case '3':	rank = 1;	break;
    			case '4':	rank = 2;	break;
    			case '5':	rank = 3;	break;
    			case '6':	rank = 4;	break;
    			case '7':	rank = 5;	break;
    			case '8':	rank = 6;	break;
    			case '9':	rank = 7;	break;
    			case 't': case 'T':	rank = 8;	break;
    			case 'j': case 'J':	rank = 9;	break;
    			case 'q': case 'Q': rank = 10;	break;
    			case 'k': case 'K': rank = 11;	break;
    			case 'a': case 'A': rank = 12;	break;
    			default:
    				bad_card = true;
    		}//end switch
    		
    		suit_ch = getchar();
    		switch (suit_ch) {
    			case 'c': case 'C':		suit = 0; break;
    			case 'd': case 'D':		suit = 1; break;
    			case 'h': case 'H':		suit = 2; break;
    			case 's': case 'S':		suit = 3; break;
    			default:
    				bad_card = true;
    		}//end switch
    		
    		while ( (ch = getchar()) != '\n')
    			if (ch != ' ') bad_card = true;
    		
    		
    		if (bad_card)
    			printf("Bad card; ignored.\n");
    		else if (card_exists[rank][suit])
    			printf("Duplicate card; ignored.\n");
    		else {
    			num_in_rank[rank]++;
    			num_in_suit[suit]++;
    			card_exists[rank][suit] = true;
    			cards_read++;
    		}
    
    	}//outer while that counts if we have reached 5 cards
    	
    }//end read_cards
    
    
    /******************************************************************
     * analyze_hand: Determines whether the hand contains a straight, *
     *				 a flush, four-of-a-kind, and/or three-of-a-kind; *
     *				 determines the number of pairs; stores the       *
     *				 results into the external variables straight,    *
     *				 flush, four, three, and pairs.					  *
     ******************************************************************/
    void analyze_hand(int num_in_rank[], int num_in_suit[], bool *straight, bool *flush, bool *four, bool *three, int *pairs)
    {
    	int num_consec = 0;
    	int rank, suit;
    	
    	*straight = false;
    	*flush = false;
    	*four = false;
    	*three = false;
    	*pairs = 0;
    	
    	//check for flush
    	for (suit = 0; suit < NUM_SUITS; suit++)
    	{
    		if (num_in_suit[suit] == NUM_CARDS)
    			*flush = true;
    	}
    	
    	//check for straight
    	rank = 0;
    	while (num_in_rank[rank] == 0) rank++;
    	for (; rank < NUM_RANKS && num_in_rank[rank] > 0; rank++)
    		num_consec++;
    	if (num_consec == NUM_CARDS)
    	{
    		*straight = true;
    		return;
    	}
    	
    	//check for 4-of-a-kind, 3-of-a-kind, and pairs
    	for (rank = 0; rank < NUM_RANKS; rank++) {
    		if (num_in_rank[rank] == 4) *four = true;
    		if (num_in_rank[rank] == 3) *three = true;
    		if (num_in_rank[rank] == 2) *pairs++;
    	}
    }//end analyze_hand
    
    
    
    /**********************************************************
     * print_result: Prints the classification of the hand,   *
     *				 based on the values of the external      *
     *				 variables straight, flush, four, three,  *
     *				 and pairs.								  *
     **********************************************************/
    void print_result(bool *straight, bool *flush, bool *four, bool *three, int *pairs)
    {
    	if (*straight && *flush) printf("Straight flush");
    	else if (*four)		   printf("Four of a kind");
    	else if (*three && *pairs == 1)	printf("Full house");
    	else if (*flush)		   printf("Flush");
    	else if (*straight)	   printf("Straight");
    	else if (*three)		   printf("Three of a kind");
    	else if (*pairs == 2)   printf("Two pairs");
    	else if (*pairs == 1)   printf("Pair");
    	else
    		printf("High card");
    	
    	printf("\n\n");
    }//end print_result
    If you run it and enter the following cards it should result a 'Pair' :
    8c, as, 7c, ad, 3h

  4. #4
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,907
    I assume the line in question is
    Code:
            if (num_in_rank[rank] == 2) *pairs++;
    In C, ++ has a higher precedence than the dereference (*) operation, so the ++ happens first. That statement basically says "increment the pointer, then dereference it". That means you make pairs point to some other place in memory (I think it's incremented by sizeof(int)), then you take the value at that new address and don't do anything with it. As a matter of fact, if you turn the warnings all the way up on your compiler you would get an error message like this:
    main.c: In function ‘analyze_hand’:
    main.c:159: warning: value computed is not used

    That tells you that the statement doesn't really have an effect. It's like saying
    Code:
    if (x == 10) {
        array[3];
    }
    which is grammatically correct, and will compile, but does nothing.

    As laserlight suggested, you want to increment the value that pairs points to, so use (*pairs)++.

    Hope that wasn't too long winded.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,834
    Quote Originally Posted by anduril462
    In C, ++ has a higher precedence than the dereference (*) operation, so the ++ happens first. That statement basically says "increment the pointer, then dereference it". That means you make pairs point to some other place in memory (I think it's incremented by sizeof(int)), then you take the value at that new address and don't do anything with it.
    Not quite: this is post-increment, so the dereference uses the original address.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    Jul 2010
    Posts
    37
    (*pairs)++ worked!
    Thanks a lot guys for your time and your clear explanations!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. returning a pointer to stack variable
    By justAlamer in forum C++ Programming
    Replies: 10
    Last Post: 03-09-2010, 08:41 AM
  2. Direct3D problem
    By cboard_member in forum Game Programming
    Replies: 10
    Last Post: 04-09-2006, 03:36 AM
  3. Could somebody please help me with this C program
    By brett73 in forum C Programming
    Replies: 6
    Last Post: 11-25-2004, 02:19 AM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  5. creating class, and linking files
    By JCK in forum C++ Programming
    Replies: 12
    Last Post: 12-08-2002, 02:45 PM