trouble with globals

M

merrittr

I define 2 variables (that I need to be globally accesible in all
files in this program)
in the file stack.c (functions push and pop):
//stack.c defnition if params
int iSp=0;
char *iItem[100];

But I also need to use iItem in a function stored in
strings.c (print which prints the stack)

void print(char *p)
{
printf("p\n");
int top=iSp-1,count;
for (count=0; count <= top; count++)
{
printf("print:%s\n",iItem[count]);
}
}


where/how do I declare iSp and iItem so all functions can access it?
See the command line
I use below to compile and the resulting errors.



$ gcc calculator.c stack.c strings.c math.c -o calculator.exe
..
calculator.c: In function `main':
calculator.c:70: error: `iItem' undeclared (first use in this
function)
calculator.c:70: error: (Each undeclared identifier is reported only
once
calculator.c:70: error: for each function it appears in.)
strings.c: In function `print':
strings.c:14: error: `iSp' undeclared (first use in this function)
strings.c:14: error: (Each undeclared identifier is reported only once
strings.c:14: error: for each function it appears in.)
strings.c:17: error: `iItem' undeclared (first use in this function)
strings.c: In function `printReverse':
strings.c:24: error: `iSp' undeclared (first use in this function)
strings.c:27: error: `iItem' undeclared (first use in this function)
 
F

Fred Kleinschmidt

merrittr said:
I define 2 variables (that I need to be globally accesible in all
files in this program)
in the file stack.c (functions push and pop):
//stack.c defnition if params
int iSp=0;
char *iItem[100];

But I also need to use iItem in a function stored in
strings.c (print which prints the stack)

void print(char *p)
{
printf("p\n");
int top=iSp-1,count;
for (count=0; count <= top; count++)
{
printf("print:%s\n",iItem[count]);
}
}


where/how do I declare iSp and iItem so all functions can access it?

declare them as extern in stack.h and #include stack.h in all of the .c
files.

Better yet, don't use them as globals. Use:

void print( char *p, char **iItem, int iSp )
{ ... }
 
C

Chris Dollin

merrittr said:
I define 2 variables (that I need to be globally accesible in all
files in this program)

[No, you don't need this.]
in the file stack.c (functions push and pop):
//stack.c defnition if params
where/how do I declare iSp and iItem so all functions can access it?

In a header file with (I suggest) the name `stack.h`, which you
#include into stack.c (do not even /think/ about not doing this)
and in other files that need it.

But /don't do that/. You'll end up with a whole bunch of global
variables smeared across your program, locking all the bits together
in a way that will have you cursing a few weeks down the line.

Wrap your stacky thingies into a struct. Put the struct declaration
(the /struct/ declaration, not a declaration for a /variable/ of
that type) into `stack.h`. Let's say it's `struct Stack`. Now you
can have a `struct Stack stack;` somewhere and pass it's /address/
to the places that need it. What's more, you can write useful
functions (like `push` and `pop` and `top` and `length` and `clear`
and `print`) which take a `struct Stack *stack` argument and do
their thing; put their definitions into `stack.c` and declarations
for them into `stack.h` (do not even think, etc).

This will save your sanity as soon as you want to have more than
one stack object, ie, as soon as you start writing your unit tests
for the stack, ie now.

It's even possible -- at some cost -- to arrange that /no one/ outside
`stack.c` knows what the insides of a `struct Stack` are, which means
that you can change them freely if you need to without having to
play tag across your entire program.

I will now descend from the soapbox and pass among the crowd.
 
C

Chris Dollin

DiegoFrei said:
I guess you should place your global variables in the main file.

Stop guessing and start thinking.

Putting his global variables in "the main file" (by which I take you
to mean the file that defines `main`) doesn't magically make them
visible inside other files. Surely you didn't think it did?
 
J

Joachim Schmitz

merrittr said:
I define 2 variables (that I need to be globally accesible in all
files in this program)
in the file stack.c (functions push and pop):
//stack.c defnition if params
int iSp=0;
If it's global, you don't need to initialize to 0, that's done for you
automagically
char *iItem[100];

But I also need to use iItem in a function stored in
strings.c (print which prints the stack)

void print(char *p)
{
printf("p\n");
int top=iSp-1,count;
for (count=0; count <= top; count++)
{
printf("print:%s\n",iItem[count]);
}
}


where/how do I declare iSp and iItem so all functions can access it?
best in a header files that all .c files using these variable #include, e.g.
globals.h
extern int iSP;
extern char *iItem[];

For gobal variables I'd recommend more descriptive names...
See the command line
I use below to compile and the resulting errors.



$ gcc calculator.c stack.c strings.c math.c -o calculator.exe
.
calculator.c: In function `main':
calculator.c:70: error: `iItem' undeclared (first use in this
function)
calculator.c:70: error: (Each undeclared identifier is reported only
once
calculator.c:70: error: for each function it appears in.)
strings.c: In function `print':
strings.c:14: error: `iSp' undeclared (first use in this function)
strings.c:14: error: (Each undeclared identifier is reported only once
strings.c:14: error: for each function it appears in.)
strings.c:17: error: `iItem' undeclared (first use in this function)
strings.c: In function `printReverse':
strings.c:24: error: `iSp' undeclared (first use in this function)
strings.c:27: error: `iItem' undeclared (first use in this function)

Bye, Jojo
 
W

wmaple

In the string.c, you can add a line at the start of this file as follows:
#include "stack.h"
And the same apply to all other files.
 
M

mark_bluemel

I define 2 variables (that I need to be globally accesible in all
files in this program)
in the file stack.c (functions push and pop):
//stack.c defnition if params
int iSp=0;
char *iItem[100];

But I also need to use iItem in a function stored in
strings.c (print which prints the stack) [Snip]

where/how do I declare iSp and iItem so all functions can access it?

Well, if you don't want any encapsulation, you could leave them where
they are in stack.c, but add appropriate external declarations in
strings.c (and presumably calculator.c which you haven't told us
anything about). Like this :-

extern int iSp;
extern char *iItem[];

Alternatively, you could use appropriate techniques to have the
stack.c file properly encapsulate the stack data and provide suitable
functions in stack.c to provide what the other files need. For
example, I can't see why your print() function is in strings.c (and by
the way, what should it be doing with its argument?).
 
C

Chris Dollin

wmaple said:
In the string.c, you can add a line at the start of this file as follows:
#include "stack.h"
And the same apply to all other files.

You left out the crucial step of writing `stack.h`; the OP might
have gained the impression that `stack.h` was language magic for
accessing declarations from another unit. (Not, after all, an
unreasonable thing for a language to do for you.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top