×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Global Variable and Compiler issue

Global Variable and Compiler issue

Global Variable and Compiler issue

(OP)
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


 

RE: Global Variable and Compiler issue

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;
 

RE: Global Variable and Compiler issue

Just so we see EXACTLY what you're working with, please copy and paste your code with your question(s).

Lee

RE: Global Variable and Compiler issue

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);  

RE: Global Variable and Compiler issue

(OP)
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

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close