Jump to content
  • Advertisement
Sign in to follow this  
Raduprv

Stupid C problem [solved]

This topic is 4880 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Consider this: if(!knowledge_list.name)return 0;//doesn't work vs if(!knowledge_list.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
Advertisement
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
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
Hmm
I also tried: if(!&knowledge_list.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
if(!knowledge_list.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.name))return 0;//should work


if(!knowledge_list.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.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.name))return 0;//doesn't work


if(!knowledge_list.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.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.name+1) ?

EDIT: actually, you might have to dereference that.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!