Newbie Question: Split Programs in to multiple files.

Discussion in 'C Programming' started by William E. Storey II, May 22, 2005.

  1. Hello All,

    I am new to C programming or Procedural programming for that matter. I
    got my start in the programming world with .NET. I have decided to move
    to a real programming language that is not so bloated.

    My problem is that I am trying to break up my program in to seperate
    files for maintainability purposes. However I cant seem to get it to
    work. The project is a simple console blackjack game. I have the
    following files,

    - main.c
    - player.c
    - dealer.c
    - deck.c
    - play.c

    I did not see the need for a library since I did not really plan on
    reusing the code ( except for maybee the shuffle function ). So what I
    was trying to do was just to break the modules up in to seperate files
    that I could just use the includes pre processor directive in order to
    combine them.

    Here is where my problem is though. In the Player, Dealer, and Play
    modules. I need to use the deck types however since the compiler needs
    things to be in a top to bottom order I was not able to include the
    files in a way that would appease the compiler. So I tried to switch to
    the library Idea.

    So I made deck.o, player.o, play.o and dealer.o. Howerver when I mse
    the player.o, play.o and dealer.o I tried to incldue the deck.h header
    file so that they could be used in the library. But I got a conflicting
    types error. So I removed the deck header file and then I just got a
    variable not declared error

    I am at a loss and was wondering if you guys and any advice on how to
    solve this problem. I am not sure if I need to restructure the project
    or not. It just seems to me unneccesary to use librarys, but since i
    can not do that in a way that will compile right I dont see any other
    method. I would love to hear your input and ideas on this topic.

    Also when designing, a library what goes in the header file and what
    goes in the c file? For instance if I made a library called deck.
    would I declare the deck structure in the header, c file, or both?

    If you need anymore information in order to help me or would like to see
    the source code that I have please feel free to email me at
    .

    Thanks Again for any Help Offered.

    William E. Storey II
     
    William E. Storey II, May 22, 2005
    #1
    1. Advertising

  2. William E. Storey II

    Michael Mair Guest

    William E. Storey II wrote:
    > Hello All,
    >
    > I am new to C programming or Procedural programming for that matter. I
    > got my start in the programming world with .NET. I have decided to move
    > to a real programming language that is not so bloated.
    >
    > My problem is that I am trying to break up my program in to seperate
    > files for maintainability purposes. However I cant seem to get it to
    > work. The project is a simple console blackjack game. I have the
    > following files,
    >
    > - main.c
    > - player.c
    > - dealer.c
    > - deck.c
    > - play.c
    >
    > I did not see the need for a library since I did not really plan on
    > reusing the code ( except for maybee the shuffle function ). So what I
    > was trying to do was just to break the modules up in to seperate files
    > that I could just use the includes pre processor directive in order to
    > combine them.
    >
    > Here is where my problem is though. In the Player, Dealer, and Play
    > modules. I need to use the deck types however since the compiler needs
    > things to be in a top to bottom order I was not able to include the
    > files in a way that would appease the compiler. So I tried to switch to
    > the library Idea.
    >
    > So I made deck.o, player.o, play.o and dealer.o. Howerver when I mse
    > the player.o, play.o and dealer.o I tried to incldue the deck.h header
    > file so that they could be used in the library. But I got a conflicting
    > types error. So I removed the deck header file and then I just got a
    > variable not declared error
    >
    > I am at a loss and was wondering if you guys and any advice on how to
    > solve this problem. I am not sure if I need to restructure the project
    > or not. It just seems to me unneccesary to use librarys, but since i
    > can not do that in a way that will compile right I dont see any other
    > method. I would love to hear your input and ideas on this topic.
    >
    > Also when designing, a library what goes in the header file and what
    > goes in the c file? For instance if I made a library called deck.
    > would I declare the deck structure in the header, c file, or both?
    >
    > If you need anymore information in order to help me or would like to see
    > the source code that I have please feel free to email me at
    > .


    I am not exactly sure where your conceptual problem is; it would help,
    though, if you would give us a short overview over the data structures
    and interfaces you are using.

    e.g.
    --- deck.h ---
    #ifndef DECK_H
    #define DECK_H

    typedef struct {
    /* describe card */
    } card;

    typedef struct {
    /* Here goes your representation of the deck */
    } deck;

    int shuffle (deck *my_Deck);
    card draw (deck *my_Deck);
    .....

    #endif
    --- deck.c ---
    .....
    #include <stdlib.h>
    .....
    #include "deck.h"
    .....
    /* return 0 on success */
    int shuffle (deck *my_Deck)
    {
    .....
    }
    .....
    --- dealer.h ---
    #ifndef DEALER_H
    #define DEALER_H

    #include <stddef.h>
    #include "deck.h"
    #include "player.h"

    typedef player dealer;

    int deal (deck *my_Deck, dealer *me, player **playerlist,
    size_t numplayers);

    #endif
    --- main.c ---

    #include "deck.h"
    #include "player.h"
    #include "dealer.h"
    #include "play.h"

    int main (int argc, char **argv)
    {
    /* retrieve number of players from command line or stdin */
    ....
    /* init players */
    ....
    /* init shuffle rng */
    ....
    /* Play until the user does not want to anymore */
    ....
    play(....);
    ....
    /* Cleanup */
    }
    -----

    Maybe the inclusion guards (#ifndef...) are what you were looking
    for.


    Cheers
    Michael
    --
    E-Mail: Mine is an /at/ gmx /dot/ de address.
     
    Michael Mair, May 22, 2005
    #2
    1. Advertising

  3. William E. Storey II

    CBFalconer Guest

    "William E. Storey II" wrote:
    >
    > I am new to C programming or Procedural programming for that matter.
    > I got my start in the programming world with .NET. I have decided to
    > move to a real programming language that is not so bloated.
    >
    > My problem is that I am trying to break up my program in to seperate
    > files for maintainability purposes. However I cant seem to get it
    > to work. The project is a simple console blackjack game. I have the
    > following files,
    >
    > - main.c
    > - player.c
    > - dealer.c
    > - deck.c
    > - play.c

    .... snip ...

    For each of those files, decide what file level entities within the
    file are to be visible outside. Then mark all others as static.
    Then create a filename.h file, which specifies the prototypes for
    the externally visible functions, and declares the externally
    visible data as "extern". #include "filename.h" in filename.c, so
    the compiler can check agreement with the actual situation.

    Whenever otherfile.c needs access to something in filename.c,
    #include "filename.h" in otherfile.c

    If a type is needed, such as a struct definition, or an enum, or a
    common #define, put that in the appropriate header file. But never
    declare data items in headers.

    The more things you mark static, the better organized your program
    actually is.

    --
    Some informative links:
    news:news.announce.newusers
    http://www.geocities.com/nnqweb/
    http://www.catb.org/~esr/faqs/smart-questions.html
    http://www.caliburn.nl/topposting.html
    http://www.netmeister.org/news/learn2quote.html
     
    CBFalconer, May 23, 2005
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    12
    Views:
    1,681
    Dave Thompson
    Jan 10, 2005
  2. Replies:
    4
    Views:
    984
    M.E.Farmer
    Feb 13, 2005
  3. Coca
    Replies:
    7
    Views:
    763
    Aidan Grey
    Aug 24, 2004
  4. Replies:
    18
    Views:
    643
    Dave Thompson
    Jan 10, 2005
  5. lone_eagle
    Replies:
    3
    Views:
    659
    psykeedelik
    May 26, 2009
Loading...

Share This Page