Originally posted by FCF
Dear quzah,
Sorry to interrupt, i think i have to ask something.
Can u describe a bit the usage of "extern" in practical? I read my reference book and i can't understand clearly.
Thank u first for your help.
Sure. Let's assume that I have a table of items in a file:
Code:
struct vehicle {
char *name;
int weight;
int color;
float mpg;
} vehiclelist[] =
{
{ "Ferarri", 2000, COLOR_RED, 20.1 },
{ "Limo", 5000, COLOR_BLACK, 15.3 },
{ "Econo-car", 1200, COLOR_BLUE, 55.7 },
};
Ok, good enough. (If you're wondering, I declared a structure "struct vehicle", defined it, and followed that by creating a table (array) of these structures, called "vehiclelist")
Let's assume this list has a ton of entries.
Let's assume I'm using them in a progject that spans multiple .c files.
Now, I have one .c file called "output.c" that handles all of my "output" functions. This may be to disk, or perhaps to the screen, whatever. Anything considered output is all in this file.
Now, assuming I actually want to display any information on my vehicles, (which are contained in a file called "vehicle.c"--that has all of my vehicle related functions--getting speed, breaking, calculating impact damage, whatever), my 'output.c' file needs to know about this table.
Thus, in 'vehicle.c', the table is actually created, it exists there.
In 'output.c', I need a reference to that variable/table:
extern struct vehicle vehicletable[];
Now it can see the table.
Quzah.