Valhalla Legends Forums Archive | C/C++ Programming | Error Checking For Null Strings/Characters

AuthorMessageTime
Dyndrilliac
[quote]--------------------Configuration: Proj162 - Win32 Debug--------------------
Compiling...
Proj62.cpp
c:\documents and settings\owner\my documents\backup\source code\proj162\proj62.h(102) : error C2664: 'IsNull' : cannot convert parameter 1 from 'char' to 'char []'
        Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
Error executing cl.exe.

Proj62.obj - 1 error(s), 0 warning(s)[/quote]

It's giving me the error here:[code]for (x = 0;x < 3;x++) {
for (y = 0;y < 3;y++) { // run through all the board spaces..
if (IsNull(GameBoard[x][y]) == true) { // Error Here!
GameBoard[x][y] = ' '; // fill the free spaces with a null space
}
}
}[/code]Other Stuff:[code]char GameBoard[3][3];
bool IsNull(char szKey[]);

bool IsNull(char szKey[])
{
/*
This function checks to see if a string/character is null or not ("").
*/
bool Result;

if (szKey == NULL) {
Result = true;
} else {
Result = false;
}

return (Result);
}[/code]
December 30, 2004, 10:24 PM
Mephisto
You're getting that error because (as the compiler says) you're passing a character of your bidimensional array into the function, when it wants the array itself.  Besides that, your logic is wrong in two ways.  One, you're seeing if a character is NULL or not by passing a character into the function, when what you really want to know (based on your function description) is whether a string is NULL or not.  Secondly, you can't check to see if an array is NULL by using the comparison operator on it.  In fact, AFAIK there's really no such thing as a NULL array (in the context of assigning an array to a NULL value); but you can assign its elements to NULL values (such as the NULL terminator in a character array) and I suppose if all elements in your array are NULL your array is NULL, but eh?

Additionally, your logic is wrong in your loop iterations.  Why would you check to see if a character is NULL, and then assign it to a NULL value?  See below for possible fixes in your code...

[s]You shouldn't take a character array as your argument, rather a pointer to a character array.[/s]  You could do something like this:

[code]bool IsNull(const char *source)
{
    return !*source; // keep in mind this checks the value it points to, you'll need to do !source to check if it even points to a value or that if it's a valid pointer IIRC
}[/code]

Also, your IsNull function checks if a string is NULL or not, not whether a character is an empty one (I think of empty characters as ' ').  Just do this:

[code]if (GameBoard[x][y] == ' ') { // error here![/code]

However, this may not even be necessary.  But that's hard to say because I have no idea what you're trying to do by checking to see if a character is NULL or not, because after you check to see if it is, if it is in fact a NULL character, you're just reassining it to a NULL character.  And besides that, your loop is doing nothing.

I'm sure you could come up with other ways and perhaps there's better ways to do it that people will share with you.
December 30, 2004, 10:37 PM
Kp
Array names are just pointers to an array, so it's perfectly logical to ask if szKey == NULL, although it looks a bit odd.  Declaring a function to take char x[] is the same as taking char *x.  That said, checking for strlen(source) == 0 is rather silly too.  It's faster and equivalent to return !*source.
December 30, 2004, 11:01 PM
Mephisto
[quote author=Kp link=topic=10057.msg93895#msg93895 date=1104447686]
Array names are just pointers to an array, so it's perfectly logical to ask if szKey == NULL, although it looks a bit odd.  Declaring a function to take char x[] is the same as taking char *x.  That said, checking for strlen(source) == 0 is rather silly too.  It's faster and equivalent to return !*source.
[/quote]

Ah, didn't know you could treat char[] as char* that way, nor did I think of deferencing the value and returning the ! of it.  Thanks Kp.
December 31, 2004, 1:06 AM
Dyndrilliac
So I should do:[code]bool IsNull(const char *source)
{
        if (!source == true) {
                return (!*source);
        } else {
                return (false);
        }
}[/code]Is that right?
December 31, 2004, 3:23 AM
UserLoser.
[code]
#define IsNull(x) !*x
[/code]

Sample:
[code]
char *Test = NULL;
printf("%i\n", IsNull(&Test)); // prints 1
[/code]
...
[code]
char *Test = "hello";
printf("%i\n", IsNull(&Test)); // prints 0
[/code]
December 31, 2004, 5:53 AM

Search