Intialising Global data-type

Why does it give intializing error when i try to write tempe within function but
works fine when i try to make tempe global int

1
2
3
4
5
6
7
8
9
10
11
12
13
int tempe ;//does not give intialize error
  int GetParent (Tree*temp,Tree *prev,int elm)
{
	int tempe;//it give intialize error and will give wrong input if i //intialize it for example tempe=0
	if(temp!=NULL)
	{
		GetParent(temp->left,temp,elm);
		if(elm==temp->data)
			tempe=prev->data;
		GetParent(temp->Right,temp,elm);
	}
	return tempe;
}
Globals are guaranteed by the standard to be initialized.
Locals are not.

Hence, your global int tempe; is the same as int tempe = 0;.

However, your local int tempe; is not given any initial value -- and it is possible to terminate the function without assigning anything to it (that is, it is possible that your function does not evaluate line 9) hence your warning.

Fix it by explicitly initializing tempe.

Hope this helps.
Yes , it does thank you :)
Last edited on
I thought it was only statics which were guaranteed by the standard to be initialized. Though I suppose globals are nearly the same as statics.
Yeah, statics and globals really are, from a data storage POV, exactly the same thing. In both cases, some part of the program's data segment must have space for it. I think that's the reason why they both have that guarantee... I suppose it was just easy enough to require the compiler to fill zeroes in the executable image's data segment.

Personally, I don't think there's any reason to rely on the compiler doing initializations for you. Take the extra few finger presses to initialize properly, no matter what or where.
Topic archived. No new replies allowed.