envp[i]!=(char *)0

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Rookie

    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.
  • Christopher Benson-Manica

    #2
    Re: envp[i]!=(char *)0

    Rookie <dominicjoseph@ rediffmail.com> spoke thus:
    [color=blue]
    > What exactly does (char *)0 stand for? Is it a test for null pointer?
    > Can we use nul for the same as in:[/color]

    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)cybers pace.org | don't, I need to know. Flames welcome.

    Comment

    • Ben Pfaff

      #3
      Re: envp[i]!=(char *)0

      dominicjoseph@r ediffmail.com (Rookie) writes:
      [color=blue]
      > I was just reading about the envp argument for main function.[/color]

      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.
      [color=blue]
      > 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[/color]

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

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

      It's a null pointer constant converted to type char *. Yes, it
      is used as part of a null pointer test.
      [color=blue]
      > Can we use nul for the same as in:
      >
      > for(i=0; envp[i]!=nul;i++)[/color]

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

      Comment

      • DJP

        #4
        Re: envp[i]!=(char *)0

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

        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?


        Comment

        • Rookie

          #5
          Re: envp[i]!=(char *)0

          Thanks for the reply.


          "Christophe r Benson-Manica" <ataru@nospam.c yberspace.org> wrote in message
          news:civ8dl$l6m $1@chessie.cirr .com...[color=blue]
          > Rookie <dominicjoseph@ rediffmail.com> spoke thus:
          >[color=green]
          >> What exactly does (char *)0 stand for? Is it a test for null pointer?
          >> Can we use nul for the same as in:[/color]
          >
          > 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)cybers pace.org | don't, I need to know. Flames welcome.[/color]


          Comment

          • Rookie

            #6
            Re: envp[i]!=(char *)0

            Thanks for the detailed reply. Very informative.

            "Ben Pfaff" <blp@cs.stanfor d.edu> wrote in message
            news:87d60cpyeh .fsf@benpfaff.o rg...[color=blue]
            > dominicjoseph@r ediffmail.com (Rookie) writes:
            >[color=green]
            >> I was just reading about the envp argument for main function.[/color]
            >
            > 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.
            >[color=green]
            >> 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[/color]
            >
            > 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.
            >[color=green]
            >> [This generally appears in a for, something like
            >> for(i=0;envp[i]!=(char *)0;i++)][/color]
            >
            > It's more natural to write:
            > for (i = 0; envp[i] != NULL; i++)
            >[color=green]
            >> What exactly does (char *)0 stand for? Is it a test for null pointer?[/color]
            >
            > It's a null pointer constant converted to type char *. Yes, it
            > is used as part of a null pointer test.
            >[color=green]
            >> Can we use nul for the same as in:
            >>
            >> for(i=0; envp[i]!=nul;i++)[/color]
            >
            > I think you mean NULL, not nul. There is nothing in ANSI C
            > called `nul'.
            > --
            > int main(void){char
            > p[]="ABCDEFGHIJKLM NOPQRSTUVWXYZab cdefghijklmnopq rstuvwxyz.\
            > \n",*q="kl BIcNBFr.NKEzjwC IxNJC";int i=sizeof p/2;char *strchr();int
            > putchar(\
            > );while(*q){i+= strchr(p,*q++)-p;if(i>=(int)si zeof p)i-=sizeof
            > p-1;putchar(p[i]\
            > );}return 0;}[/color]


            Comment

            • Artie Gold

              #7
              Re: envp[i]!=(char *)0

              DJP wrote:[color=blue][color=green]
              >>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.[/color]
              >
              >
              > 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?
              >
              >[/color]
              Yes.

              HTH,
              --ag

              --
              Artie Gold -- Austin, Texas

              "If you don't think it matters, you're not paying attention."

              Comment

              • Keith Thompson

                #8
                Re: envp[i]!=(char *)0

                dominicjoseph@r ediffmail.com (Rookie) writes:[color=blue]
                > 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.[/color]

                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).
                [color=blue]
                > 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?[/color]

                Yes, (char *)0 is a null pointer.
                [color=blue]
                > Can we use nul for the same as in:
                >
                > for(i=0; envp[i]!=nul;i++)[/color]

                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_Keit h) kst-u@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.

                Comment

                • Keith Thompson

                  #9
                  Re: envp[i]!=(char *)0

                  "DJP" <dominicjoseph@ rediffmail.com> writes:[color=blue][color=green]
                  >> 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.[/color]
                  >
                  > 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?[/color]

                  Yes. Any decent C textbook should explain this.

                  --
                  Keith Thompson (The_Other_Keit h) kst-u@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.

                  Comment

                  • Derrick Coetzee

                    #10
                    Re: envp[i]!=(char *)0

                    Rookie wrote:[color=blue]
                    > What exactly does (char *)0 stand for? Is it a test for null pointer?[/color]

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



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

                    Comment

                    • John Temples

                      #11
                      Re: envp[i]!=(char *)0

                      In article <lnisa4r8hn.fsf @nuthaus.mib.or g>, Keith Thompson wrote:
                      [color=blue]
                      > Yes, (char *)0 is a null pointer.[/color]

                      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

                      Comment

                      • Rookie

                        #12
                        Re: envp[i]!=(char *)0

                        > Is that also true in C90? I can't find anything in C90 that permits[color=blue]
                        > conversion of a null pointer constant to a null pointer except
                        > equality and assignment operators.[/color]


                        What is C90?


                        Comment

                        • Joona I Palaste

                          #13
                          Re: envp[i]!=(char *)0

                          Rookie <dominicjoseph@ rediffmail.com> scribbled the following:[color=blue][color=green]
                          >> 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.[/color][/color]
                          [color=blue]
                          > What is C90?[/color]

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

                          --
                          /-- Joona Palaste (palaste@cc.hel sinki.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

                          Comment

                          • pete

                            #14
                            Re: envp[i]!=(char *)0

                            John Temples wrote:[color=blue]
                            >
                            > In article <lnisa4r8hn.fsf @nuthaus.mib.or g>, Keith Thompson wrote:
                            >[color=green]
                            > > Yes, (char *)0 is a null pointer.[/color]
                            >
                            > 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.[/color]

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

                            --
                            pete

                            Comment

                            • Dan Pop

                              #15
                              Re: envp[i]!=(char *)0

                              In <ln6564r7tl.fsf @nuthaus.mib.or g> Keith Thompson <kst-u@mib.org> writes:
                              [color=blue]
                              >"DJP" <dominicjoseph@ rediffmail.com> writes:[color=green][color=darkred]
                              >>> 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.[/color]
                              >>
                              >> 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?[/color]
                              >
                              >Yes. Any decent C textbook should explain this.[/color]

                              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: Dan.Pop@ifh.de
                              Currently looking for a job in the European Union

                              Comment

                              Working...