Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Global Variable and Compiler issue 1

Status
Not open for further replies.

toddyl

Technical User
Sep 26, 2005
102
US
Hi,

I have no knowledge of C but have been asked to modify a file to carry out a task. What I am trying to do is to use a global variable to replace a particular variable in a config file.

Under the include section I have added the following:

int globalvar;

In the main() section I have:

globalvar = 100;

and in the function block I have:

if (my criteria are met)
{
globalvar++;
char strBuf[16];
int len;

sprintf(strBuf, "%d", gcount);
len = strlen(strBuf);

When I try to compile this, using the Solaris cc compiler, I am getting the following error:

line 129: syntax error before or at: char
line 132: undefined symbol: strBuf
line 132: warning: improper pointer/integer combination: arg #1
undefined symbol: len

I have tried replacing the incrementing of globalvar with:

int localvar = globalvar + 1;

and this compiles fine and outputs the globalvar incremented by 1 for each instance. However I want to be able to increment the globalvar each time the condition is met so the number should increment over and over.

Can anyone please advise on what I am doing wrong.

Thanks,

toddyl


 
What's your "function block"?
If your function is in a separate file (module), you must declare globalvar as an external variable in this module:
Code:
extern int globalvar;
 
Just so we see EXACTLY what you're working with, please copy and paste your code with your question(s).

Lee
 
You're working in C: not C++. All declarations have to be before code.
Code:
    char strBuf[16];
    int len;
    /* No executable statements before this unless they are assignments */

    globalvar++;
    sprintf(strBuf, "%d", gcount);
    len = strlen(strBuf);
 
xwb,

Thanks for your help.
What you said corrected the problem and its working now.
Cant believe something so simple could cause all the issues I had.

Cheers,

toddyl
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top