Amiga.org
Operating System Specific Discussions => Amiga OS => Amiga OS -- Development => Topic started by: Jose on May 25, 2004, 08:23:12 PM
-
On the code at the bottom, what's the meaning of the ! operator in if(!LibBase)... ?
I tried to find it on some C book I have but ! seems to be the NOT operator. From what I understand the goal would be to evaluate the returned value of the function OpenLibrary and if not zero than the statements after If get done other wise the else part get's done (the basic If thing...).
But from the code below it's evaluating the negation of the
returned Libase ?! Obviously this doesn't make sense so I'm missing something here.
P.S. No, I'm still not learning C, I'm just reading some parts of the Rom Kernel Manuals on my DevCD and some of the examples are in C only...
struct Library *LibBase; /* Global: declare this above main() */
main()
{
LibBase = OpenLibrary("library.name",version);
if(!LibBase) { /* Library did not open, so exit */ }
else { /* Library opened, so use its functions */ }
}
-
If the library didn't open, LibBase would be NULL, which is zero, which is FALSE. If the library did open, it wouldn't be zero, which is TRUE. This all stems from the fact that C isn't type strict and doesn't have a boolean type. A more logical way to do the same thing would be to use:
if(LibBase != NULL)
Any compiler worth its salt will optimise this so that it's no less efficient than the first version.
-
Pah, the assembler version is more straightforward :-)
Since all is needed is to evaluate if LibBase is Null then why not just use:
if(LibBase) statement1 /*do stuff if Library suceesfully opened e.g. LibBase not zero*/
else statement2 /* exit.. the thing didn't open*/
What is that bloody exclamtion point doing in there? Is it still acting as NOT ?
-
My guess would be that if you did it your way, you would have something like this:
if (Libbase) { //start code here
... //5000 lines later
}
else { exit }
Its easier to read if you do the not case first... Otherwise you have a whole program and then you have an else clause just sitting there and you are wondering what it belongs to.
But what you said is correct, you COULD reverse the order. And yes, as explained in another message, the '!' is a NOT. Its just checking for a null pointer.
-Thorrin
-
Ahhh.. Ok I finally got it :-D
Cheers
-
Hmm, I came across something like this when I was writing some code which used the AmiSSL package... just checking that code now...
Ahh, probably not related, well could be, but when you initialise AmiSSL you're actually checking it like:
if(!InitAmiSSL(...))
{
/* worked */
}
else
{
/* didn't work */
}
Which is reverse logic of what you would expect most of the time when checking for a successful result.
Maybe some libraries work like this, most do not though. Strange.
-
And while coding C, why not write it like this instead?
if(!(LibBase = OpenLibrary("library.name",version))
{ ...
:-D
That's one thing I never really understood in C coding... WHY it seems to be so important to fit all possible commands to a single line? When compiled to assembler, it likely won't result in ANY less commands...
But if you write commands on separate lines, you can be more sure, that they're actually executed in right order...