Thread: Help with array def in different file

  1. #1
    Registered User
    Join Date
    Jan 2011
    Posts
    20

    Help with array def in different file

    I have a const char array defined in a .c file. I have a .h associated with the .c file included in my main file. Can i use that array in my main file? Do i have to define it in my main.c file and my other file(card.c)?

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    extern type arrayname[ ARRAYSIZE ];

    Put that in your header file, and include the header file wherever you want to use it.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    As long as the array is global in that .c file, copy the declaration to the .h file, and put the word "extern" in front of it:
    Code:
    extern const int array[10];
    Then, include that .h file in your main file, and you can access the array. Note that global variables are evil and should be avoided wherever possible.

  4. #4
    Registered User
    Join Date
    Jan 2011
    Posts
    20
    Thanks, I put extern in front of the definintion in the .h file and tried to compile. This is what i got, im not sure what it means.
    Code:
    In file included from Card.c:11:
    Card.h:17: warning: ‘ranks’ initialized and declared ‘extern’
    In file included from Main.c:8:
    Card.h:17: warning: ‘ranks’ initialized and declared ‘extern’
    /tmp/ccB8elgO.o:(.rodata+0x0): multiple definition of `ranks'
    /tmp/ccNOr1zb.o:(.rodata+0x0): first defined here
    collect2: ld returned 1 exit status
    I understand the first 4 lines are issuing warnings about using global variables, but i dont understand what the last 3 lines are saying.

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You need to recompile everything that has your .h included any time you change it.


    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    It's hard to say exactly what the problem is without your code, but here's a guess:

    Don't put initializers in the .h file. The header should have a declaration only. Put them in the .c file that has the definition. Read up on the difference between declaration here: Declaration vs definition. Prelude's comment in post #7 is a good, brief explanation.

    Example of how it should be:
    Code:
    /* card.c */
    int ranks[13] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13};
    ...
    
    /* card.h */
    extern int ranks[13];
    
    /* main.c */
    #include "card.h"
    // do something with ranks

  7. #7
    Registered User
    Join Date
    Jan 2011
    Posts
    20
    That worked, thank you.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can you help me about tolower() in file
    By nctar in forum C Programming
    Replies: 7
    Last Post: 05-12-2010, 10:04 AM
  2. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. Making a LIB file from a DEF file for a DLL
    By JMPACS in forum C++ Programming
    Replies: 0
    Last Post: 08-02-2003, 08:19 PM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM