envp[i]!=(char *)0

R

Rookie

I was just reading about the envp argument for main function. Since
there is no argc equivalent for envp (i.e the number of elements for
the envp array are not known) the last element will be a null pointer.
But my question is about the syntax used to test this:

envp!=(char *)0

[This generally appears in a for, something like
for(i=0;envp!=(char *)0;i++)]

What exactly does (char *)0 stand for? Is it a test for null pointer?
Can we use nul for the same as in:

for(i=0; envp!=nul;i++)

Hope to hear from someone. Thanks.
 
C

Christopher Benson-Manica

Rookie said:
What exactly does (char *)0 stand for? Is it a test for null pointer?
Can we use nul for the same as in:

1) Yes, unadorned 0 in a pointer context is identical with the macro
NULL. The cast is unnecessary.
2) The macro is NULL, not nul.
 
B

Ben Pfaff

I was just reading about the envp argument for main function.

That's not portable. The "envp" argument is a traditional Unix
thing, not an ANSI C thing. Modern Unix systems aren't required
to support envp. SUSv3 explains it this way:

Some implementations provide a third argument to main()
called envp. This is defined as a pointer to the
environment. The ISO C standard specifies invoking
main() with two arguments, so implementations must
support applications written this way. Since this volume
of IEEE Std 1003.1-2001 defines the global variable
environ, which is also provided by historical
implementations and can be used anywhere that envp could
be used, there is no functional need for the envp
argument. Applications should use the getenv() function
rather than accessing the environment directly via either
envp or environ. Implementations are required to support
the two-argument calling sequence, but this does not
prohibit an implementation from supporting envp as an
optional third argument.
Since there is no argc equivalent for envp (i.e the number of
elements for the envp array are not known) the last element
will be a null pointer. But my question is about the syntax
used to test this:

envp!=(char *)0


Why are you testing it that way? It's a bit silly. I would
write it as `envp != NULL', and the `!= NULL' part is optional.
[This generally appears in a for, something like
for(i=0;envp!=(char *)0;i++)]


It's more natural to write:
for (i = 0; envp != NULL; i++)
What exactly does (char *)0 stand for? Is it a test for null pointer?

It's a null pointer constant converted to type char *. Yes, it
is used as part of a null pointer test.
Can we use nul for the same as in:

for(i=0; envp!=nul;i++)


I think you mean NULL, not nul. There is nothing in ANSI C
called `nul'.
 
D

DJP

Why are you testing it that way? It's a bit silly. I would
write it as `envp != NULL', and the `!= NULL' part is optional.


Why is the `!= NULL' part optional? Does this mean that:

if(envp)

would evaluate to true if envp is not a null pointer and false if it is a
null pointer?
 
R

Rookie

Thanks for the reply.


Christopher Benson-Manica said:
1) Yes, unadorned 0 in a pointer context is identical with the macro
NULL. The cast is unnecessary.
2) The macro is NULL, not nul.
 
R

Rookie

Thanks for the detailed reply. Very informative.

Ben Pfaff said:
I was just reading about the envp argument for main function.

That's not portable. The "envp" argument is a traditional Unix
thing, not an ANSI C thing. Modern Unix systems aren't required
to support envp. SUSv3 explains it this way:

Some implementations provide a third argument to main()
called envp. This is defined as a pointer to the
environment. The ISO C standard specifies invoking
main() with two arguments, so implementations must
support applications written this way. Since this volume
of IEEE Std 1003.1-2001 defines the global variable
environ, which is also provided by historical
implementations and can be used anywhere that envp could
be used, there is no functional need for the envp
argument. Applications should use the getenv() function
rather than accessing the environment directly via either
envp or environ. Implementations are required to support
the two-argument calling sequence, but this does not
prohibit an implementation from supporting envp as an
optional third argument.
Since there is no argc equivalent for envp (i.e the number of
elements for the envp array are not known) the last element
will be a null pointer. But my question is about the syntax
used to test this:

envp!=(char *)0


Why are you testing it that way? It's a bit silly. I would
write it as `envp != NULL', and the `!= NULL' part is optional.
[This generally appears in a for, something like
for(i=0;envp!=(char *)0;i++)]


It's more natural to write:
for (i = 0; envp != NULL; i++)
What exactly does (char *)0 stand for? Is it a test for null pointer?

It's a null pointer constant converted to type char *. Yes, it
is used as part of a null pointer test.
Can we use nul for the same as in:

for(i=0; envp!=nul;i++)


I think you mean NULL, not nul. There is nothing in ANSI C
called `nul'.
--
int main(void){char
p[]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz.\
\n",*q="kl BIcNBFr.NKEzjwCIxNJC";int i=sizeof p/2;char *strchr();int
putchar(\
);while(*q){i+=strchr(p,*q++)-p;if(i>=(int)sizeof p)i-=sizeof
p-1;putchar(p\
);}return 0;}
 
A

Artie Gold

DJP said:
Why are you testing it that way? It's a bit silly. I would
write it as `envp != NULL', and the `!= NULL' part is optional.



Why is the `!= NULL' part optional? Does this mean that:

if(envp)

would evaluate to true if envp is not a null pointer and false if it is a
null pointer?

Yes.

HTH,
--ag
 
K

Keith Thompson

I was just reading about the envp argument for main function. Since
there is no argc equivalent for envp (i.e the number of elements for
the envp array are not known) the last element will be a null pointer.

The envp argument to main() is not defined in standard C, and probably
isn't supported by all compilers. (It's probably better to use an
external variable called "environ", but that's also non-standard and
non-portable.) The only standard C way to get environment variables
is the getenv() function, but that lacks some functionality (like the
ability to traverse all environment variables and to set or modify
them).
But my question is about the syntax used to test this:

envp!=(char *)0

[This generally appears in a for, something like
for(i=0;envp!=(char *)0;i++)]

What exactly does (char *)0 stand for? Is it a test for null pointer?


Yes, (char *)0 is a null pointer.
Can we use nul for the same as in:

for(i=0; envp!=nul;i++)


No, there is nothing in C called "nul". You're probably looking for
NULL, a macro defined in <stddef.h> (and other headers) that expands
to a null pointer constant.
 
K

Keith Thompson

DJP said:
Why are you testing it that way? It's a bit silly. I would
write it as `envp != NULL', and the `!= NULL' part is optional.


Why is the `!= NULL' part optional? Does this mean that:

if(envp)

would evaluate to true if envp is not a null pointer and false if it is a
null pointer?


Yes. Any decent C textbook should explain this.
 
J

John Temples

Yes, (char *)0 is a null pointer.

Is that also true in C90? I can't find anything in C90 that permits
conversion of a null pointer constant to a null pointer except
equality and assignment operators.
 
R

Rookie

Is that also true in C90? I can't find anything in C90 that permits
conversion of a null pointer constant to a null pointer except
equality and assignment operators.


What is C90?
 
J

Joona I Palaste

What is C90?

The second-to-latest version of the C standard, published in 1990, hence
the name.

--
/-- Joona Palaste ([email protected]) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"You have moved your mouse, for these changes to take effect you must shut down
and restart your computer. Do you want to restart your computer now?"
- Karri Kalpio
 
D

Dan Pop

In said:
DJP said:
Why are you testing it that way? It's a bit silly. I would
write it as `envp != NULL', and the `!= NULL' part is optional.


Why is the `!= NULL' part optional? Does this mean that:

if(envp)

would evaluate to true if envp is not a null pointer and false if it is a
null pointer?


Yes. Any decent C textbook should explain this.


The c.l.c FAQ, too:

5.3: Is the abbreviated pointer comparison "if(p)" to test for non-
null pointers valid?

Dan
 
P

pete

John said:
Is that also true in C90? I can't find anything in C90 that permits
conversion of a null pointer constant to a null pointer except
equality and assignment operators.

He meant "(char *)0 is a null pointer constant."
 
D

Dan Pop

In said:
Is that also true in C90? I can't find anything in C90 that permits
conversion of a null pointer constant to a null pointer except
equality and assignment operators.

The implicit conversions performed by the assignment operators have
exactly the same semantics as the corresponding explicit conversions.
It would be downright insane if p and q in the following example
wouldn't compare equal:

char *p = 0;
char *q = (char *)0;

Dan
 
R

Richard Bos

pete said:
He meant "(char *)0 is a null pointer constant."

If he did, he'd be wrong. 0 is a null pointer constant; (void *)0 is a
null pointer constant; but (char *)0 is _not_ a null pointer constant.
It's a null pointer constant, 0, _plus_ a cast to char *. This evaluates
to a null pointer with char pointer type, so it could be said to be a
null pointer, but it is not a null pointer constant.

Richard
 
D

Dan Pop

In said:
He meant "(char *)0 is a null pointer constant."

What makes you think so? (char *)0 is a null pointer but it's certainly
not a null pointer constant (check the definition of the null pointer
constant).

(char *)0 is a constant expression evaluating to a null pointer to char.
If you don't see how this is semantically different from a null pointer
constant, try:

int *p = (char *)0;

A conforming compiler must issue a diagnostic. Remove the cast and you
have a correct initialiser for p.

Dan
 

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

Forum statistics

Threads
474,146
Messages
2,570,832
Members
47,374
Latest member
EmeliaBryc

Latest Threads

Top