Sign in to follow this  
Raduprv

Stupid C problem [solved]

Recommended Posts

Raduprv    997
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
I could be wrong, but, I think the first checks if the pointer value of name is == 0, which since its created on the stack means its probably not true... the second version checks if the first element is 0 which would be true if you zerod out the array.

Share this post


Link to post
Share on other sites
Catafriggm    296
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
Raduprv    997
Hmm
I also tried: if(!&knowledge_list[i].name)return 0;
Still doesn't work.
Shouldn't that point at the first char in the string?

Share this post


Link to post
Share on other sites
xor    516
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
Raduprv    997
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
Quote:
Original post by Raduprv
Hmm
I also tried: if(!&knowledge_list[i].name)return 0;
Still doesn't work.
Shouldn't that point at the first char in the string?


maybe you could do if(!knowledge_list[i].name+1) ?

EDIT: actually, you might have to dereference that.

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