Thread: Multiple Files and Struct

  1. #1
    Registered User
    Join Date
    Jul 2004
    Posts
    68

    Multiple Files and Struct

    Im currently trying to seperate my files to make it easier for myself later on. Anyway Im having some trouble with passing structs into functions that dont have the struct defined in its header

    Im compiling like this
    gcc *.c -o blackjack2 -W -Wall -ansi
    game.c:10: warning: `struct player' declared inside parameter list
    game.c:10: warning: its scope is only this definition or declaration, which is probably not what you want
    game.c: In function `game':
    game.c:10: warning: unused parameter `head'
    game.c: At top level:
    game.c:15: warning: `struct player' declared inside parameter list
    game.c: In function `makeBet':
    game.c:15: warning: unused parameter `head'
    game.c: At top level:
    game.c:20: warning: `struct player' declared inside parameter list
    game.c: In function `dealHand':
    game.c:20: warning: unused parameter `head'
    game.c:20: warning: unused parameter `dealerhand'
    game.c: In function `inHand':
    game.c:25: warning: unused parameter `hand'
    game.c: At top level:
    game.c:30: warning: `struct player' declared inside parameter list
    game.c: In function `memHandAllocate':
    game.c:30: warning: unused parameter `head'
    game.c:30: warning: unused parameter `dealerhand'
    game.c: At top level:
    game.c:35: warning: `struct player' declared inside parameter list
    game.c: In function `memHandFree':
    game.c:35: warning: unused parameter `head'
    game.c:35: warning: unused parameter `dealerhand'

    The last few errors about unused parameters in each function I am aware of and know why as the function isnt doing anything at the point in time.

    My game.h looks like this
    Code:
    int game(struct player *head);
    int makeBet(struct player *head);
    int dealHand(struct player *head, char *dealerhand[10]);
    
    int inHand(char *hand[10]);
    
    void memHandAllocate(struct player *head, char *dealerhand[10]);
    void memHandFree(struct player *head, char *dealerhand[10]);
    As you can see its just has function prototypes and nothing more. The file that contains the structure is player.h which also has a player.c file which makes it a Linked List eventually.

    player.h
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    #define NUMPLAYERS 3 /* Numplayers is computer players so 3 is, 3 computer opponents, dealer and user */
    
    /*
    
    	Player Structure
    
    */
    
    struct player {
    	char name[80];
    	char *hand[10];
    	int wins;
    	int draw; 
    	int lost;
    	float money;
    	float currentbet;
    	struct player *next;
    };
    
    /*
    
    		Player Prototypes
    
    */
    
    int initPlayers(struct player **head);
    int addNode(struct player **head, char *name);
    char *namelist(void);
    void getUsersName(char *name, size_t size);
    void listShow(struct player **head);
    int freeList(struct player **head);
    Now without using the game.h header file I get no errors so i know its the game.h / game.c errors.

    I then have a main file called blackjack2.c which calls the header files

    Code:
    #include <time.h>
    #include "deck.h"
    #include "player.h"
    #include "game.h"
    
    /* more code below */
    This file doesnt really do much other than include the files required for the program to work and use the int main() function to start the program.

    Now I am wondering how can I not get the errors I thought about just putting the functions from game.h/game.c into the player.h/player.c file (which works no problems) however my goal is to try and seperate my code as much as possible and this isnt working as well as Id hoped.

    Ive also tried including the player.h header in game.h with no luck, just the same errors.

    I am wondering is there a way to do this? Am I doing something wrong in the main file? Am I making any sense ?

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Why don't you just declare the structure in a header file?
    Code:
    /* someheader.h */
    struct foo
    {
        ...stuff...
    };
    Code:
    /* somefile.c */
    #include "somefile.h"
    
    ...stuff...
    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Jul 2004
    Posts
    68
    Ok seems i forgot to include "game.h" in game.c however that made a few more errors so I moved the include of player.h from the main file into game.h again now im just getting unused parameters which are the errors I suspect.

    I feel like such a doofus.

    Also answering your question the struct is already in a header file player.h and if I do the same in game.h i just get the redeclare structure player error.

    game.h
    Code:
    #include "player.h"
    
    int game(struct player *head);
    int makeBet(struct player *head);
    int dealHand(struct player *head, char *dealerhand[10]);
    
    int inHand(char *hand[10]);
    
    void memHandAllocate(struct player *head, char *dealerhand[10]);
    void memHandFree(struct player *head, char *dealerhand[10]);
    Main code
    Code:
    #include <time.h>
    #include "deck.h"
    #include "game.h"
    /* more code below */
    Now this prompts me to a hopefully much simpler question why does this work. Having the player.h file in game.h (and now including game.h in game.c) work? Wouldnt it be the same as if player.h was included before game.h in the main file?

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > Wouldnt it be the same as if player.h was included before game.h in the main file?
    Yes it would.

    Which way is preferable is a matter of debate. Some think that include files inside include files promotes muddle and confusion since it's a lot harder to work out all the true dependencies between modules.
    Others think that having to say include player.h in every single place (and there may be hundreds in large projects) before game.h is too laborious to contemplate, especially as the fix is easy.
    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.

  5. #5
    Registered User
    Join Date
    Jul 2004
    Posts
    68
    Thats one thing I want to avoid (includes of files included everywhere). Although it works now and I'll keep the include which Id rather not I havent found away as of yet for it to work which really confuses me I thought that was pretty simple logic to be all honest.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Global arrays shared between multiple source files
    By mike_g in forum C Programming
    Replies: 4
    Last Post: 08-14-2008, 06:29 PM
  2. How to read files into a struct
    By verbity in forum C Programming
    Replies: 16
    Last Post: 11-24-2006, 03:39 PM
  3. What's wrong with my search program?
    By sherwi in forum C Programming
    Replies: 5
    Last Post: 04-28-2006, 09:57 AM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. multiple source files
    By AmazingRando in forum C Programming
    Replies: 6
    Last Post: 03-13-2005, 03:39 PM