Prev: Reference to an undefined assignment operator of a base class in a derived class.
Next: How to convert a Borland C++ App to Visual Studio App
From: Robby on 18 Dec 2009 13:07 Hello, I have asked this question in a previous post. I don't mean to double post, but I would simply like to ask the same question in a more direct manner. Please consider these .c modules: =====================KERNEL.h extern long MQ[]; =====================KERNEL.c long MQ[10] = {1,2,3,4,5,6,7,8,9,0}; // <<< at top of a particular .c file! =====================API.c #include KERNEL.h // <<< include KERNEL.h header in all .c files that need MQ[] void API_InsertMessage(enum enumKM m) { MQ[0] = m; } =========================== Objective: The data in the MQ array has to be accessible and available in many functions of several .c files, and therefore should appear to have global scope. Resolution: As shown in code above, I declared an array as "extern" and then defined it in one .c file and where ever other .c files require access to MQ[], I have to include the KERNEL.h file. Is this an okay practice to do with all other global variables, arrays and arrays of structures? P.S. I am only asking this to assure that I apply the same coding habits for the rest of my global variables. In all sincereity, thanks all for any feedback! -- Best regards Roberto
From: David Lowndes on 18 Dec 2009 13:34 >As shown in code above, I declared an array as "extern" and then defined it >in one .c file and where ever other .c files require access to MQ[], I have >to include the KERNEL.h file. Is this an okay practice to do with all other >global variables, arrays and arrays of structures? Yes. Dave
From: Robby on 18 Dec 2009 14:34 Thank you Dave! -- Best regards Roberto "David Lowndes" wrote: > >As shown in code above, I declared an array as "extern" and then defined it > >in one .c file and where ever other .c files require access to MQ[], I have > >to include the KERNEL.h file. Is this an okay practice to do with all other > >global variables, arrays and arrays of structures? > > Yes. > > Dave > . >
From: mzdude on 18 Dec 2009 16:54 On Dec 18, 1:07 pm, Robby <Ro...(a)discussions.microsoft.com> wrote: <snip> > Resolution: > As shown in code above, I declared an array as "extern" and then defined it > in one .c file and where ever other .c files require access to MQ[], I have > to include the KERNEL.h file. Is this an okay practice to do with all other > global variables, arrays and arrays of structures? > > P.S. I am only asking this to assure that I apply the same coding habits for > the rest of my global variables. > Having been bitten by global vars more than once in my life time, I would do something like === Kernal.h ===== long GetMessage(int msgIndex ); void SetMessage(int msgIndex, long message ); ==== Kernal.c === static long MQ[10] = {1,2,3,4,5,6,7,8,9,0}; long GetMessage(int msgIndex) { return MQ[msgIndex]; } // could add range check to index void SetMessage(int msgIndex, long message) { MQ[msgIndex] = message; } Just my preference. ==== Api.c ==== #include "Kernal.h" void API_InsertMessage(enum enumKM m) { SetMessage(0, m); } Now if you ever have to track down message changes, you have a single function in Kernal.c to set break points in, or add logging functions too.
From: Barry Schwarz on 19 Dec 2009 03:58
On Fri, 18 Dec 2009 18:34:12 +0000, David Lowndes <DavidL(a)example.invalid> wrote: >>As shown in code above, I declared an array as "extern" and then defined it >>in one .c file and where ever other .c files require access to MQ[], I have >>to include the KERNEL.h file. Is this an okay practice to do with all other >>global variables, arrays and arrays of structures? > >Yes. While this is obviously the right (tm) way to define a global variable exactly once and declare it extern everywhere it is needed, it begs the question of whether global variables are the right choice. Since they have a tendency to confound debugging, there is a school of thought that strongly recommends passing the array as an argument to those functions that need it. -- Remove del for email |