a question about a string

Q

QQ

I have a string

the length is 0. I got it from strlen(TE);
however, if I use

if(TE!=NULL)
{

}

it always goes to this loop.

Does strlen(TE)=0 mean TE=NULL?

Thanks a lot!
 
A

Artie Gold

QQ said:
I have a string

the length is 0. I got it from strlen(TE);
however, if I use

if(TE!=NULL)
{

}

it always goes to this loop.

Does strlen(TE)=0 mean TE=NULL?
No, but it means that *TE == 0 (or, if you prefer, '\0').

HTH,
--ag
 
N

Nelu

QQ said:
what's the different between
TE==NULL and
*TE==0

Please quote some context when replying. Even if you're replying to
your own post. Also, read: http://cfaj.freeshell.org/google/
If people see this post they won't know what TE is and nobody has to
read your older posts.
If *TE==0 (*TE=='\0') the first character of your string contains the
string
termination character, i.e. empty string (strlen(TE)==0).
If TE==NULL it means you have a pointer to a character that's
initialized to
NULL. You can't store anything into it. If you make it point to some
area in
memory that you are allowed to use (you allocated space for it) then
you can
use it to store characters.
 
P

pete

QQ said:
what's the different between
TE==NULL and
*TE==0
?

TN == NULL
TN is a null pointer, it doesn't point to anything.
*TN is a constraint violation.

*TS==0
TS is a string pointer.
TS points to a byte that has a value of zero.
 
P

Pedro Graca

QQ said:
what's the different between
TE==NULL and
*TE==0

if (TE == NULL) /* if TE points "to nowhere" */

if (*TE == 0) /* if the thing TE points to is 0 */



newbie example, trying to explain with a bit of fun mixed-in:

point your finger to any number (read vertically) in the list below.

0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4



.... Let's say you pointed to the number 18 and call your finger TE, so

*TE == 0 /* evaluates to false, because *TE == 18 */
TE == NULL /* evaluates to false, because TE is pointing to
somewhere relevant */

.... now cut your finger off (or, better, close your hand into a fist)

TE == NULL /* evaluates to true now; your finger isn't pointing to
anywhere */
/* *TE == 0 */ /* you can't do this now -- if the finger isn't
pointing to somewhere specific, you can't check
the value of what it points to */

.... now, assuming you opted to close your hand, point your finger to the
number 0 in the list

TE == NULL /* evaluates to false again */
*TE == 0 /* evaluates to true */

move your finger 26 positions to the right (TE = TE + 26;)


Q: where does it point to?
A: it points to somewhere outside the boundaries of the list and (if I
am not wrong) you have demons oozing out of your nostrils at this
moment :)
 
K

Kenneth Brody

QQ said:
what's the different between
TE==NULL and
*TE==0
?

The first (TE == NULL) says "is TE a NULL pointer?"

The second (*TE == 0) says "does TE point to a zero?"

Warning! Bad analogy coming up.

Suppose TE is a key holder. "*TE==0" would be "the box that the key opens
is empty" whereas "TE==NULL" would be "you have no key".

--
+-------------------------+--------------------+-----------------------------+
| Kenneth J. Brody | www.hvcomputer.com | |
| kenbrody/at\spamcop.net | www.fptech.com | #include <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------------+
Don't e-mail me at: <mailto:[email protected]>
 
J

Jordan Abel

TN == NULL
TN is a null pointer, it doesn't point to anything.
*TN is a constraint violation.

If by constraint violation you mean undefined behavior. But i think he
meant one or the other, not both in turn
 
S

Sirius Black

Jordan said:
The latter sets the first character to zero, in effect making the string
empty "".

Correction - The latter checks if the value in the location pointed to
by TE is zero or not.
 
M

Mark McIntyre

I have a string

the length is 0. I got it from strlen(TE);
however, if I use

if(TE!=NULL)
{

}

it always goes to this loop.

Of course it does - you're testing wether the pointer is NULL, not
whether its contents are of zero size.
Does strlen(TE)=0 mean TE=NULL?

No, its a pointer and can point to no bytes, but still be a pointer.
Mark McIntyre
 
R

Richard G. Riley

The latter sets the first character to zero, in effect making the string
empty "".

Slight misread there I think :-;

It checks if the character pointed to by TE have a value of 0.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,175
Messages
2,570,944
Members
47,491
Latest member
mohitk

Latest Threads

Top