array to pointer decay question !!

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

    array to pointer decay question !!

    Hi,
    I know about the equivalence of pointer and arrays.But my doubt
    comes when its for multidimentiona l arrays.I have read the C faq but
    still have some doubts.

    Suppose I have a declaration as
    1. char array[x][x] or char array[][x]
    and it will get decayed to
    2. char *array[x]

    similarly if I have a declaration as
    3. char *argv[]
    it can decay to
    4. char **argv
    Though its at our prerogative the way we use it.


    Now from the above cases we see that,
    1. 2 dimentional array can get decayed to an array of char pointers.
    2. An array of char pointers can also be used as a pointer to pointer.

    If I am correct till now, what I mean to know is that though we have
    the cases above,a 2 dimentional array can never directly decay to a
    pointer to pointer.


    Correct me if I am wrong.

    regards
    pandapower
  • Joona I Palaste

    #2
    Re: array to pointer decay question !!

    pandapower <pandapower@sof thome.net> scribbled the following:[color=blue]
    > Hi,
    > I know about the equivalence of pointer and arrays.But my doubt
    > comes when its for multidimentiona l arrays.I have read the C faq but
    > still have some doubts.[/color]
    [color=blue]
    > Suppose I have a declaration as
    > 1. char array[x][x] or char array[][x]
    > and it will get decayed to
    > 2. char *array[x][/color]

    Yes.
    [color=blue]
    > similarly if I have a declaration as
    > 3. char *argv[]
    > it can decay to
    > 4. char **argv
    > Though its at our prerogative the way we use it.[/color]

    Yes.
    [color=blue]
    > Now from the above cases we see that,
    > 1. 2 dimentional array can get decayed to an array of char pointers.
    > 2. An array of char pointers can also be used as a pointer to pointer.[/color]
    [color=blue]
    > If I am correct till now, what I mean to know is that though we have
    > the cases above,a 2 dimentional array can never directly decay to a
    > pointer to pointer.[/color]

    That is true, i.e. it can never directly decay into a pointer to a
    pointer. The rule is, more or less:
    An array of type <T> can decay into a pointer to type <T>, but only
    once. This new, decayed type, can never further decay into anything.

    --
    /-- Joona Palaste (palaste@cc.hel sinki.fi) ------------- Finland --------\
    \-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
    "'I' is the most beautiful word in the world."
    - John Nordberg

    Comment

    • Peter Nilsson

      #3
      Re: array to pointer decay question !!

      "pandapower " <pandapower@Sof tHome.net> wrote in message
      news:3713912d.0 402020156.3ec7d 4bf@posting.goo gle.com...[color=blue]
      > Hi,
      > I know about the equivalence of pointer and arrays.[/color]

      They are not equivalent! ;)
      [color=blue]
      > But my doubt comes when its for multidimentiona l arrays.
      > I have read the C faq but still have some doubts.[/color]

      Try Steve Summit's course notes as additional reading...


      [color=blue]
      >
      > Suppose I have a declaration as
      > 1. char array[x][x] or char array[][x]
      > and it will get decayed to
      > 2. char *array[x][/color]

      If and what an array 'decays to' depends on the context, but 2 is wrong.

      char array[2];
      char *p = array; /* array decays to &array[0] */

      ....

      char array[2][5];
      char (*p)[5] = array; /* array decays to &array[0]
      // which is a pointer to an
      // array of 5 char
      */

      size_t size = sizeof array; /* array doesn't decay */
      char (*q)[2][5] = &array; /* array doesn't decay */

      The unary & and sizeof operators are exempt, when array is the only operand.
      [color=blue]
      > similarly if I have a declaration as
      > 3. char *argv[]
      > it can decay to
      > 4. char **argv
      > Though its at our prerogative the way we use it.
      >
      > Now from the above cases we see that,
      > 1. 2 dimentional array can get decayed to an array of char pointers.[/color]


      Now you appear to be talking about function parameters, not 'ordinary'
      objects. In such cases, arrays don't 'decay' as a such. It's just that there
      happen to be two different syntax methods for the _same_ thing...

      int main(int argc, char *argv[]);
      int main(int argc, char **argv);

      void foo(int array[20][50]);
      void foo(int array[][50]);
      void foo(int (*array)[50]);

      void bar(double array[20][50][7]);
      void bar(double array[][50][7]);
      void bar(double (*array)[50][7]);

      C does not pass arrays by value (i.e. a copy of the whole array is not
      passed), it only passes pointers to sequences of objects. So, the array
      syntax in function parameter declarations are actually pointer declarations.
      [color=blue]
      > 2. An array of char pointers can also be used as a pointer to pointer.[/color]

      Vaguely yes. A named object which is an array decays to a pointer.
      [color=blue]
      > If I am correct till now, what I mean to know is that though we have
      > the cases above,a 2 dimentional array can never directly decay to a
      > pointer to pointer.[/color]

      Correct. A two dimensional array can only decay to a pointer to an array.

      --
      Peter


      Comment

      • Martin Dickopp

        #4
        Re: array to pointer decay question !!

        pandapower@Soft Home.net (pandapower) writes:
        [color=blue]
        > I know about the equivalence of pointer and arrays.[/color]

        There is no such equivalence. The relation between pointers and arrays
        is that if an array is used in a value context, it is converted to a
        pointer to its first element.
        [color=blue]
        > Suppose I have a declaration as
        > 1. char array[x][x] or char array[][x]
        > and it will get decayed to
        > 2. char *array[x][/color]

        Yes, if by "decay to" you mean "is converted to in a value context."
        [color=blue]
        > similarly if I have a declaration as
        > 3. char *argv[]
        > it can decay to
        > 4. char **argv[/color]

        Well, I don't think saying it *can* decay is correct. In a value
        context, it is *always* converted.

        Also, note the difference between the types of `a' and `b' in these
        examples:

        void foo (void) { char a [42]; }

        void bar (char b []) { }

        `a' has type array-of-char, and gets converted to a pointer in a value
        context. `b' already has type pointer-to-char, because there's special
        rule for parameter declarations. In a parameter declaration (and only
        there!), the innermost [] operator is just a fancy way to declare a
        pointer.
        [color=blue]
        > Now from the above cases we see that,
        > 1. 2 dimentional array can get decayed to an array of char pointers.
        > 2. An array of char pointers can also be used as a pointer to pointer.
        >
        > If I am correct till now, what I mean to know is that though we have
        > the cases above,a 2 dimentional array can never directly decay to a
        > pointer to pointer.[/color]

        Correct. All that happens is that `array of T' is converted to `pointer
        to T' in a value context. This is also true if `T' is itself an array,
        so `array of array of U' is converted to `pointer to array of U'.

        Martin

        Comment

        • Arjan Kenter

          #5
          Re: array to pointer decay question !!



          Joona I Palaste wrote:[color=blue]
          > pandapower <pandapower@sof thome.net> scribbled the following:
          >[color=green]
          >>Hi,
          >> I know about the equivalence of pointer and arrays.But my doubt
          >>comes when its for multidimentiona l arrays.I have read the C faq but
          >>still have some doubts.[/color]
          >
          >[color=green]
          >>Suppose I have a declaration as
          >>1. char array[x][x] or char array[][x]
          >>and it will get decayed to
          >>2. char *array[x][/color]
          >
          >
          > Yes.[/color]

          No, it decays to char (*array)[x], which is very different:
          it's a pointer to an array of chars, while your 2nd declaration
          is an array of pointers to char.
          [color=blue][color=green]
          >>similarly if I have a declaration as
          >>3. char *argv[]
          >>it can decay to
          >>4. char **argv
          >>Though its at our prerogative the way we use it.[/color]
          >
          >
          > Yes.
          >
          >[color=green]
          >>Now from the above cases we see that,
          >>1. 2 dimentional array can get decayed to an array of char pointers.[/color][/color]

          No, it decays to a pointer to array to char.
          [color=blue][color=green]
          >>2. An array of char pointers can also be used as a pointer to pointer.[/color][/color]

          Yes, indeed.
          [color=blue][color=green]
          >>If I am correct till now, what I mean to know is that though we have
          >>the cases above,a 2 dimentional array can never directly decay to a
          >>pointer to pointer.[/color]
          >
          >
          > That is true, i.e. it can never directly decay into a pointer to a
          > pointer. The rule is, more or less:
          > An array of type <T> can decay into a pointer to type <T>, but only
          > once. This new, decayed type, can never further decay into anything.[/color]

          Unless you repeat the trick:

          int x[10][42];
          int *y = (x + 1)[2];

          x + 1 has type: int (*x)[42] (decayed once)
          (x + 1)[2] has type: int * (decayed again)

          But of course our wonderful FAQ will make all this much clearer.

          --
          ir. H.J.H.N. Kenter ^^
          Electronic Design & Tools oo ) Philips Research Labs
          Building WAY 3.23 =x= \ arjan.kenter@ph ilips.com
          Prof. Holstlaan 4 (WAY31) | \ tel. +31 40 27 45334
          5656 AA Eindhoven /|__ \ tfx. +31 40 27 44626
          The Netherlands (____)_/ http://www.kenter.demon.nl/

          Famous last words: Segmentation Fault (core dumped)

          Comment

          • Horst Kraemer

            #6
            Re: array to pointer decay question !!

            On 2 Feb 2004 01:56:12 -0800, pandapower@Soft Home.net (pandapower)
            wrote:
            [color=blue]
            > Hi,
            > I know about the equivalence of pointer and arrays.[/color]

            There isn't such a thing. The correct formulation would be: If used in
            an expression (except in the expressions sizeof array or &array) an
            array of <T> decays to (is implicitly converted to) a pointer to <T>
            which points to the initial array element.

            But my doubt[color=blue]
            > comes when its for multidimentiona l arrays.I have read the C faq but
            > still have some doubts.
            >
            > Suppose I have a declaration as
            > 1. char array[x][x] or char array[][x]
            > and it will get decayed to
            > 2. char *array[x][/color]

            No. The expression 'array' will decay to a pointer which may be
            produced by the declaration

            char (*array) [x]

            i.e. pointer to array of x chars. Your definition

            char *array[x]

            declares a array of x pointers to char which is a very different
            thing.

            char array[2][5]

            is by definition an array of 2 (arrays of 5 chars). Read from left to
            right. The first, i.e. only the top 'array' decays to 'pointer'. Same
            for

            char array [2][3][4];

            array of 2 (arrays of 3 (arrays of 4 chars))

            This decays to

            pointer to (array of (3 arrays of 4 chars)), i.e. to

            char (*array) [3][4]

            and not to

            char * array [3][4]

            ( array of 3 (arrays of 4 pointers to char) )


            --
            Horst

            Comment

            Working...