Why "A"="" Is Not the Same as ""="A"Last reviewed: August 13, 1997Article ID: Q138206 |
The information in this article applies to:
SUMMARYWhen using the single equal sign (=) to test for a match between a string variable and an empty string, different results are obtained depending on which side of the equal sign the empty string ("") is placed. This is unlike string comparison routines that do not test first for length, such as the double equal sign (==) operator in Visual FoxPro.
MORE INFORMATIONThe code that follows is an example of how a string comparison routine might be written in the C programming language. Although this is not the exact code used by the architects of Visual FoxPro, it has been written in order to explain why a comparison between a string to an empty or null string might result in a true or false, depending on which side of the equal sign the null string appears.
#include <stdio.h> #include <string.h> int mycompare( char *, char * ); /* declare compare function */main() { char a1[]="Test", a2[]=""; /* data to compare */ /* Call the compare function */ printf("%c\n", mycompare(a1,a2)?'T':'F' ); /* Call the compare function, switching the addresses of the data */ printf("%c\n", mycompare(a2,a1)?'T':'F' ); } /* end of main program */ mycompare( char * c1, char * c2) { /* This is an example of C source code to compare two 'strings' */ /* in memory. The starting addresses of the two strings already */ /* in memory is passed to this function and assigned to local */ /* variables designated by "star" (meaning 'pointer to' or */ /* 'address of') c1 and "star" c2 */ /* Create a local variable to hold the length of the string, loop */ /* counter, and the result */ int iLen, iCounter, iResult; /* get the size of the string being compared to the primary string*/ iLen = strlen( c2 ); /* initially set the result to true (non-zero) */ iResult = 1; for ( iCounter=0; iCounter < iLen; iCounter++) /* A standard C 'for' loop. The first element of any C array is */ /* element zero, not element one, so you start the counter at zero.*/ /* Now, the array in memory actually contains the 'string' plus */ /* an additional ASCII zero terminating character. You must */ /* subtract one from the length to get the actual length of the */ /* string being compared, thus iLen-1. Even so, because the loop */ /* counter begins at zero and the strlen function returns the */ /* actual length of the string, you loop as long as the counter */ /* is less than the length. */ { if ( *(c1+iCounter) != *(c2+iCounter) ) if (iResult) iResult = 0; } /* This code goes through and compares both strings character */ /* by character until the end of string 2, pointed to by */ /* *(c2+iCounter, is reached. During this time, if the characters */ /* do not match and the result is not already false, the result */ /* is set to false. */ /* But what happens if iLen-1 is already zero because the string */ /* is empty? You never enter the loop and iResult remains set */ /* to 1 (true). This is returned by the function, thus */ /* "Test"="" is true. However, when you reverse the data, iLen-1 */ /* contains the length of the string being compared. You enter */ /* the For loop and iResult is set to false when comparing the */ /* first character. */ return iResult; } /* end of mycompare() function */ Keywords : FxprgGeneral vfoxwin kbcode kbcode kbhowto Platform : WINDOWS Issue type : kbhowto |
================================================================================
© 1998 Microsoft Corporation. All rights reserved. Terms of Use. |