code question

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

    #31
    Re: code question

    Bill Cunningham wrote:
    "Barry Schwarz" <schwarzb@dqel. comwrote in message
    news:7fe8d4def7 j0qh98uumds7bt8 ae0635kvc@4ax.c om...
    >On Fri, 19 Sep 2008 16:52:17 -0400, "Bill Cunningham"
    ><nospam@nspam. invalidwrote:
    >
    >> if ((fp = fopen(argv[4], "a")) == NULL) {
    >At the same time, it should be a file name.
    >>
    >That doesn't strike you as slightly odd?
    >>
    A filename input is an argument. I thought you treated it as such. There are
    many things that kandr2 doesn't teach you.
    Like common sense?

    --
    Ian Collins.

    Comment

    • Richard

      #32
      Re: code question

      Ben Bacarisse <ben.usenet@bsb .me.ukwrites:
      Richard<rgrdev@ gmail.comwrites :
      >
      >"Bill Cunningham" <nospam@nspam.i nvalidwrites:
      >>
      >>"Ian Collins" <ian-news@hotmail.co mwrote in message
      >>news:6jimldF3 8973U6@mid.indi vidual.net...
      <snip>
      >>> unsigned n = 1;
      >>> while ( n < argc ) {
      >>> data[n-1] = strtod(argv[n], NULL);
      >>> ++n;
      >>>
      >>[snip]
      >>>
      >>What's above is a bit of a different method than I would think of. Is that n
      >>minus 1 in the brackets?
      >>Why set n to 1?
      <snip>
      >To confuse you I assume. Its not a very C way of doing it IMO. Not
      >tested for ISO C correctness or whether it even works but this is much
      >clearer if you (and you must as a c programmer) understand for
      >loops. You should get the idea.
      >>
      > for(int n=0;n<argc;n++)
      > data[n] = strtod(argv[n+1], NULL);
      >
      Great disclaimer! "This is better (but it may be wrong)". Yes it is
      (wrong). Here is the flaw with using a debugger. It is easier to
      think about this code than to paste this into a main function and
      debug it.
      I noticed it when hitting return as the thread will show you. Whoops
      indeed. And I never used a debugger so where that came from I'm not
      sure. My reference to a debugger before was for Bill to see where he seg
      faulted with zero arguments.

      Comment

      • Richard

        #33
        Re: code question

        Bill Cunningham <nospam@nspam.i nvalidwrites:
        Richard wrote:
        >
        >And while I am at it, please stop spamming the application development
        >groups telling them you are trying to write a printer driver when you
        >could not even tell them the type of printer you are using.
        >
        [Testing out THunderbird]
        >
        Go back a read the thread for what it says. We were conversing about
        my printer make and model. I don't need to defend what's posted.
        I know what was said in the thread. You told the guys you wanted to
        write a printer driver for it.

        Comment

        • Richard

          #34
          Re: code question

          Ian Collins <ian-news@hotmail.co mwrites:
          Bill Cunningham wrote:
          >"Barry Schwarz" <schwarzb@dqel. comwrote in message
          >news:7fe8d4def 7j0qh98uumds7bt 8ae0635kvc@4ax. com...
          >>On Fri, 19 Sep 2008 16:52:17 -0400, "Bill Cunningham"
          >><nospam@nspam .invalidwrote:
          >>
          >>> if ((fp = fopen(argv[4], "a")) == NULL) {
          >>At the same time, it should be a file name.
          >>>
          >>That doesn't strike you as slightly odd?
          >>>
          >A filename input is an argument. I thought you treated it as such. There are
          >many things that kandr2 doesn't teach you.
          >
          Like common sense?
          Amazing stuff. Really. If he's a troll (and I really think he is) then
          he's damn good.

          Comment

          • Ian Collins

            #35
            Re: code question

            Richard wrote:
            Ian Collins <ian-news@hotmail.co mwrites:
            >
            >Bill Cunningham wrote:
            >>"Barry Schwarz" <schwarzb@dqel. comwrote in message
            >>news:7fe8d4de f7j0qh98uumds7b t8ae0635kvc@4ax .com...
            >>>On Fri, 19 Sep 2008 16:52:17 -0400, "Bill Cunningham"
            >>><nospam@nspa m.invalidwrote:
            >>>> if ((fp = fopen(argv[4], "a")) == NULL) {
            >>>At the same time, it should be a file name.
            >>>>
            >>>That doesn't strike you as slightly odd?
            >>>>
            >>A filename input is an argument. I thought you treated it as such. There are
            >>many things that kandr2 doesn't teach you.
            >Like common sense?
            >
            Amazing stuff. Really. If he's a troll (and I really think he is) then
            he's damn good.
            Hell, I'm working on a sunny Saturday afternoon so a bit of light
            entertainment is called for!

            --
            Ian Collins.

            Comment

            • blargg

              #36
              Re: code question

              In article <gb1lqt$m5j$3@r egistered.motza rella.org>,
              Richard<rgrdev@ gmail.comwrote:
              Ian Collins <ian-news@hotmail.co mwrites:
              >
              Bill Cunningham wrote:
              "Barry Schwarz" <schwarzb@dqel. comwrote in message
              news:7fe8d4def7 j0qh98uumds7bt8 ae0635kvc@4ax.c om...
              >On Fri, 19 Sep 2008 16:52:17 -0400, "Bill Cunningham"
              ><nospam@nspam. invalidwrote:
              >
              >> if ((fp = fopen(argv[4], "a")) == NULL) {
              >At the same time, it should be a file name.
              >>
              >That doesn't strike you as slightly odd?
              >>
              A filename input is an argument. I thought you treated it as such.
              There are
              many things that kandr2 doesn't teach you.
              Like common sense?
              >
              Amazing stuff. Really. If he's a troll (and I really think he is) then
              he's damn good.
              And in a way, that makes him an excellent programmer of the wetware of the
              people who just can't help not responding to him, time after time after
              time.

              Comment

              • CBFalconer

                #37
                Re: code question

                Andrew Poelstra wrote:
                Bill Cunningham <nospam@nspam.i nvalidwrote:
                >
                >Thanks Richard I know I can always count on you. I wondered about
                >argc and its uses.
                >
                In addition to argc, argv is also NULL terminated (IIRC!), so
                you can loop through it without needed argc to find out when
                to stop.
                >
                while(*argv)
                {
                process(*argv);
                ++argv;
                }
                I'm not sure that is safe. Check whether argv[0] can be a NULL.

                --
                [mail]: Chuck F (cbfalconer at maineline dot net)
                [page]: <http://cbfalconer.home .att.net>
                Try the download section.

                Comment

                • CBFalconer

                  #38
                  Re: code question

                  Bill Cunningham wrote:
                  >
                  Someone pointed out to me Richard that an answer to a question I
                  raised about bitwise operators was on page 49 or kandr2. I read,
                  and re-read, and re-read page 49 about 3 times and strained to
                  keep my focus on reading and the text. The question was why is
                  the unary operator ~ shown like this:
                  >
                  x~34;
                  and not x=x~34;
                  >
                  Is the question indeed answered on that page?
                  Are you sure it is not "x ~= 34;"? Have you checked the published
                  errata sheet? I greatly doubt that K&R have suppressed the blanks
                  as you have.

                  --
                  [mail]: Chuck F (cbfalconer at maineline dot net)
                  [page]: <http://cbfalconer.home .att.net>
                  Try the download section.

                  Comment

                  • Ian Collins

                    #39
                    Re: code question

                    CBFalconer wrote:
                    Andrew Poelstra wrote:
                    >Bill Cunningham <nospam@nspam.i nvalidwrote:
                    >>
                    >>Thanks Richard I know I can always count on you. I wondered about
                    >>argc and its uses.
                    >In addition to argc, argv is also NULL terminated (IIRC!), so
                    >you can loop through it without needed argc to find out when
                    >to stop.
                    >>
                    >while(*argv)
                    >{
                    > process(*argv);
                    > ++argv;
                    >}
                    >
                    I'm not sure that is safe. Check whether argv[0] can be a NULL.
                    >
                    Did you bother to read Richard's reply?

                    --
                    Ian Collins.

                    Comment

                    • Andrew Poelstra

                      #40
                      Re: code question

                      On 2008-09-20, CBFalconer <cbfalconer@yah oo.comwrote:
                      Andrew Poelstra wrote:
                      >Bill Cunningham <nospam@nspam.i nvalidwrote:
                      >>
                      >>Thanks Richard I know I can always count on you. I wondered about
                      >>argc and its uses.
                      >>
                      >In addition to argc, argv is also NULL terminated (IIRC!), so
                      >you can loop through it without needed argc to find out when
                      >to stop.
                      >>
                      >while(*argv)
                      >{
                      > process(*argv);
                      > ++argv;
                      >}
                      >
                      I'm not sure that is safe. Check whether argv[0] can be a NULL.
                      >
                      Oops! The first line should be
                      while(argv)
                      or
                      while(argv != NULL)

                      ...without the dereference.

                      --
                      Andrew Poelstra apoelstra@wpsof tware.net
                      That was a joke. Jokes in mathematics, are sometimes not funny.
                      -Veselin Jungic

                      Comment

                      • Richard Heathfield

                        #41
                        Re: code question

                        Bill Cunningham said:
                        Someone pointed out to me Richard that an answer to a question I
                        raised
                        about bitwise operators was on page 49 or kandr2. I read, and re-read,
                        and re-read page 49 about 3 times and strained to keep my focus on
                        reading and the text. The question was why is the unary operator ~ shown
                        like this:
                        >
                        x~34;
                        and not x=x~34;
                        I have read page 49 from top to bottom and back again, and can't even find
                        34 on that page, let alone x~34. Perhaps you could tell me on which line
                        the ~ operator is shown as x~34?

                        --
                        Richard Heathfield <http://www.cpax.org.uk >
                        Email: -http://www. +rjh@
                        Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
                        "Usenet is a strange place" - dmr 29 July 1999

                        Comment

                        • Richard Heathfield

                          #42
                          Re: code question

                          Andrew Poelstra said:
                          On 2008-09-20, CBFalconer <cbfalconer@yah oo.comwrote:
                          >Andrew Poelstra wrote:
                          >>Bill Cunningham <nospam@nspam.i nvalidwrote:
                          >>>
                          >>>Thanks Richard I know I can always count on you. I wondered about
                          >>>argc and its uses.
                          >>>
                          >>In addition to argc, argv is also NULL terminated (IIRC!), so
                          >>you can loop through it without needed argc to find out when
                          >>to stop.
                          >>>
                          >>while(*argv )
                          >>{
                          >> process(*argv);
                          >> ++argv;
                          >>}
                          >>
                          >I'm not sure that is safe. Check whether argv[0] can be a NULL.
                          >>
                          >
                          Oops! The first line should be
                          while(argv)
                          or
                          while(argv != NULL)
                          >
                          ..without the dereference.
                          No, you were right the first time.

                          --
                          Richard Heathfield <http://www.cpax.org.uk >
                          Email: -http://www. +rjh@
                          Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
                          "Usenet is a strange place" - dmr 29 July 1999

                          Comment

                          • Andrew Poelstra

                            #43
                            Re: code question

                            On 2008-09-20, Richard Heathfield <rjh@see.sig.in validwrote:
                            Andrew Poelstra said:
                            >
                            >On 2008-09-20, CBFalconer <cbfalconer@yah oo.comwrote:
                            >>Andrew Poelstra wrote:
                            >>>Bill Cunningham <nospam@nspam.i nvalidwrote:
                            >>>>
                            >>>>Thanks Richard I know I can always count on you. I wondered about
                            >>>>argc and its uses.
                            >>>>
                            >>>In addition to argc, argv is also NULL terminated (IIRC!), so
                            >>>you can loop through it without needed argc to find out when
                            >>>to stop.
                            >>>>
                            >>>while(*arg v)
                            >>>{
                            >>> process(*argv);
                            >>> ++argv;
                            >>>}
                            >>>
                            >>I'm not sure that is safe. Check whether argv[0] can be a NULL.
                            >>>
                            >>
                            >Oops! The first line should be
                            > while(argv)
                            >or
                            > while(argv != NULL)
                            >>
                            >..without the dereference.
                            >
                            No, you were right the first time.
                            >
                            Oh, I see. I guess I need more sleep before working with
                            pointers to pointers any more. =P

                            --
                            Andrew Poelstra apoelstra@wpsof tware.net
                            That was a joke. Jokes in mathematics, are sometimes not funny.
                            -Veselin Jungic

                            Comment

                            • Richard

                              #44
                              Re: code question

                              Andrew Poelstra <apoelstra@supe rnova.homewrite s:
                              On 2008-09-20, CBFalconer <cbfalconer@yah oo.comwrote:
                              >Andrew Poelstra wrote:
                              >>Bill Cunningham <nospam@nspam.i nvalidwrote:
                              >>>
                              >>>Thanks Richard I know I can always count on you. I wondered about
                              >>>argc and its uses.
                              >>>
                              >>In addition to argc, argv is also NULL terminated (IIRC!), so
                              >>you can loop through it without needed argc to find out when
                              >>to stop.
                              >>>
                              >>while(*argv )
                              >>{
                              >> process(*argv);
                              >> ++argv;
                              >>}
                              >>
                              >I'm not sure that is safe. Check whether argv[0] can be a NULL.
                              >>
                              >
                              Oops! The first line should be
                              while(argv)
                              or
                              while(argv != NULL)
                              >
                              ..without the dereference.
                              No. With the dereference. Not a typo so a fundamental misunderstandin g
                              of the basic use of pointers there. I can only assume beer has been
                              consumed :-;

                              Comment

                              • Richard

                                #45
                                Re: code question

                                Ian Collins <ian-news@hotmail.co mwrites:
                                CBFalconer wrote:
                                >Andrew Poelstra wrote:
                                >>Bill Cunningham <nospam@nspam.i nvalidwrote:
                                >>>
                                >>>Thanks Richard I know I can always count on you. I wondered about
                                >>>argc and its uses.
                                >>In addition to argc, argv is also NULL terminated (IIRC!), so
                                >>you can loop through it without needed argc to find out when
                                >>to stop.
                                >>>
                                >>while(*argv )
                                >>{
                                >> process(*argv);
                                >> ++argv;
                                >>}
                                >>
                                >I'm not sure that is safe. Check whether argv[0] can be a NULL.
                                >>
                                Did you bother to read Richard's reply?
                                Interestingly enough I must admit to never having seen or heard of that
                                before.

                                And I guess the resulting C code would be something like:

                                for(;*argv;proc ess(*argv++));

                                or

                                for(char *p=*argv++; p; process(p));

                                Since you have to read the contents of the address pointed to by argv
                                anyway the compiler is clever enough in most cases to mean that using a
                                decrement and branch like argc-- is not any faster except for the last
                                null case.


                                Comment

                                Working...