• Welcome to Valhalla Legends Archive.
 

Error Checking For Null Strings/Characters

Started by Dyndrilliac, December 30, 2004, 04:24 PM

Previous topic - Next topic

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)

It's giving me the error here: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
}
}
}
Other Stuff: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);
}
Quote from: Edsger W. DijkstraIt is practically impossible to teach good programming to students that have had a prior exposure to BASIC; as potential programmers they are mentally mutilated beyond hope of regeneration.

Mephisto

#1
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...

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

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
}


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:

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

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.

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.
[19:20:23] (BotNet) <[vL]Kp> Any idiot can make a bot with CSB, and many do!

Mephisto

Quote from: Kp on December 30, 2004, 05:01 PM
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.

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.

Dyndrilliac

So I should do:bool IsNull(const char *source)
{
        if (!source == true) {
                return (!*source);
        } else {
                return (false);
        }
}
Is that right?
Quote from: Edsger W. DijkstraIt is practically impossible to teach good programming to students that have had a prior exposure to BASIC; as potential programmers they are mentally mutilated beyond hope of regeneration.

UserLoser.


#define IsNull(x) !*x


Sample:

char *Test = NULL;
printf("%i\n", IsNull(&Test)); // prints 1

...

char *Test = "hello";
printf("%i\n", IsNull(&Test)); // prints 0