Warning in visibility.c

Discuss the future of Warzone 2100 with us.
Tribaldome
Greenhorn
Posts: 11
Joined: 28 Sep 2009, 17:16

Warning in visibility.c

Post by Tribaldome »

I got a compiler src\visibility.c(538) : warning C4706: In ANSI C, the result of an assignment expression is not an l-value. Therefore, the legal C++ expression (a += b) += c is illegal in C.

Code: Select all

	while (psViewer = gridIterate(), psViewer != NULL)
	{
		int val;

		// If we've got ranged line of sight...
		if (psViewer->type != OBJ_FEATURE 
			&& currVis[psViewer->player] < UBYTE_MAX
		    && (val = visibleObject(psViewer, psObj, false))) // C4706 - illegal in c, warning in c++
 		{
			// Tell system that this side can see this object
			currVis[psViewer->player] = val;
			if (prevVis[psViewer->player] < currVis[psViewer->player])
			{
				if (psObj->visible[psViewer->player] < val)
				{
					psObj->visible[psViewer->player] = val;
				}
				if(psObj->type != OBJ_FEATURE)
				{
					// features are not in the cluster system
					clustObjectSeen(psObj, psViewer);
				}
			}
 		}
	}
If you intend to make your test value the result of an assignment, test to ensure that the assignment is non-zero or not null. For example, the following code will not generate this warning:

Code: Select all

		// If we've got ranged line of sight...
		if (psViewer->type != OBJ_FEATURE 
			&& currVis[psViewer->player] < UBYTE_MAX
		    && (val = visibleObject(psViewer, psObj, false)) != NULL)
 		{
right?
Per
Warzone 2100 Team Member
Warzone 2100 Team Member
Posts: 3780
Joined: 03 Aug 2006, 19:39

Re: Warning in visibility.c

Post by Per »

That code looks right to me. visibleObject() returns an int, so checking != NULL against it would be rather bogus.

What compiler are you using? By the way, we are using C89, not strict ANSI C.
cybersphinx
Inactive
Inactive
Posts: 1695
Joined: 01 Sep 2006, 19:17

Re: Warning in visibility.c

Post by cybersphinx »

It is valid C, the warning is just about an assignment within a conditional (to catch errors where it should be a comparison).
Tribaldome
Greenhorn
Posts: 11
Joined: 28 Sep 2009, 17:16

Re: Warning in visibility.c

Post by Tribaldome »

oh yes, it should be more "!= 0" instead of "!= NULL".

i am using MSVC 9.0 with warning level 4 - http://msdn.microsoft.com/en-us/library ... 80%29.aspx

so in case of performance an lesser warnings about unclear code i managed it like the following. But maybe i should also ask if ((val = visibleObject(psViewer, psObj, false)) != 0)?

Code: Select all

		if (currVis[psViewer->player] < UBYTE_MAX)
 		{
			if (psViewer->type != OBJ_FEATURE)
 			{
				val = visibleObject(psViewer, psObj, false);
				// Tell system that this side can see this object
				currVis[psViewer->player] = val;
				if (prevVis[psViewer->player] < currVis[psViewer->player])
				{
					if (psObj->visible[psViewer->player] < val)
					{
						psObj->visible[psViewer->player] = val;
					}
					if(psObj->type != OBJ_FEATURE)
					{
						// features are not in the cluster system
						clustObjectSeen(psObj, psViewer);
					}
				}
			}
		}