Thread: using a pointer to an integer in a for loop

  1. #1
    Registered User
    Join Date
    Apr 2019
    Posts
    808

    using a pointer to an integer in a for loop

    i have the following function.
    Code:
    void move_deck(int cards_in_play, Card deck[])
    {
        int i, j;
    
        do
        {
            for ( i = 0, j = cards_in_play ; j < 52 ; i++, j++ )
            {
                deck[i] = deck[j];
            }
            for ( ; i < 52 ; i++ )
            {
                deck[i].in_play = true;
            }
        } while (deck[0].in_play);
        print_deck(deck);
    }
    as it stands it works fine however it would be useful to have cards_in_play as a pointer but if i use the pointer in the for loop i get the usual warning about integer being turned into a pointer without cast,

    can i use a pointer as one of the controling paramaters of a for loop or do i have to assign the value its pointing at to another variable first

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Try this
    Code:
    void move_deck(int *cards_in_play, Card deck[])
    {
        int i, j;
     
        do
        {
            for ( i = 0, j = *cards_in_play ; j < 52 ; i++, j++ )
            {
                deck[i] = deck[j];
            }
            for ( ; i < 52 ; i++ )
            {
                deck[i].in_play = true;
            }
        } while (deck[0].in_play);
        print_deck(deck);
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Apr 2019
    Posts
    808
    im sure i tried that but cant of as it worked thank you

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 03-27-2014, 12:10 AM
  2. Character pointer and integer pointer
    By mohsen in forum C Programming
    Replies: 1
    Last Post: 07-11-2013, 08:09 AM
  3. Pointer to integer without a cast
    By javaeyes in forum C Programming
    Replies: 9
    Last Post: 03-11-2012, 10:01 AM
  4. comparison between pointer and integer
    By bazzano in forum C Programming
    Replies: 3
    Last Post: 03-07-2006, 01:15 PM
  5. pointer variable to integer?
    By Unregistered in forum C++ Programming
    Replies: 4
    Last Post: 01-18-2002, 05:06 PM

Tags for this Thread