#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Location
    Bournemouth(U.K.)
    Posts
    24
    Rep Power
    0

    Using Arrays as function arguments


    Hi,

    I want to use an array
    Code:
    char cDeckofCards[NUM_CARDS][MAX_LENGTH]
    Where NUM_CARDS is 52 and MAX_LENGTH is 4.

    Basically, I need to use the array as an argument to a function. There will be no output from the function, so I have declared the function as
    Code:
    void initialDeal()
    The function accesses elements from the array in sequence to simulate dealing from the pack. I have included the array in the function prototype as follows:-
    Code:
    void initialDeal(char *cDeckofCards[MAX_LENGTH])
    When called from
    Code:
    main
    , i have used this:-

    Code:
    initialDeal(*cDeckofCards[])
    within the function I have used *cDeckofCards[] when accessing elements.

    And wish to use operators & and * to reference the array, and display results etc in main.

    Is this any where near how I would use an array as a function argument? The books I have looked at do not explain things very well!!!

    Any other Q's regarding my rather unhelpful way of explaining things, please ask!!

    Thanks
  2. #2
  3. I'm Baaaaaaack!
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Jul 2003
    Location
    Maryland
    Posts
    5,538
    Rep Power
    243
    Is this what you are looking for?

    Code:
    #define NUM_CARDS 52
    #define MAX_LENGTH 4
    
    
    //function prototype is changed:
    void initialDeal(char deckOcards[NUM_CARDS][MAX_LENGTH]);
    
    //function is called, no amperstand needed,
    //already a pointer to a 2d array:
    int main(){
        //oject declaration is unchanged:
        char cDeckofCards[NUM_CARDS][MAX_LENGTH];
    
        initialDeal(cDeckofCards);
    
        return 0;
    }
    
    //here is a do-nothing function...
    void initialDeal(char deckOcards[NUM_CARDS][MAX_LENGTH]){
        int i, j;
    
        for (i=0; i<NUM_CARDS; i++){
            for (j=0; j<MAX_LENGTH; j++){
                deckOcards[i][j] = i*j;
            }
        }
        return;
    }

    My blog, The Fount of Useless Information http://sol-biotech.com/wordpress/
    Free code: http://sol-biotech.com/code/.
    Secure Programming: http://sol-biotech.com/code/SecProgFAQ.html.
    Performance Programming: http://sol-biotech.com/code/PerformanceProgramming.html.
    LinkedIn Profile: http://www.linkedin.com/in/keithoxenrider

    It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it.
    --Me, I just made it up

    The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.
    --George Bernard Shaw
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Location
    Bournemouth(U.K.)
    Posts
    24
    Rep Power
    0
    Thank you ! That solved the problem initially.

    I am now getting a segmentation fault.
    The array - cDeckofCards[NUM_CARDS][MAX_LENGTH] has been declared in MAIN. Additionally all of the variables that are needed to control dealing, etc have also been declared in main. When I call the function from main, to deal the initial 2 cards i have used:-

    Code:
    initialDeal(cDeckofCards, iCardPoints, cFileDeck, iFilePoints, cUserHand, iUserHandPoints, cDealerHand, iDealerHandPoints,  iUserTotalPoints, iDealerTotalPoints, iUserCardNo, iDealerCardNo, iDealIndex);
    The function was prototyped as:-
    Code:
    void initialDeal(char cDeckofCards[NUM_CARDS][MAX_LENGTH], int iCardPoints[NUM_CARDS], char cFileDeck[NUM_CARDS][MAX_LENGTH], int iFilePoints[NUM_CARDS], char cUserHand[MAX_HAND][MAX_LENGTH],int iUserHandPoints[MAX_HAND], char cDealerHand[MAX_HAND][MAX_LENGTH], int iDealerHandPoints[MAX_HAND],  int *iUserTotalPoints, int *iDealerTotalPoints, int *iUserCardNo, int *iDealerCardNo, int *iDealIndex);
    Is the reason I can't access elements of the array using the variables declared in main something to do with how I declare them? No warnings are identified by the comiler, and it produces a .exe file as it should. The program runs until the function is called, then produces a segmentation fault. Is it to do with the pointers pointing to garbage which is too big for the array? Please help!!!
  6. #4
  7. I'm Baaaaaaack!
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Jul 2003
    Location
    Maryland
    Posts
    5,538
    Rep Power
    243
    I would have to look at your actual running code to be of any help. You can break lines for easier readabilty, the compiler ignores almost all cases of white space (with linefeeds considered as white space). When you use the code tags, the lines are not wrapped and it can be difficult to read your code (when you don't use the code tags, you lose all indentation, also making it difficult to read). As another design note, you can create structures to store your variable data, then just pass a pointer to the structure. That way you avoid global variables but don't have to have a million variables on your functions (it also makes maitenance a great deal easier, you only have to make changes in one place).

    My blog, The Fount of Useless Information http://sol-biotech.com/wordpress/
    Free code: http://sol-biotech.com/code/.
    Secure Programming: http://sol-biotech.com/code/SecProgFAQ.html.
    Performance Programming: http://sol-biotech.com/code/PerformanceProgramming.html.
    LinkedIn Profile: http://www.linkedin.com/in/keithoxenrider

    It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it.
    --Me, I just made it up

    The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.
    --George Bernard Shaw

IMN logo majestic logo threadwatch logo seochat tools logo