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!

Conditional declaration of a variable.

Status
Not open for further replies.

sjmolloy

Programmer
Aug 23, 2001
14
GB
Hi,
I am using Microsoft Visual C++ and I want to declare a variable and initialise it to zero the first time a function is called.
To this end I created a condition to check if it the first time through.
I tried to do the following:

if(first)
{
int j=0;
}

I then go on to use j BUT the compiler doesn't like this....
I am getting;
error C2065: 'j' : undeclared identifier

Is there a way around this or am I going about this totally wrong?
 
... youtfunction(........)
{
static int x = 0;//static variable retain its value
//between calls
x++;//ueach function call will increment once the variable
return ...
} Ion Filipski
1c.bmp


filipski@excite.com
 
if you declare it like that j's scope is only within the if statement. Do this

int j;

if (first)
{
j=0;
}


That way j will be in scope
 
And yes, JohnFill is right, you should use static vars if you want to keep function call counts
 
Hi,
Variables are only available within the set of curly braces there are declared. This is called the SCOPE of the variable.

therefore your code snipet....

if(first)
{
int j=0;
}


j is only available between the braces. if your code really looked like....

if(first)
{
int j=0;
}

j++;

you will get a j not declared because you are trying to to use j ourside the SCOPE that j is declared.

A variable can have multiple SCOPES.

func()
{
int j;

if(first)
{
int j=0;
j++
}

j += 5;

you would actually have 2 different variables called 'j'.

One whose scope is the WHOLE function except for the inside the if (first) and a sceond whose scope is just inside the if statement. j += 5 could be any random value becasue j in the Outer SCOPE has never been properly initialized.

This just causes confusion in most programs and should be avoided.

The above posts define how to declare j at the appropriate SCOPE.

func()
{
int j;

if(first)
{
j=0;
}

}

However, no where did I see where you had decalred first. first has the same issues as 'j'.

func()
{
BOOL first = 0;
int j;

if ( first )
{
j = 0;
first = FALSE;
}
.
.
.
}

The next time you come into the function 'first' will again be initialized to '0' and you will again initialize 'j' to 0. kind of a circular problem.

Now I supoose you could pass first in a paramter to func().

Therefore if you really only want to initialize 'j' to 0 the first time the function is called then use the STATIC declaration.

func()
{
static j = 0;

j ++;
.
.
.
}

This will initialize 'j' to 0 when the program starts and it retain the value in 'j' accross the function calls.

Now this works fine unless you are running multiple threads in your application in which case it is possible for 2 people to execute this code simultaneously in which case you need to use a locked Add to make sure only one thread bumps it a time or declare 'j' to be Thread Specific in which case each thread will get their own private copy of 'j'.



 
Thanks a million for the responses !
Have it cleared up now.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top