Re: FAQ 20-12

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

    Re: FAQ 20-12

    On Jul 3, 6:51 pm, viza <tom.v...@gm-il.com.obviousc hange.invalid>
    wrote:
       do {
               *--p = "0123456789abcd ef"[num % base];  << -------This
    >
    line has me confused? If someone could help, I will appreciate it.
    >
    Let me space it out a bit:
    >
    do {
      char hexidecimal_dig its[]= {'0','1','2', ...etc };
    >
      p= p - 1;
    >
      *p= hexidecimal_dig its[ num % base ];
    >
    The only bit of maths that you might not understand is that ( num %
    base ) means the remainder when num is divided by base.

    I have just never seen ( not that that necessarily means anything) a
    char array initialized like that. I am curious as to how you get from
    this ("0123456789abc def"[num % base];) to this (char
    hexidecimal_dig its[]= {'0','1','2', ...etc };). ( The math is fine
    ( viz the modulo operator). Thank you for your help.

  • Richard Heathfield

    #2
    Re: FAQ 20-12

    mdh said:
    On Jul 3, 6:51 pm, viza <tom.v...@gm-il.com.obviousc hange.invalid>
    wrote:
    >
    do {
    *--p = "0123456789abcd ef"[num % base]; << -------This
    >>
    >line has me confused? If someone could help, I will appreciate it.
    >>
    >Let me space it out a bit:
    >>
    >do {
    >char hexidecimal_dig its[]= {'0','1','2', ...etc };
    >>
    >p= p - 1;
    >>
    >*p= hexidecimal_dig its[ num % base ];
    >>
    >The only bit of maths that you might not understand is that ( num %
    >base ) means the remainder when num is divided by base.
    >
    >
    I have just never seen ( not that that necessarily means anything) a
    char array initialized like that. I am curious as to how you get from
    this ("0123456789abc def"[num % base];) to this (char
    hexidecimal_dig its[]= {'0','1','2', ...etc };).
    Come on Michael, he's just spreading it out a little to help you see the
    logic, that's all. There is no hexadecimal_dig its array, because it isn't
    necessary - the string literal will do just as well. But what viza is
    telling you is that we could write code that has the same effect, using a
    temporary array instead of a string literal, and you seem to agree that
    this equivalent would be easy to understand.

    The trick is that there is no trick. "A"[0] is 'A'. "AB"[0] is also 'A',
    and "AB"[1] is 'B'. "ABC"[0] is 'A', "ABC"[1] is 'B', "ABC"[2] is 'C'.

    "0123456789abcd ef"[0] is '0'.
    "0123456789abcd ef"[1] is '1'.
    "0123456789abcd ef"[9] is '9'.
    "0123456789abcd ef"[10] is 'a'.
    "0123456789abcd ef"[11] is 'b'.

    Clear now?

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

    • CBFalconer

      #3
      Re: FAQ 20-12

      mdh wrote:
      >
      .... snip ...
      >
      I have just never seen ( not that that necessarily means anything)
      a char array initialized like that. I am curious as to how you get
      from this ("0123456789abc def"[num % base];) to this (char
      hexidecimal_dig its[]= {'0','1','2', ...etc };). ( The math is fine
      (viz the modulo operator). Thank you for your help.
      Why the difficulty? "012...def" is a char array. num % base is a
      number in the range 0 through 15 as long as num and base are
      positive, and base is in the range 1 through 16. Therefore
      "0123...def "[num % base] selects one of those characters.

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

      Comment

      • mdh

        #4
        Re: FAQ 20-12

        On Jul 3, 10:34 pm, Richard Heathfield <r...@see.sig.i nvalidwrote:
        mdh said:
        Come on Michael, he's just spreading it out a little to help you see the
        logic, that's all.

        Sounds perfectly logical to me!



        There is no hexadecimal_dig its array, because it isn't
        necessary - the string literal will do just as well.
        ....snip....
        The trick is that there is no trick. "A"[0] is 'A'. "AB"[0] is also 'A',
        and "AB"[1] is 'B'. "ABC"[0] is 'A', "ABC"[1] is 'B', "ABC"[2] is 'C'.
        >
        "0123456789abcd ef"[0] is '0'.
        "0123456789abcd ef"[1] is '1'.
        "0123456789abcd ef"[9] is '9'.
        "0123456789abcd ef"[10] is 'a'.
        "0123456789abcd ef"[11] is 'b'.
        >
        Clear now?
        >
        Yep...I have gone back and looked at K&R and agree it is a very neat
        way of doing this. What I was trying to say was that even though the
        construct "I am a string literal" is well explained in K&R, I was not
        aware that one could refer to each character in the constant by using
        the operator [] immediately following it. All I know is that when I
        have simply assumed things in the past, it has often gotten me into
        hot water...so I know I can ask here and all my assumptions will be
        rapidly corrected!!! :-)
        Thank you Richard.

        Comment

        • mdh

          #5
          Re: FAQ 20-12

          On Jul 3, 7:58 pm, CBFalconer <cbfalco...@yah oo.comwrote:
          >
          Why the difficulty?  "012...def" is a char array.  num % base is a
          number in the range 0 through 15 as long as num and base are
          positive, and base is in the range 1 through 16.  Therefore
          "0123...def "[num % base] selects one of those characters.
          >
          --

          Hi Chuck...I think I made it more difficult than it is.


          One last question. The line

          p = &retbuf[sizeof(retbuf)-1];

          where retbuf[] is declared as a static char array

          uses the ampersand when assigning the address to the pointer 'p'. Is
          this necessary because using the name alone would assign the address
          of retbuf[0] which is not what we want ie retbuf[x] by itself would
          not do it? Sorry if this is another obvious question, but that's the
          way I learn!!!

          Thanks for taking the time to reply.


          Thank you for your help.

          Comment

          • santosh

            #6
            Re: FAQ 20-12

            mdh wrote:
            On Jul 3, 7:58 pm, CBFalconer <cbfalco...@yah oo.comwrote:
            >>
            >Why the difficulty?  "012...def" is a char array.  num % base is a
            >number in the range 0 through 15 as long as num and base are
            >positive, and base is in the range 1 through 16.  Therefore
            >"0123...def "[num % base] selects one of those characters.
            >>
            >--
            >
            >
            Hi Chuck...I think I made it more difficult than it is.
            >
            >
            One last question. The line
            >
            p = &retbuf[sizeof(retbuf)-1];
            >
            where retbuf[] is declared as a static char array
            >
            uses the ampersand when assigning the address to the pointer 'p'. Is
            this necessary because using the name alone would assign the address
            of retbuf[0] which is not what we want ie retbuf[x] by itself would
            not do it? Sorry if this is another obvious question, but that's the
            way I learn!!!
            You're right. Just doing:

            p = retbuf;

            will assign the address of the first element of the array retbuf to p,
            which must be of the appropriate pointer type. Doing:

            p = &retbuf;

            will assign the address of the array 'retbuf' to p, which must be of the
            appropriate pointer to array type. Doing:

            p = retbuf[x];

            will assign the _value_ of element 'x' of retbuf to p, which is probably
            not what you want. Doing:

            p = &retbuf[x];

            will assign the address of element 'x' of retbuf to p, which is what we
            want in this code posted up-thread.

            <snip>

            Comment

            • mdh

              #7
              Re: FAQ 20-12

              On Jul 4, 7:05 am, santosh <santosh....@gm ail.comwrote:
              mdh wrote:
              On Jul 3, 7:58 pm, CBFalconer <cbfalco...@yah oo.comwrote:
              >
              Why the difficulty?  "012...def" is a char array.  num % base is a
              number in the range 0 through 15 as long as num and base are
              positive, and base is in the range 1 through 16.  Therefore
              "0123...def "[num % base] selects one of those characters.
              >
              --
              >
              Hi Chuck...I think I made it more difficult than it is.
              >
              One last question.  The line
              >
              p = &retbuf[sizeof(retbuf)-1];
              >
              where retbuf[] is declared as a static char array
              >
              uses the ampersand when assigning the address to the pointer 'p'. Is
              this necessary because using the name alone would assign the address
              of retbuf[0] which is not what we want ie retbuf[x] by itself would
              not do it? Sorry if this is another obvious question, but that's the
              way I learn!!!
              >
              You're right. Just doing:
              >
                p = retbuf;
              >
              will assign the address of the first element of the array retbuf to p,
              which must be of the appropriate pointer type. Doing:
              >
                p = &retbuf;
              >
              will assign the address of the array 'retbuf' to p, which must be of the
              appropriate pointer to array type. Doing:
              >
                p = retbuf[x];
              >
              will assign the _value_ of element 'x' of retbuf to p, which is probably
              not what you want. Doing:
              >
                p = &retbuf[x];
              >
              will assign the address of element 'x' of retbuf to p, which is what we
              want in this code posted up-thread.
              >
              <snip>
              Thanks Santosh.

              Comment

              • Keith Thompson

                #8
                Re: FAQ 20-12

                santosh <santosh.k83@gm ail.comwrites:
                [...]
                You're right. Just doing:
                >
                p = retbuf;
                >
                will assign the address of the first element of the array retbuf to p,
                which must be of the appropriate pointer type. Doing:
                >
                p = &retbuf;
                >
                will assign the address of the array 'retbuf' to p, which must be of the
                appropriate pointer to array type. Doing:
                >
                p = retbuf[x];
                >
                will assign the _value_ of element 'x' of retbuf to p, which is probably
                not what you want. Doing:
                >
                p = &retbuf[x];
                >
                will assign the address of element 'x' of retbuf to p, which is what we
                want in this code posted up-thread.
                [...]

                And the last is equivalent to

                p = retbuf + x;

                where the "+" is doing pointer arithmetic. But

                p = &retbuf[x];

                is arguably clearer.

                --
                Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
                Nokia
                "We must do something. This is something. Therefore, we must do this."
                -- Antony Jay and Jonathan Lynn, "Yes Minister"

                Comment

                • CBFalconer

                  #9
                  Re: FAQ 20-12

                  mdh wrote:
                  >
                  .... snip ...
                  >
                  One last question. The line
                  >
                  p = &retbuf[sizeof(retbuf)-1];
                  >
                  where retbuf[] is declared as a static char array
                  I have no idea. Not enough code is shown. Bear in mind that
                  pointers to local data are invalid outside of the function (barring
                  static data).

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


                  Comment

                  • Richard Heathfield

                    #10
                    Re: FAQ 20-12

                    CBFalconer said:
                    mdh wrote:
                    >>
                    ... snip ...
                    >>
                    >One last question. The line
                    >>
                    > p = &retbuf[sizeof(retbuf)-1];
                    >>
                    >where retbuf[] is declared as a static char array
                    >
                    I have no idea. Not enough code is shown.
                    Looks like plenty to me.
                    Bear in mind that
                    pointers to local data are invalid outside of the function
                    He did say it's static.
                    (barring static data).
                    Right.

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

                      #11
                      Re: FAQ 20-12

                      CBFalconer <cbfalconer@yah oo.comwrites:
                      mdh wrote:
                      >>
                      ... snip ...
                      >>
                      >One last question. The line
                      >>
                      > p = &retbuf[sizeof(retbuf)-1];
                      >>
                      >where retbuf[] is declared as a static char array
                      >
                      I have no idea. Not enough code is shown. Bear in mind that
                      pointers to local data are invalid outside of the function (barring
                      static data).
                      There is more than enough information for the question asked.

                      Comment

                      Working...