C FAQs 6.12

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

    #1

    C FAQs 6.12

    FAQ 6.12 http://c-faq.com/aryptr/aryvsadr.html :
    int a[10];
    a reference to "a" has type "pointer to int", and &a is "pointer to
    array of 10 ints".
    and it does not give any code on how to get "a pointer to the whole array"
    and what is the significance of such a pointer ?





    --


    Please remove capital 'V's when you reply to me via e-mail.

  • arnuld

    #2
    Re: C FAQs 6.12

    On Wed, 09 Apr 2008 14:28:44 +0500, arnuld wrote:
    and it does not give any code on how to get "a pointer to the whole array"
    and what is the significance of such a pointer ?
    I got some answers here: http://c-faq.com/aryptr/ptrtoarray.html

    but it doe snot tell why should I use "pointer to arrays" ?



    -- http://lispmachine.wordpress.com/

    Please remove capital 'V's when you reply to me via e-mail.

    Comment

    • Martin

      #3
      Re: C FAQs 6.12

      On Wed, 09 Apr 2008 10:34:10 +0100, arnuld <arnVuld@ippiVm ail.comwrote:
      I got some answers here: http://c-faq.com/aryptr/ptrtoarray.html
      >
      but it doe snot tell why should I use "pointer to arrays" ?
      It may be useful when you want to walk through arrays of arrays.

      --
      Martin

      Comment

      • Philip Potter

        #4
        Re: C FAQs 6.12

        arnuld wrote:
        >On Wed, 09 Apr 2008 14:28:44 +0500, arnuld wrote:
        >
        >and it does not give any code on how to get "a pointer to the whole array"
        >and what is the significance of such a pointer ?
        >
        I got some answers here: http://c-faq.com/aryptr/ptrtoarray.html
        >
        but it doe snot tell why should I use "pointer to arrays" ?
        >
        The first sentences of the link you quote are:
        "Usually, you don't want to. When people speak casually of a pointer to
        an array, they usually mean a pointer to its first element."

        I've never seen a pointer-to-array used. I can't tell you any reason why
        you should use them. They presumably exist for completeness.

        Philip

        Comment

        • Morris Dovey

          #5
          Re: C FAQs 6.12

          Philip Potter wrote:
          >
          arnuld wrote:
          On Wed, 09 Apr 2008 14:28:44 +0500, arnuld wrote:
          and it does not give any code on how to get "a pointer to the whole array"
          and what is the significance of such a pointer ?
          I got some answers here: http://c-faq.com/aryptr/ptrtoarray.html

          but it doe snot tell why should I use "pointer to arrays" ?
          >
          The first sentences of the link you quote are:
          "Usually, you don't want to. When people speak casually of a pointer to
          an array, they usually mean a pointer to its first element."
          >
          I've never seen a pointer-to-array used. I can't tell you any reason why
          you should use them. They presumably exist for completeness.
          There are occasions when they're useful. Some time back I wrote a
          "tokenize" function that took a pointer to a string and returned
          a pointer to an array of pointers to token strings.

          A bit later, I wrote another function that took a FILE pointer
          and used tokenize() on each line of the text file and returned a
          pointer to an array of pointers of the type returned by
          tokenize().

          I almost ran out of '*'s. :-)

          --
          Morris Dovey
          DeSoto Solar
          DeSoto, Iowa USA

          Comment

          • Martin

            #6
            Re: C FAQs 6.12

            On Wed, 09 Apr 2008 12:49:12 +0100, Morris Dovey <mrdovey@iedu.c omwrote:
            There are occasions when they're useful. Some time back I wrote a
            "tokenize" function that took a pointer to a string and returned
            a pointer to an array of pointers to token strings.
            >
            A bit later, I wrote another function that took a FILE pointer
            and used tokenize() on each line of the text file and returned a
            pointer to an array of pointers of the type returned by
            tokenize().
            I'm not sure where I'm seeing the 'pointer to array' as queried by the OP.
            You're talking about pointers to arrays of pointers, similar to argv.

            --
            Martin

            Comment

            • Morris Dovey

              #7
              Re: C FAQs 6.12

              Martin wrote:
              >
              On Wed, 09 Apr 2008 12:49:12 +0100, Morris Dovey <mrdovey@iedu.c omwrote:
              There are occasions when they're useful. Some time back I wrote a
              "tokenize" function that took a pointer to a string and returned
              a pointer to an array of pointers to token strings.

              A bit later, I wrote another function that took a FILE pointer
              and used tokenize() on each line of the text file and returned a
              pointer to an array of pointers of the type returned by
              tokenize().
              >
              I'm not sure where I'm seeing the 'pointer to array' as queried by the OP.
              You're talking about pointers to arrays of pointers, similar to argv.
              I thought the OP's query had already been well-answered, and was
              responding to Philip's comment that "pointer to array" might only
              exist for sake of syntactical completeness.

              My example actually resulted in a pointer to an array of pointers
              to arrays of pointers to strings to indicate that the construct
              was actually useful.

              Sorry for the noise/confusion.

              --
              Morris Dovey
              DeSoto Solar
              DeSoto, Iowa USA

              Comment

              • Philip Potter

                #8
                Re: C FAQs 6.12

                Morris Dovey wrote:
                Martin wrote:
                >On Wed, 09 Apr 2008 12:49:12 +0100, Morris Dovey <mrdovey@iedu.c omwrote:
                >>There are occasions when they're useful. Some time back I wrote a
                >>"tokenize" function that took a pointer to a string and returned
                >>a pointer to an array of pointers to token strings.
                >>>
                >>A bit later, I wrote another function that took a FILE pointer
                >>and used tokenize() on each line of the text file and returned a
                >>pointer to an array of pointers of the type returned by
                >>tokenize().
                >I'm not sure where I'm seeing the 'pointer to array' as queried by the OP.
                >You're talking about pointers to arrays of pointers, similar to argv.
                >
                I thought the OP's query had already been well-answered, and was
                responding to Philip's comment that "pointer to array" might only
                exist for sake of syntactical completeness.
                >
                My example actually resulted in a pointer to an array of pointers
                to arrays of pointers to strings to indicate that the construct
                was actually useful.
                >
                Sorry for the noise/confusion.
                You seemed to be talking about pointers to the first element of an
                array, not pointers to arrays proper. A char * can be a pointer to the
                first element of a char array, but a char (*)[] is a pointer to a char
                array. argv is a pointer to the first element of an array of pointers to
                the first character of a string. But since that's so convoluted people
                colloquially say argv is a pointer to an array of char. If we took this
                literally, that would mean it was char (*argv)[] instead of char **argv.

                Nevertheless Martin raised the good point about multidimensiona l arrays
                using pointers-to-arrays, which very much proved me wrong :)

                Philip

                Comment

                • Keith Thompson

                  #9
                  Re: C FAQs 6.12

                  Martin <m@b.cwrites:
                  On Wed, 09 Apr 2008 12:49:12 +0100, Morris Dovey <mrdovey@iedu.c omwrote:
                  >There are occasions when they're useful. Some time back I wrote a
                  >"tokenize" function that took a pointer to a string and returned
                  >a pointer to an array of pointers to token strings.
                  >>
                  >A bit later, I wrote another function that took a FILE pointer
                  >and used tokenize() on each line of the text file and returned a
                  >pointer to an array of pointers of the type returned by
                  >tokenize().
                  >
                  I'm not sure where I'm seeing the 'pointer to array' as queried by the
                  OP. You're talking about pointers to arrays of pointers, similar to
                  argv.
                  Strictly speaking, argv is not a pointer to arrays of pointers. It
                  doesn't point to an array; it points to the first element of an array.
                  It's an important distinction, since a pointer to an array and a
                  pointer to the first element of an array are two distinct thingsm,
                  both potentially useful.

                  Pointers to arrays (at least pointer values if not pointer objects)
                  show up in multidimensiona l arrays. For example:

                  int arr[10][20];
                  ...
                  arr[x][y];

                  arr is of type array 10 of array 20 of int, which decays to pointer to
                  array 10 of int (there's your pointer to an array).

                  arr[x] is of type array 10 of int, which decays to pointer to int.

                  arr[x][y] is of type int.

                  In most cases, though, a pointer to the first element of an array
                  (along with the separately remembered length of the array) is more
                  useful and flexible than a pointer to the whole array. You need an
                  element pointer for indexing (which after all is what arrays are for),
                  and the whole-array pointer implicitly contains the array's length
                  (which must be constant) in its type.

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

                  Comment

                  • Keith Thompson

                    #10
                    Re: C FAQs 6.12

                    Philip Potter <pgp@doc.ic.ac. ukwrites:
                    [...]
                    Nevertheless Martin raised the good point about multidimensiona l arrays
                    using pointers-to-arrays, which very much proved me wrong :)
                    Um, I thought that was me (unless Martin also mentioned it in another
                    message). No biggie.

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

                    Comment

                    • Philip Potter

                      #11
                      Re: C FAQs 6.12

                      Keith Thompson wrote:
                      Philip Potter <pgp@doc.ic.ac. ukwrites:
                      [...]
                      >Nevertheless Martin raised the good point about multidimensiona l arrays
                      >using pointers-to-arrays, which very much proved me wrong :)
                      >
                      Um, I thought that was me (unless Martin also mentioned it in another
                      message). No biggie.
                      >
                      He beat you to it: <op.t9cbabbiczh 7vb@ukmp4792.pb i.global.pvt>

                      Indeed, your message hadn't hit my server when I wrote mine above.

                      Comment

                      • Keith Thompson

                        #12
                        Re: C FAQs 6.12

                        Philip Potter <pgp@doc.ic.ac. ukwrites:
                        Keith Thompson wrote:
                        >Philip Potter <pgp@doc.ic.ac. ukwrites:
                        >[...]
                        >>Nevertheles s Martin raised the good point about multidimensiona l arrays
                        >>using pointers-to-arrays, which very much proved me wrong :)
                        >>
                        >Um, I thought that was me (unless Martin also mentioned it in another
                        >message). No biggie.
                        >>
                        He beat you to it: <op.t9cbabbiczh 7vb@ukmp4792.pb i.global.pvt>
                        >
                        Indeed, your message hadn't hit my server when I wrote mine above.
                        Ok, I concede. But I claim credit for going into more detail (unless
                        I got something wrong, in which case I'll deny the whole sordid affair
                        ever happened).

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

                        Comment

                        • Martin

                          #13
                          Re: C FAQs 6.12

                          On Wed, 09 Apr 2008 17:10:03 +0100, Keith Thompson <kst-u@mib.orgwrote:
                          Strictly speaking, argv is not a pointer to arrays of pointers. It
                          doesn't point to an array; it points to the first element of an array.
                          Keith,

                          I understand what you mean, but K&R2 describes argv as "a pointer to an
                          array of character strings" (K&R2, p114). I don't think they meant "array
                          pointer" though - and neither did I!

                          It's an important distinction, since a pointer to an array and a
                          pointer to the first element of an array are two distinct thingsm,
                          both potentially useful.
                          Ys, I hope that other messages on this thread have emphasised that
                          important distinction.

                          int arr[10][20];
                          ...
                          arr[x][y];
                          >
                          arr is of type array 10 of array 20 of int, which decays to pointer to
                          array 10 of int (there's your pointer to an array).
                          Whoops, I think you'll find 'arr' decays to "pointer to array of 20 ints."

                          arr[x] is of type array 10 of int, which decays to pointer to int.
                          arr[x] is of type array 20 of int, which decays to pointer to int,

                          arr[x][y] is of type int.
                          Indeed.

                          In most cases, though, a pointer to the first element of an array
                          (along with the separately remembered length of the array) is more
                          useful and flexible than a pointer to the whole array. You need an
                          element pointer for indexing (which after all is what arrays are for),
                          and the whole-array pointer implicitly contains the array's length
                          (which must be constant) in its type.
                          I wrote a program for this thread which showed the use of a true array
                          pointer, but it was rather contrived. As you say, C facilitates the use of
                          indices rather nicely for array accesses, multi-dimensional or otherwise,
                          so I didn't post it.

                          --
                          Martin

                          Comment

                          • Keith Thompson

                            #14
                            Re: C FAQs 6.12

                            Martin <m@b.cwrites:
                            On Wed, 09 Apr 2008 17:10:03 +0100, Keith Thompson <kst-u@mib.orgwrote:
                            >Strictly speaking, argv is not a pointer to arrays of pointers. It
                            >doesn't point to an array; it points to the first element of an array.
                            >
                            Keith,
                            >
                            I understand what you mean, but K&R2 describes argv as "a pointer to
                            an array of character strings" (K&R2, p114). I don't think they meant
                            "array pointer" though - and neither did I!
                            In my opinion, that's a mistake on their part.

                            [...]
                            > int arr[10][20];
                            > ...
                            > arr[x][y];
                            >>
                            >arr is of type array 10 of array 20 of int, which decays to pointer to
                            >array 10 of int (there's your pointer to an array).
                            >
                            Whoops, I think you'll find 'arr' decays to "pointer to array of 20 ints."
                            You're right, of course.
                            >arr[x] is of type array 10 of int, which decays to pointer to int.
                            >
                            arr[x] is of type array 20 of int, which decays to pointer to int,
                            And again, thanks for the correction.
                            >arr[x][y] is of type int.
                            >
                            Indeed.
                            [snip]

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

                            Comment

                            Working...