Thread: extern chars?

  1. #1
    Registered User
    Join Date
    Jul 2002
    Posts
    913

    extern chars?

    i want to be able to use some chars in different source files, how can i do this? extern char doesnt work for me(im using Dev C/C++ in XP).

  2. #2
    Unregistered
    Guest

    Re: extern chars?

    Originally posted by mart_man00
    i want to be able to use some chars in different source files, how can i do this? extern char doesnt work for me(im using Dev C/C++ in XP).
    In the _other_ source file, are these variables declared globally? If not, they should be.

  3. #3
    Registered User
    Join Date
    Jul 2002
    Posts
    913
    my files look like this

    main.c
    Code:
    extern chat input[255];
    
    int main(int argc, char *argv[]) {
    
    {
    other.c
    Code:
         if(input[0] = '\0')
    it doesnt see input, why?

  4. #4
    KingoftheWorld
    Guest
    Originally posted by mart_man00
    my files look like this

    main.c
    Code:
    extern chat input[255];
    
    int main(int argc, char *argv[]) {
    
    {
    other.c
    Code:
         if(input[0] = '\0')
    it doesnt see input, why?
    Where do you define the char input[] variable, it seem like you
    just declare the vairiable but not define????

    KingIII

  5. #5
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    In other.c (or preferably other.h), you must define your variable:
    >char input[255];

    Are you doing this?

    Also, when you compile, depending on how you #incude things, you may need to link the 2 modules together. EG:

    >gcc main.c other.c
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  6. #6
    Registered User
    Join Date
    Jul 2002
    Posts
    913
    so your suposed to delcare a variable twice? this doesnt overlap, id like this program to run in dos and linux.

  7. #7
    KingoftheWorld
    Guest
    Originally posted by mart_man00
    so your suposed to delcare a variable twice? this doesnt overlap, id like this program to run in dos and linux.
    One call 'define' a variable and another call 'declare' a variable.
    For example:

    file1.c file2.c
    ----------------------------- --------------------------------
    | //define a variable | | //declare a variable here
    | char input[255]; | | extern char input[255];
    | .......... | | //to access the char input[255]
    |________________ | | // in file1.c
    ------------------------------------

    KingIII

  8. #8
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,380
    >so your suposed to delcare a variable twice?

    You declare it once. The other times, you declare it as external. Assume I have a variable called var which I want to use in file1, file2 and file3, then I have to do it like this:

    file1:

    /* Here I declare my variable. */
    int var;


    file2:

    /* Here I tell that my variable is an external variable, in other words, it is declared in a different file. */
    extern int var;


    file3:

    /* Also here I tell that my variable is an external variable, in other words, it is declared in a different file. */
    extern int var;

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. extern const?Please help
    By huwan in forum C++ Programming
    Replies: 10
    Last Post: 08-12-2008, 04:53 AM
  2. extern classes
    By Swordsman in forum C++ Programming
    Replies: 1
    Last Post: 05-07-2008, 02:07 AM
  3. Replies: 16
    Last Post: 10-29-2006, 05:04 AM
  4. extern symbols, what do these mean exactly?
    By Shadow12345 in forum C++ Programming
    Replies: 8
    Last Post: 11-02-2002, 03:14 PM
  5. fancy strcpy
    By heat511 in forum C++ Programming
    Replies: 34
    Last Post: 05-01-2002, 04:29 PM