Sign in to follow this  
Raduprv

Stupid C problem [solved]

Recommended Posts

Consider this: if(!knowledge_list[i].name)return 0;//doesn't work vs if(!knowledge_list[i].name[0])return 0;//works Any idea why the first doesn't work? [Edited by - Raduprv on June 9, 2005 10:47:05 PM]

Share this post


Link to post
Share on other sites
The first you're trying to convert a char array into a boolean. That isn't possible. In the second, however, you're trying to convert a number (a char, to be precise) into a boolean. This is possible (0 = false, anything else = true).

Share this post


Link to post
Share on other sites
if(!knowledge_list[i].name)return 0;//doesn't work

Will evaluate the address of the first element.
Since the address will be something other then 0(true), NOT'ed will be false.
To verify this do: if(!*(knowledge_list[i].name))return 0;//should work


if(!knowledge_list[i].name[0])return 0;//works

Will evaluate the content of the first element.
Here the content will allways be zero, NOT'ed will be true.

Share this post


Link to post
Share on other sites
Quote:
Original post by xor
if(!knowledge_list[i].name)return 0;//doesn't work

Will evaluate the address of the first element.
Since the address will be something other then 0(true), NOT'ed will be false.
To verify this do: if(!*(knowledge_list[i].name))return 0;//doesn't work


if(!knowledge_list[i].name[0])return 0;//works

Will evaluate the content of the first element.
Here the content will allways be zero, NOT'ed will be true.


You are right!
God, I really shouldn't program when I am tired. Wasted half an hour with this stupid thing.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this