CompuServe Messages

#Strings in C

    27-Nov-94 09:41:50
Sb: #44297-#Strings in C
Fm: Peter Wade 100265,2740
To: Shraddhan 70374,754
If I understand you rightly, you only have struct Atom { char *_Name; APTR atom_Value; APTR atom_Next; }; in the first file. Am I correct ? If this is the case then that is your problem. The compiler treats each .c file seperatly, it does not remember the declaration of struct Atom when it is on the second file. The line : extern struct Atom Object[] ; tells it that Object is an external array of struct Atom but it still does not know what a struct Atom actually is. The way round this is to take your "struct Atom { …" declaration out of the first file and put it into a header file, e.g. atom.h, then put the line: #include "atom.h" at the tope of both of your .c files. Keep the "struct Atom Object[] …" definition in your first file and use the line: extern struct Atom Object[]; in your second file. > Why won't the compiler (DICE) recognise that the array referred to in > one file is defined in the other file? C compilers treat each .c file they compile as a seperate operation even if they are on the same command line. That is why you have to #include all of the system header files into each file that uses them. You can use extern to tell it about an external variable which is a data type it already knows, but you have to include the structure declarations for any structures that you use. N.B. The difference between declaring a variable and defining it is that defining it creates the variable and allocates memory for it but declaring it just says what type it is. So : struct Atom Object[1000] ; /* This is a definition */ extern struct Atom Object[] ; /* This is a declaration */ This is worth remembering if you are reading textbooks on the subject. Well, that is probably longer than you were expecting. I hope it is of help. Happy Programming! Peter Wade Autopiloting from London, England