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).
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.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).
my files look like this
main.c
other.cCode:extern chat input[255]; int main(int argc, char *argv[]) { {
it doesnt see input, why?Code:if(input[0] = '\0')
Where do you define the char input[] variable, it seem like youOriginally posted by mart_man00
my files look like this
main.c
other.cCode:extern chat input[255]; int main(int argc, char *argv[]) { {
it doesnt see input, why?Code:if(input[0] = '\0')
just declare the vairiable but not define????
KingIII
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]
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.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.
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
>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;