pointer to an int array

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

    pointer to an int array

    Hi Folks,


    #include<stdio. h>
    int main()
    {
    int (*p)[10];
    int arr[10];
    int i;

    p = arr; /* <-- compiler warning */
    for(i=0; i <= 12; i++){ /* i <=12 delibrately written code */
    *((*p) + i) = i;

    printf("%d\n", p[0][i]);
    }

    return 0;
    }

    In the above program compiler is giving following warning

    $gcc pointer2array.c
    pointer2array.c : In function `main':
    pointer2array.c :8: warning: assignment from incompatible pointer type

    Why the warning?

    O'kay and what is the use of int (*p)[10] type of declaration ?
    Does the compiler perform any check on the array bounds?
    lets say if i declare int arr[12]...
    -Neo


  • Jack Klein

    #2
    Re: pointer to an int array

    On Mon, 3 Jan 2005 12:44:42 +0530, "Neo" <timeless_illus ion@yahoo.com>
    wrote in comp.lang.c:
    [color=blue]
    > Hi Folks,
    >
    >
    > #include<stdio. h>
    > int main()
    > {
    > int (*p)[10];
    > int arr[10];
    > int i;
    >
    > p = arr; /* <-- compiler warning */[/color]

    The name of an array in an expression, other than when it is the
    operand of the 'sizeof' or '&' operators, is converted to a pointer to
    its first element. So in the statement above, you are assigning the
    address of an int (arr[0]) to a pointer to an array. But arr[0] is an
    int, not an array of ints.

    Replace with:

    p = &arr;
    [color=blue]
    > for(i=0; i <= 12; i++){ /* i <=12 delibrately written code */
    > *((*p) + i) = i;
    >
    > printf("%d\n", p[0][i]);
    > }
    >
    > return 0;
    > }
    >
    > In the above program compiler is giving following warning
    >
    > $gcc pointer2array.c
    > pointer2array.c : In function `main':
    > pointer2array.c :8: warning: assignment from incompatible pointer type
    >
    > Why the warning?
    >
    > O'kay and what is the use of int (*p)[10] type of declaration ?
    > Does the compiler perform any check on the array bounds?
    > lets say if i declare int arr[12]...[/color]

    --
    Jack Klein
    Home: http://JK-Technology.Com
    FAQs for
    comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
    comp.lang.c++ http://www.parashift.com/c++-faq-lite/
    alt.comp.lang.l earn.c-c++

    Comment

    • Neo

      #3
      Re: pointer to an int array


      "Jack Klein" <jackklein@spam cop.net> wrote in message
      news:dbuht0ldaa 72vr2a1d9nlhgtl 35k95gfju@4ax.c om...[color=blue]
      > On Mon, 3 Jan 2005 12:44:42 +0530, "Neo" <timeless_illus ion@yahoo.com>
      > wrote in comp.lang.c:
      >[color=green]
      >> Hi Folks,
      >>
      >>
      >> #include<stdio. h>
      >> int main()
      >> {
      >> int (*p)[10];
      >> int arr[10];
      >> int i;
      >>
      >> p = arr; /* <-- compiler warning */[/color]
      >
      > The name of an array in an expression, other than when it is the
      > operand of the 'sizeof' or '&' operators, is converted to a pointer to
      > its first element. So in the statement above, you are assigning the
      > address of an int (arr[0]) to a pointer to an array. But arr[0] is an
      > int, not an array of ints.
      >
      > Replace with:
      >
      > p = &arr;
      >[/color]

      O'kay, dats fine.
      if I change the declaration above as :

      int (*p)[]; <-- pointer2array.c :12: error: invalid use of array with
      unspecified bounds
      int arr[10];
      p = &arr;
      for(i=0; i <= 12; i++){
      *((*p) + i) = i;
      printf("%d\n", p[0][i]);
      }

      compiler error!
      what's the use of array index in the above declaration?
      as i can go upto 12 or more...

      -Neo


      [color=blue][color=green]
      >> for(i=0; i <= 12; i++){ /* i <=12 delibrately written code */
      >> *((*p) + i) = i;
      >>
      >> printf("%d\n", p[0][i]);
      >> }
      >>
      >> return 0;
      >> }
      >>
      >> In the above program compiler is giving following warning
      >>
      >> $gcc pointer2array.c
      >> pointer2array.c : In function `main':
      >> pointer2array.c :8: warning: assignment from incompatible pointer type
      >>
      >> Why the warning?
      >>
      >> O'kay and what is the use of int (*p)[10] type of declaration ?
      >> Does the compiler perform any check on the array bounds?
      >> lets say if i declare int arr[12]...[/color]
      >
      > --
      > Jack Klein
      > Home: http://JK-Technology.Com
      > FAQs for
      > comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
      > comp.lang.c++ http://www.parashift.com/c++-faq-lite/
      > alt.comp.lang.l earn.c-c++
      > http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html[/color]


      Comment

      • Mark McIntyre

        #4
        Re: pointer to an int array

        On Mon, 3 Jan 2005 14:01:44 +0530, in comp.lang.c , "Neo"
        <timeless_illus ion@yahoo.com> wrote:
        [color=blue]
        >if I change the declaration above as :
        >
        > int (*p)[]; <-- pointer2array.c :12: error: invalid use of array with unspecified bounds
        >
        >compiler error![/color]

        yes, its an error
        [color=blue]
        >what's the use of array index in the above declaration?[/color]

        none - its an illegal construct
        [color=blue]
        >as i can go upto 12 or more...[/color]

        only by invoking undefined behaviour....

        Don't do that.
        --
        Mark McIntyre
        CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
        CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt >

        ----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==----
        http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
        ----= East and West-Coast Server Farms - Total Privacy via Encryption =----

        Comment

        • gooch

          #5
          Re: pointer to an int array

          You can index up to whatever value you want. p[1000] is fine. The
          result is going to be undefined. In most cases you would get a seg
          fault but that may not be the case. You have to do the bounds checking
          yourself.

          Comment

          • Richard Bos

            #6
            Re: pointer to an int array

            "gooch" <gooch001@comca st.net> wrote:

            [ Using Google-Broken-Beta is not an excuse not to show any context.
            Learn to use it to quote properly or get a real newsreader. Context
            reinstated. ]
            [color=blue]
            > "Neo" <timeless_illus ion@yahoo.com> wrote:
            >[color=green]
            > > if I change the declaration above as :
            > >
            > > int (*p)[]; <-- pointer2array.c :12: error: invalid use of array with
            > > unspecified bounds
            > > int arr[10];
            > > p = &arr;
            > > for(i=0; i <= 12; i++){
            > > *((*p) + i) = i;
            > > printf("%d\n", p[0][i]);
            > > }
            > >
            > > compiler error!
            > > what's the use of array index in the above declaration?
            > > as i can go upto 12 or more...[/color][/color]
            [color=blue]
            > You can index up to whatever value you want. p[1000] is fine. The
            > result is going to be undefined.[/color]

            I don't see how you can write those sentences together. The result of
            p[10] and over are indeed going to be undefined, which is exactly why
            such indices are _not_ fine.
            [color=blue]
            > In most cases you would get a seg fault but that may not be the case.[/color]

            If you're lucky, you'll get a segfault. If you're unlucky, you won't see
            the segfault, but your customer will. If you're _really_ unlucky,
            neither of you will get a segfault, but your program will slowly
            scribble over all your customer's data, and he'll sue you. Don't run
            over the end of your arrays, not even when you think "it'll be safe,
            because you'll get a segfault anyway".

            Richard

            Comment

            • gooch

              #7
              Re: pointer to an int array

              "I don't see how you can write those sentences together. ....."

              I am not saying it is a good thing or that you should do it, only that
              it is not a legal operation.

              "If you're lucky, you'll get a segfault. ....."

              Again I am not suggesting he assume a segfault will occur, only that he
              needs to do the bounds checking himself.

              Comment

              • Mark McIntyre

                #8
                Re: pointer to an int array

                On 3 Jan 2005 10:19:39 -0800, in comp.lang.c , "gooch"
                <gooch001@comca st.net> wrote:

                (contextless stuff)

                please read the VERY FIRST THING that richard said in his last post to you.

                --
                Mark McIntyre
                CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
                CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt >

                ----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==----
                http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
                ----= East and West-Coast Server Farms - Total Privacy via Encryption =----

                Comment

                • Keith Thompson

                  #9
                  Re: pointer to an int array

                  "gooch" <gooch001@comca st.net> writes:[color=blue]
                  > You can index up to whatever value you want. p[1000] is fine. The
                  > result is going to be undefined. In most cases you would get a seg
                  > fault but that may not be the case. You have to do the bounds checking
                  > yourself.[/color]

                  (I presume p is an array with fewer than 1001 elements.)

                  p[1000] is not "fine" in any sense of the word. It is a serious error
                  that the implementation is not required to diagnose. This can
                  sometimes have the same visible results as something that's perfectly
                  legal, but the distinction is critical.

                  Incidentally, it's not true that "in most cases you would get a seg
                  fault". Attempting to access an array beyond its bounds is very
                  likely to refer to some other variable in your program, with
                  unpredictable results.

                  Your overall point, that you have to do your own bounds checking
                  because the implementation won't do it for you, is quite correct; I'm
                  just (over)reacting to your use of the word "fine".

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

                  • Stan Milam

                    #10
                    Re: pointer to an int array

                    Jack Klein wrote:
                    [color=blue]
                    > On Mon, 3 Jan 2005 12:44:42 +0530, "Neo" <timeless_illus ion@yahoo.com>
                    > wrote in comp.lang.c:
                    >
                    >[color=green]
                    >>Hi Folks,
                    >>
                    >>
                    >>#include<stdi o.h>
                    >>int main()
                    >>{
                    >> int (*p)[10];
                    >> int arr[10];
                    >> int i;
                    >>
                    >> p = arr; /* <-- compiler warning */[/color]
                    >
                    >
                    > The name of an array in an expression, other than when it is the
                    > operand of the 'sizeof' or '&' operators, is converted to a pointer to
                    > its first element. So in the statement above, you are assigning the
                    > address of an int (arr[0]) to a pointer to an array. But arr[0] is an
                    > int, not an array of ints.
                    >
                    > Replace with:
                    >
                    > p = &arr;
                    >
                    >[color=green]
                    >> for(i=0; i <= 12; i++){ /* i <=12 delibrately written code */
                    >> *((*p) + i) = i;
                    >>
                    >> printf("%d\n", p[0][i]);
                    >> }
                    >>
                    >> return 0;
                    >>}
                    >>
                    >>In the above program compiler is giving following warning
                    >>
                    >>$gcc pointer2array.c
                    >>pointer2array .c: In function `main':
                    >>pointer2array .c:8: warning: assignment from incompatible pointer type
                    >>
                    >>Why the warning?
                    >>
                    >>O'kay and what is the use of int (*p)[10] type of declaration ?
                    >>Does the compiler perform any check on the array bounds?
                    >>lets say if i declare int arr[12]...[/color][/color]

                    Okay, both of you are mistaken. int (*p)[10] is declaring an array of
                    10 pointers to function that return integers. Not quite the same thing
                    as either of you are talking about.

                    --
                    Regards,
                    Stan Milam.
                    -----------------------------------------------------------------------------
                    Vita Brevis. Carpe Guitarum! - Jamie Kinscherff
                    -----------------------------------------------------------------------------

                    Comment

                    • Neo

                      #11
                      Re: pointer to an int array


                      "Stan Milam" <stmilam@swbell .net> wrote in message
                      news:5EpCd.1071 6$wi2.5209@news svr11.news.prod igy.com...[color=blue]
                      > Jack Klein wrote:
                      >[color=green]
                      >> On Mon, 3 Jan 2005 12:44:42 +0530, "Neo" <timeless_illus ion@yahoo.com>
                      >> wrote in comp.lang.c:
                      >>
                      >>[color=darkred]
                      >>>Hi Folks,
                      >>>
                      >>>
                      >>>#include<std io.h>
                      >>>int main()
                      >>>{
                      >>> int (*p)[10];
                      >>> int arr[10];
                      >>> int i;
                      >>>
                      >>> p = arr; /* <-- compiler warning */[/color]
                      >>
                      >>
                      >> The name of an array in an expression, other than when it is the
                      >> operand of the 'sizeof' or '&' operators, is converted to a pointer to
                      >> its first element. So in the statement above, you are assigning the
                      >> address of an int (arr[0]) to a pointer to an array. But arr[0] is an
                      >> int, not an array of ints.
                      >>
                      >> Replace with:
                      >>
                      >> p = &arr;
                      >>
                      >>[color=darkred]
                      >>> for(i=0; i <= 12; i++){ /* i <=12 delibrately written code */
                      >>> *((*p) + i) = i;
                      >>>
                      >>> printf("%d\n", p[0][i]);
                      >>> }
                      >>>
                      >>> return 0;
                      >>>}
                      >>>
                      >>>In the above program compiler is giving following warning
                      >>>
                      >>>$gcc pointer2array.c
                      >>>pointer2arra y.c: In function `main':
                      >>>pointer2arra y.c:8: warning: assignment from incompatible pointer type
                      >>>
                      >>>Why the warning?
                      >>>
                      >>>O'kay and what is the use of int (*p)[10] type of declaration ?
                      >>>Does the compiler perform any check on the array bounds?
                      >>>lets say if i declare int arr[12]...[/color][/color]
                      >
                      > Okay, both of you are mistaken. int (*p)[10] is declaring an array of 10
                      > pointers to function that return integers. Not quite the same thing as
                      > either of you are talking about.
                      >
                      > --
                      > Regards,
                      > Stan Milam.
                      > -----------------------------------------------------------------------------
                      > Vita Brevis. Carpe Guitarum! - Jamie Kinscherff
                      > -----------------------------------------------------------------------------[/color]

                      pointers to function???
                      hmm.... its a pointer to arrary of 10 INTs!
                      -Neo


                      Comment

                      • Jens.Toerring@physik.fu-berlin.de

                        #12
                        Re: pointer to an int array

                        Stan Milam <stmilam@swbell .net> wrote:[color=blue]
                        > Okay, both of you are mistaken. int (*p)[10] is declaring an array of
                        > 10 pointers to function that return integers. Not quite the same thing
                        > as either of you are talking about.[/color]

                        Sorry, but no.

                        int ( *p )[ 10 ];

                        is a pointer to an array of 10 ints. The parentheses here are needed to
                        distinguish it from

                        int *p[ 10 ];

                        which would be an array of 10 pointers to int. An array of 10 pointers
                        to functions that return int would be

                        int ( *p[ 10 ] )( );

                        Regards, Jens
                        --
                        \ Jens Thoms Toerring ___ Jens.Toerring@p hysik.fu-berlin.de
                        \______________ ____________ http://www.toerring.de

                        Comment

                        • Lawrence Kirby

                          #13
                          Re: pointer to an int array

                          On Mon, 03 Jan 2005 12:44:42 +0530, Neo wrote:
                          [color=blue]
                          > Hi Folks,
                          >
                          >
                          > #include<stdio. h>
                          > int main()
                          > {
                          > int (*p)[10];
                          > int arr[10];
                          > int i;
                          >
                          > p = arr; /* <-- compiler warning */
                          > for(i=0; i <= 12; i++){ /* i <=12 delibrately written code */
                          > *((*p) + i) = i;
                          >
                          > printf("%d\n", p[0][i]);
                          > }
                          >
                          > return 0;
                          > }
                          >
                          > In the above program compiler is giving following warning
                          >
                          > $gcc pointer2array.c
                          > pointer2array.c : In function `main':
                          > pointer2array.c :8: warning: assignment from incompatible pointer type
                          >
                          > Why the warning?[/color]

                          Becuase you are trying to assign a value of type int * to a variable of
                          type int (*)[10]. C doesn't support implicit conversion between these
                          types. You could write

                          p = &arr;

                          but there isn't anything obvious to be gained over defining p as int *.
                          [color=blue]
                          > O'kay and what is the use of int (*p)[10] type of declaration ?[/color]

                          You could use this in something like the following

                          int arr2[5][10];
                          int (*p2)[10] = arr2;

                          Then accessing p2[x][y] will access the element arr2[x][y]
                          [color=blue]
                          > Does
                          > the compiler perform any check on the array bounds? lets say if i
                          > declare int arr[12]...[/color]

                          C doesn't require any sort of bounds checking from the compiler, and most
                          don't attempt to.

                          Lawrence

                          Comment

                          • S.Tobias

                            #14
                            Re: pointer to an int array

                            Mark McIntyre <markmcintyre@s pamcop.net> wrote:[color=blue]
                            > On Mon, 3 Jan 2005 14:01:44 +0530, in comp.lang.c , "Neo"
                            > <timeless_illus ion@yahoo.com> wrote:[/color]
                            [color=blue][color=green]
                            > >if I change the declaration above as :
                            > >
                            > > int (*p)[]; <-- pointer2array.c :12: error: invalid use of array with unspecified bounds
                            > >
                            > >compiler error![/color][/color]
                            [color=blue]
                            > yes, its an error[/color]

                            IMHO I think you're wrong here. `p' is simply a pointer to incomplete type
                            ("array of int of unknown size").

                            The OP probably hadn't noticed that the error was at different line:
                            printf("%d\n", p[0][i]); //error
                            Exactly, the offending expression is p[0], because, it gets translated
                            to:
                            *(p + 0)
                            in which the summation cannot be evaluated because `*p' has an incomplete
                            type (however I fail to explain exactly why, because this expression doesn't
                            seem to violate any constraints at the "+" operator; could anyone help?).

                            The above expression can be corrected with:
                            (*p)[i];
                            This is interesting, because we seem to apply indirection operator to
                            pointer to incomplete type (again, this is not a constraint violation,
                            however it doesn't work with pointers to incomplete struct type; could
                            someone help explain this please?); but here lvalue `*p', which is
                            of "array of int" type, is converted to "pointer to int" and points
                            to its first element, essentially same location as the array itself;
                            this rule seems to take precedence.

                            [color=blue][color=green]
                            > >what's the use of array index in the above declaration?[/color][/color]

                            Type `int[]' is compatible with `int[10]' and `int[20]',
                            so `p' could be assigned an address of objects defined as:
                            int a[15];
                            int b[80];
                            double d[10]; //except this object
                            p = &a;
                            p = &b;
                            p = &d; //error
                            However, since `p' is a pointer to incomplete type, no arithmetic or
                            comparisons can be made with it.
                            I think one can describe `p' as a generic pointer to "arrays of int".
                            I don't know if it could be more useful than that.
                            [color=blue][color=green]
                            > >as i can go upto 12 or more...[/color][/color]
                            [color=blue]
                            > only by invoking undefined behaviour....[/color]

                            Yes.

                            --
                            Stan Tobias
                            mailx `echo siXtY@FamOuS.Be dBuG.pAlS.INVALID | sed s/[[:upper:]]//g`

                            Comment

                            • Lawrence Kirby

                              #15
                              Re: pointer to an int array

                              On Fri, 07 Jan 2005 22:31:16 +0000, S.Tobias wrote:
                              [color=blue]
                              > Mark McIntyre <markmcintyre@s pamcop.net> wrote:[color=green]
                              >> On Mon, 3 Jan 2005 14:01:44 +0530, in comp.lang.c , "Neo"
                              >> <timeless_illus ion@yahoo.com> wrote:[/color]
                              >[color=green][color=darkred]
                              >> >if I change the declaration above as :
                              >> >
                              >> > int (*p)[]; <-- pointer2array.c :12: error: invalid use of array with unspecified bounds
                              >> >
                              >> >compiler error![/color][/color]
                              >[color=green]
                              >> yes, its an error[/color]
                              >
                              > IMHO I think you're wrong here. `p' is simply a pointer to incomplete type
                              > ("array of int of unknown size").
                              >
                              > The OP probably hadn't noticed that the error was at different line:
                              > printf("%d\n", p[0][i]); //error
                              > Exactly, the offending expression is p[0], because, it gets translated
                              > to:
                              > *(p + 0)
                              > in which the summation cannot be evaluated because `*p' has an incomplete
                              > type (however I fail to explain exactly why, because this expression doesn't
                              > seem to violate any constraints at the "+" operator; could anyone help?).[/color]

                              In both C90 and C99 the + operator requires a pointer operand to have a
                              pointer to an object type; an incomplete type is not an object type. This
                              is a constraint.

                              Lawrence

                              Comment

                              Working...