473,395 Members | 1,577 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

envp[i]!=(char *)0

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[i]!=(char *)0

[This generally appears in a for, something like
for(i=0;envp[i]!=(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[i]!=nul;i++)

Hope to hear from someone. Thanks.
Nov 14 '05 #1
24 3036
Rookie <do***********@rediffmail.com> spoke thus:
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.

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Nov 14 '05 #2
do***********@rediffmail.com (Rookie) writes:
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[i]!=(char *)0
Why are you testing it that way? It's a bit silly. I would
write it as `envp[i] != NULL', and the `!= NULL' part is optional.
[This generally appears in a for, something like
for(i=0;envp[i]!=(char *)0;i++)]
It's more natural to write:
for (i = 0; envp[i] != 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[i]!=nul;i++)


I think you mean NULL, not nul. There is nothing in ANSI C
called `nul'.
--
int main(void){char p[]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuv wxyz.\
\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[i]\
);}return 0;}
Nov 14 '05 #3
DJP
> Why are you testing it that way? It's a bit silly. I would
write it as `envp[i] != NULL', and the `!= NULL' part is optional.


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

if(envp[i])

would evaluate to true if envp[i] is not a null pointer and false if it is a
null pointer?
Nov 14 '05 #4
Thanks for the reply.
"Christopher Benson-Manica" <at***@nospam.cyberspace.org> wrote in message
news:ci**********@chessie.cirr.com...
Rookie <do***********@rediffmail.com> spoke thus:
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.

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.

Nov 14 '05 #5
Thanks for the detailed reply. Very informative.

"Ben Pfaff" <bl*@cs.stanford.edu> wrote in message
news:87************@benpfaff.org...
do***********@rediffmail.com (Rookie) writes:
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[i]!=(char *)0


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


It's more natural to write:
for (i = 0; envp[i] != 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[i]!=nul;i++)


I think you mean NULL, not nul. There is nothing in ANSI C
called `nul'.
--
int main(void){char
p[]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuv wxyz.\
\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[i]\
);}return 0;}

Nov 14 '05 #6
DJP wrote:
Why are you testing it that way? It's a bit silly. I would
write it as `envp[i] != NULL', and the `!= NULL' part is optional.

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

if(envp[i])

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

Yes.

HTH,
--ag

--
Artie Gold -- Austin, Texas

"If you don't think it matters, you're not paying attention."
Nov 14 '05 #7
do***********@rediffmail.com (Rookie) writes:
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[i]!=(char *)0

[This generally appears in a for, something like
for(i=0;envp[i]!=(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[i]!=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.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #8
"DJP" <do***********@rediffmail.com> writes:
Why are you testing it that way? It's a bit silly. I would
write it as `envp[i] != NULL', and the `!= NULL' part is optional.


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

if(envp[i])

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


Yes. Any decent C textbook should explain this.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #9
Rookie wrote:
What exactly does (char *)0 stand for? Is it a test for null pointer?


Read the excellent section 5 of the FAQ on null pointers:

http://www.eskimo.com/~scs/C-faq/s5.html

(Yes, the entire section.)
--
Derrick Coetzee
I grant this newsgroup posting into the public domain. I disclaim all
express or implied warranty and all liability. I am not a professional.
Nov 14 '05 #10
In article <ln************@nuthaus.mib.org>, Keith Thompson wrote:
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.

--
John W. Temples, III
Nov 14 '05 #11
> 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?
Nov 14 '05 #12
Rookie <do***********@rediffmail.com> scribbled the following:
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?


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

--
/-- Joona Palaste (pa*****@cc.helsinki.fi) ------------- 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
Nov 14 '05 #13
John Temples wrote:

In article <ln************@nuthaus.mib.org>, Keith Thompson wrote:
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.


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

--
pete
Nov 14 '05 #14
In <ln************@nuthaus.mib.org> Keith Thompson <ks***@mib.org> writes:
"DJP" <do***********@rediffmail.com> writes:
Why are you testing it that way? It's a bit silly. I would
write it as `envp[i] != NULL', and the `!= NULL' part is optional.


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

if(envp[i])

would evaluate to true if envp[i] 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
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Currently looking for a job in the European Union
Nov 14 '05 #15
In <cj**********@oravannahka.helsinki.fi> Joona I Palaste <pa*****@cc.helsinki.fi> writes:
Rookie <do***********@rediffmail.com> scribbled the following:
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?


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


It's also the currently used version of the C standard.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Currently looking for a job in the European Union
Nov 14 '05 #16
In <sl*******************@jwt.xargs.com> John Temples <us****@xargs-spam.com> writes:
In article <ln************@nuthaus.mib.org>, Keith Thompson wrote:
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.


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
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Currently looking for a job in the European Union
Nov 14 '05 #17
pete <pf*****@mindspring.com> wrote:
John Temples wrote:

In article <ln************@nuthaus.mib.org>, Keith Thompson wrote:
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.


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
Nov 14 '05 #18
In <41***********@mindspring.com> pete <pf*****@mindspring.com> writes:
John Temples wrote:

In article <ln************@nuthaus.mib.org>, Keith Thompson wrote:
> 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.


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
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Currently looking for a job in the European Union
Nov 14 '05 #19
Richard Bos wrote:

pete <pf*****@mindspring.com> wrote:
John Temples wrote:

In article <ln************@nuthaus.mib.org>, Keith Thompson wrote:

> 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.


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


If he did, he'd be wrong.


It was just me being wrong.

--
pete
Nov 14 '05 #20
> 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.


Newbie question: Whats the difference between a null pointer and a null
pointer constant?
Nov 14 '05 #21
"Rookie" <do***********@rediffmail.com> writes:
Newbie question: Whats the difference between a null pointer and a null
pointer constant?


Here are the official definitions from C99:

3 An integer constant expression with the value 0, or such an
expression cast to type void *, is called a null pointer
constant.55) If a null pointer constant is converted to a
pointer type, the resulting pointer, called a null pointer,
is guaranteed to compare unequal to a pointer to any object
or function.

Basically, a null pointer constant is a specific kind of
expression. If you then use it as a pointer, then it's a null
pointer.
--
Ben Pfaff
email: bl*@cs.stanford.edu
web: http://benpfaff.org
Nov 14 '05 #22
"Rookie" <do***********@rediffmail.com> writes:
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.


Newbie question: Whats the difference between a null pointer and a null
pointer constant?


The C FAQ is at <http://www.eskimo.com/~scs/C-faq/faq.html>.

Read section 5, "Null Pointers". (Then read the rest of the FAQ.)

Quick answer: a null pointer is a value that exists while your program
is running; a null pointer constant is a construct in your C source
program (that can indicate a null pointer value when it's evaluated).

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #23
"Rookie" <do***********@rediffmail.com> wrote in message news:<cj**********@gist.usc.edu>...
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.


Newbie question: Whats the difference between a null pointer and a null
pointer constant?


Quoth the C99 standard:

6.3.2.3 Pointers

[...]

3. An integer constant expression with the value 0, or such an
expression cast to type void *, is called a null pointer constant.48)
If a null pointer constant is converted to a pointer type, the
resulting pointer, called a null pointer, is guaranteed to compare
unequal to a pointer to any object or function.
Mark F. Haigh
mf*****@sbcglobal.net
Nov 14 '05 #24
"Rookie" <do***********@rediffmail.com> wrote:
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.


Newbie question: Whats the difference between a null pointer and a null
pointer constant?


Basically, the same difference as that between a float object with the
value 4.0 when your program is running, and the characters 4.0f in your
source code.

Richard
Nov 14 '05 #25

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Sigfried Manner | last post by:
Hello NG! I have following situation: c-prg | webprg "c-prg" is a small c-programm which reads data from an fastCgi-Server and writes them to stdout. The stdout is piped into webprg....
15
by: Casanova | last post by:
Hello!! how can i get all the details of the environmental variables. If i use getenv, i will have to specify the name of the variable. for intsance i will have to give getenv("PATH") How can...
25
by: broeks | last post by:
I'm trying to create an array of character arrays. I would use strings, but the function I need to feed into looks like this int execve(const char *filename, char *const argv , char *const envp...
6
by: LaVic | last post by:
Hello Everyone, I am trying to add two binary numbers in the form of two arrays. The code that i have been using compiles, but it does not give me the results i would expect. The problem that i...
3
by: HardHackz | last post by:
Are there any variables like %HOMEDIR%, %USERNAME%, etc. that batch has for C++?
1
stealwings
by: stealwings | last post by:
I have implemented a binary search and some other functions to my code, when I compile it, it gives me no error, but when I run it, it gives me an Access violation, why? Can some one explain me plz....
0
by: kukluxklan | last post by:
hi all, im tring to implement a bash shell with some operations like(pipe,redirection etc).im using execvp system call to achieve this.im stuck at redirecting the outut of a command to a file.the...
6
by: Andrew Wingorodov | last post by:
i have std::vector<stringarg; for dynamic program configuration. i need get const char* argv; for execv(3). how i can make its simple? there may be ready solutions? -- www.andr.ca
0
by: jsimps44 | last post by:
Hi, I'm fairly new to c, and very new to piping and file descriptors and can't seem to get past this problem. The piping is very much not working, and I can't figure out for the life of me why. Any...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.