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 wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to evaluate a string in an if-statement 2

Status
Not open for further replies.

weberm

Programmer
Dec 23, 2002
240
US
I'm something of a C++ novice and I have this legacy code C++ program I am attampting to update. Basically, this program scrapes the contents of an emulator, formats, and then sends the data to a specialized printer. I am attempting to check the value of a single-character field on the emulator to determine how to format the printer data and am running into problems.

Code:
static char wBillingType[2];

[blah blah assign value blah]

if (wBillingType == "T") {
   [blah blah]
}
else {
   [blah blah]
}
As I understand it, wBillingType has a length of two to accomodate some sort of terminating value ('/o', IIRC), but I'm obviously doing something wrong becuase the statement never evaluates as TRUE. It's possible I am not setting wBillingTYpe correctly, but I am able to display the value as a character followed by a low-hex value. Any ideas of the obvious solution I am missing?
 
Never mind, I found a solution elsewhere:
Code:
if (!stricmp(wBillingType,"T"))
 
Over 35 years ago this solution was found by a certain D.Ritchy...
Yet another (more effective;) solution:
Code:
if (*wBillingType == 'T')
 
Does *wBillingType work for wBillingType[2]? I remember in the dim and distant past that it only worked on some compilers. Got a feeling it doesn't work on AS400. I'd feel safer using
Code:
if (wBillingType[0] == 'T')
 
Ahhh, so using single quotes when referencing wBillingType[0] works! What is the difference between single and double quotes?
 
Or if you're feeling obscure
Code:
if (wBillingType[0] == "T"[0])

--
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
 
Single quotes for characters, double quotes for strings of characters
Code:
char sname[] = {'w', 'e', 'b', 'e', 'r', 'm', '\0'};
char dname[] = "weberm";
The contents of sname and dname are the same.
 
Builtin operator [] is defined as *(pointer+index) in C and C++. That's why it's commutative operator (see Salem's post). An array name implicitly converted in a pointer to the 1st element in all contexts except sizeof argument. That's why
Code:
*array == array[0] // array[0] == *(array+0)
and
array[0] == 0[array]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top