Thread: char and extern uses

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    569

    char and extern uses

    Say that I have defined :


    char tokenString[MAXTOKENLEN+1];

    in a file called test.h

    and I want to use the value tokenString[MAXTOKENLEN+1];

    in another file also, so far I tried:

    extern char* tokenString;

    but whenever I tried to run the code it always gives me a segmentation fault, can you guys help me out what's wrong here

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Variables in .h files = very very very very very very very bad.
    Put extern char tokenString[MAXTOKENLEN+1] in the header file, and include it where you need to see the variable.
    ONE .c file has char tokenString[MAXTOKENLEN+1] declared (without the extern).

  3. #3
    Registered User
    Join Date
    Jan 2008
    Posts
    569
    Quote Originally Posted by tabstop View Post
    Variables in .h files = very very very very very very very bad.
    Put extern char tokenString[MAXTOKENLEN+1] in the header file, and include it where you need to see the variable.
    ONE .c file has char tokenString[MAXTOKENLEN+1] declared (without the extern).
    sorry my mistake I did not write it in the .h file instead in the .c file, sorry .. can you clarify what you mean?

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You can have "extern char tokenString[MAXTOKENLEN+1]" as many times as you like.

    You can only have "char tokenString[MAXTOKENLEN+1]" once, and you must have it once. Note that the definition cannot be inside main(), it has to be at global scope.

  5. #5
    Registered User
    Join Date
    Jan 2008
    Posts
    569
    I cannot do extern char tokenString[MAXTOKENLEN+1] as maxtokenlen+1 is only defined once in that file, so does that mean that I need to have #define maxtokenlen+1 all over the place as well??

  6. #6
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    So define it in a header and include as required...

    constants.h (terrible name )
    Code:
    #ifndef INCLUDED_CONSTANTS_H
    #define INCLUDED_CONSTANTS_H
    
    /* probably best to include the +1 here... */
    #define MAXTOKENLEN 32
    
    #endif /* INCLUDED_CONSTANTS_H */
    header.h
    Code:
    #ifndef INCLUDED_HEADER_H
    #define INCLUDED_HEADER_H
    
    #include "constants.h"
    
    extern char tokenString[MAXTOKENLEN];
    
    #endif /* INCLUDED_HEADER_H */
    main.c
    Code:
    #include <stdio.h>
    
    #include "constants.h"
    #include "header.h"
    
    /* ... */
    
    char tokenString[MAXTOKENLEN];
    
    int main(void)
    {
       /* ... */
       return 0;
    }
    Of course there is nothing stoping you from defining MAXTOKENLEN and extern char tokenString[] in the same header.

  7. #7
    Registered User
    Join Date
    Jan 2008
    Posts
    569
    why can't I just define it as extern char* tokenString??

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Because it's not the same type. You're trying to tell the compiler there is an extern variable with a name and different type than there is.
    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.

  9. #9
    Registered User
    Join Date
    Jan 2008
    Posts
    569
    Isn't a char* abc basically the same as char abc []? sorry to ask such a dumb question.. I haven't programmed in C for a while

  10. #10
    Ex scientia vera
    Join Date
    Sep 2007
    Posts
    477
    Quote Originally Posted by -EquinoX- View Post
    Isn't a char* abc basically the same as char abc []? sorry to ask such a dumb question.. I haven't programmed in C for a while
    Yes and no. When you pass a character array to a function, it degenerates to a pointer. Even though char * and char [] is almost the same, it isn't completely, and to the compiler, it's not the same type.
    "What's up, Doc?"
    "'Up' is a relative concept. It has no intrinsic value."

  11. #11
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    In prototypes, they are the same:
    Code:
    void foo(int* x);
    void foo2(int x[]);
    Otherwise, they are not the same.
    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. Code review
    By Elysia in forum C++ Programming
    Replies: 71
    Last Post: 05-13-2008, 09:42 PM
  2. Fixing Errors
    By brietje698 in forum Networking/Device Communication
    Replies: 9
    Last Post: 12-10-2007, 11:17 PM
  3. Replies: 16
    Last Post: 10-29-2006, 05:04 AM
  4. Need help understanding info in a header file
    By hicpics in forum C Programming
    Replies: 8
    Last Post: 12-02-2005, 12:36 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