473,320 Members | 1,854 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

trouble with globals

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)

May 14 '07 #1
8 1234
I guess you should place your global variables in the main file.

May 14 '07 #2
merrittr wrote:
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.

--
"Never ask that question!" Ambassador Kosh, /Babylon 5/

Hewlett-Packard Limited Cain Road, Bracknell, registered no:
registered office: Berks RG12 1HN 690597 England

May 14 '07 #3
"merrittr" <me******@gmail.comschrieb im Newsbeitrag
news:11**********************@k79g2000hse.googlegr oups.com...
>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
May 14 '07 #4

"merrittr" <me******@gmail.comwrote in message
news:11**********************@k79g2000hse.googlegr oups.com...
>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 )
{ ... }

--
Fred L. Kleinschmidt

May 14 '07 #5
DiegoFrei wrote:
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?

--
"Possibly you're not recalling some of his previous plans." Zoe, /Firefly/

Hewlett-Packard Limited registered no:
registered office: Cain Road, Bracknell, Berks RG12 1HN 690597 England

May 14 '07 #6
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.

"merrittr" <me******@gmail.com>
??????:11**********************@k79g2000hse.google groups.com...
>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)

May 14 '07 #7
On 14 May, 14:37, merrittr <merri...@gmail.comwrote:
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?).

May 14 '07 #8
wmaple wrote:
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.)

--
"How am I to understand if you won't teach me?" - Trippa, /Falling/

Hewlett-Packard Limited Cain Road, Bracknell, registered no:
registered office: Berks RG12 1HN 690597 England

May 14 '07 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Frostillicus | last post by:
I'm trying to use array_multisort to sort by one of the dimensions of an array stored in $GLOBALS like this: array_multisort($GLOBALS, SORT_STRING, SORT_DESC); Each "row" in $GLOBALS contains...
1
by: Sean Pinto | last post by:
Ok, you all are going to have to bear with me on this one as it is kinda complicated to explain. I am implementing a company management suite that requires Role-Based authentiations (ie. users are...
7
by: John | last post by:
Hi, I'm looking for the best way to deal with globals in PHP. As a 'C' software developer, I would normally avoid all globals and not have any at all, but use structs and pass everything in...
0
by: DPhelps | last post by:
I have a multithreaded Python shell server (base on the sample code 'pysvr.py') that uses a C-based extension class. The trouble I'm having is that I cannot figure out a way to create a Python...
27
by: ncf | last post by:
Hi all. In another topic, I was informed that I had to dynamically allocate memory instead of just trying to expand on a list. (I'm trying to learn C, and have a strong background in PHP and...
2
by: xml0x1a | last post by:
How do I use exec? Python 2.4.3 ---- from math import * G = 1 def d(): L = 1 exec "def f(x): return L + log(G) " in globals(), locals() f(1)
5
by: Steven W. Orr | last post by:
I have two seperate modules doing factory stuff which each have the similar function2: In the ds101 module, def DS101CLASS(mname,data): cname = mname+'DS101' msg_class = globals() msg =...
1
by: cokofreedom | last post by:
if __name__ == '__main__': print "Globals (For Loop):" try: for i in globals(): print "\t%s" % i except RuntimeError: print "Only some globals() printed\n" else: print "All globals()...
20
by: Ruud | last post by:
Hallo allemaal, I created a program where I opened a file to read from. For various reasons I had to split up this program in several modules. Variables declared in one module are made...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.