Stupid C problem [solved]

Started by
7 comments, last by graveyard filla 18 years, 10 months ago
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]
Advertisement
No chance of telling that without knowing what datatype name is.
Oops, sorry.
char name[64];

And the string is empty.
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.
FTA, my 2D futuristic action MMORPG
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).
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?
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.
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.
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.
FTA, my 2D futuristic action MMORPG

This topic is closed to new replies.

Advertisement