Thread: call function

  1. #1
    Registered User
    Join Date
    Oct 2009
    Posts
    46

    call function

    this funciton initialized as:

    Code:
    void initCards(int *playerHand[], int *dealerHand[], int numOfCards)
    is it wrong if I call it this way?

    Code:
    main() {
        int dealer[NUM_CARDS], player[NUM_CARDS];  // NUM_CARDS is defined as 21
        int *dealerHand = &dealer[0];
        int *playerHand = &player[0];
        numOfCards = numOfDecks * 52; // numOfDecks is found earlier in main and 
                                                           // it's 2 in this case
    
        initCards(&playerHand, &dealerHand, numOfCards);

    I'm getting a bus error right when this function is called. What is wrong? I debugged it with printf statements and right at the function call it returns a bus error.
    in initCards() I have the first line being a printf statement and that's not showing... so the bus error come before anything inside iniCards() is done.

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    "int *playerHand[]" refers to an array of pointers. Niether int *playerhand nor &playerhand is that.

    I would recommend you change the prototype to:
    Code:
    void initCards(int *playerHand, int *dealerHand, int numOfCards)
    That could be a pointer to an int array. An array name is a pointer to it, so you can now forget about playerHand and dealerHand in main() and just pass this:
    Code:
    initCards(player, dealer, numOfCards);
    "playerHand" and "dealerHand" inside initCards will refer to the arrays correctly. Simple!
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Just for reference:
    &dealerHand is int** (pointer to pointer).
    &dealer is int*[] (pointer to array).

    Also remember that you can simply use the array name wherever you see fit to attain a pointer to the first element of the array for free. You don't have to do it as a separate step.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  2. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  3. Troubleshooting Input Function
    By SiliconHobo in forum C Programming
    Replies: 14
    Last Post: 12-05-2007, 07:18 AM
  4. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  5. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM