array pointer question

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

    array pointer question

    let's say i have a 2D array, how would i access it with a pointer?

    int x [2][2];

    func(x);

    void func(int *x) {
    //i want to fill it with a number, like 1
    }

  • Dmitrii PapaGeorgio

    #2
    Re: array pointer question

    Dmitrii PapaGeorgio wrote:[color=blue]
    > let's say i have a 2D array, how would i access it with a pointer?
    >
    > int x [2][2];
    >
    > func(x);
    >
    > void func(int *x) {
    > //i want to fill it with a number, like 1
    > }
    >[/color]

    forgot...the array is being passed as func(x[0])

    Comment

    • WW

      #3
      Re: array pointer question

      Dmitrii PapaGeorgio wrote:[color=blue]
      > let's say i have a 2D array, how would i access it with a pointer?
      >
      > int x [2][2];
      >
      > func(x);
      >
      > void func(int *x) {
      > //i want to fill it with a number, like 1
      > }[/color]

      The above array looks like this:

      +---+---+---+---+
      |0,0|0,1|1,0|1, 1|
      +---+---+---+---+

      Each box represent an integer. The indices are n,m as: x[n][m].

      --
      WW aka Attila


      Comment

      • Dmitrii PapaGeorgio

        #4
        Re: array pointer question

        inside the function when i try to assign a value i get an error saying
        it's not a pointer or array like

        x[1][2] = 1;

        WW wrote:[color=blue]
        > Dmitrii PapaGeorgio wrote:
        >[color=green]
        >>let's say i have a 2D array, how would i access it with a pointer?
        >>
        >>int x [2][2];
        >>
        >>func(x);
        >>
        >>void func(int *x) {
        >>//i want to fill it with a number, like 1
        >>}[/color]
        >
        >
        > The above array looks like this:
        >
        > +---+---+---+---+
        > |0,0|0,1|1,0|1, 1|
        > +---+---+---+---+
        >
        > Each box represent an integer. The indices are n,m as: x[n][m].
        >[/color]

        Comment

        • Mike Wahler

          #5
          Re: array pointer question

          "Dmitrii PapaGeorgio" <vrenna@woh.rr. comNOSPAM> wrote in message
          news:zw0db.7757 4$xx4.7951757@t wister.neo.rr.c om...[color=blue]
          > let's say i have a 2D array, how would i access it with a pointer?
          >
          > int x [2][2];
          >
          > func(x);
          >
          > void func(int *x) {[/color]

          That type will only work for a single dimension array.
          [color=blue]
          > //i want to fill it with a number, like 1
          > }[/color]

          #include <cstdlib>
          #include <iostream>

          void func(int (*x)[2])
          {
          x[0][0] = 1;
          x[0][1] = 1;
          x[1][0] = 1;
          x[1][1] = 1;
          }

          int main()
          {
          int x [2][2] = {0};
          func(x);

          for(size_t i = 0; i < sizeof x / sizeof *x; ++i)
          for(size_t j = 0; j < sizeof *x / sizeof **x; ++j)
          std::cout << "x[" << i << "][" << j << "] == "
          << x[i][j] << '\n';

          return 0;
          }

          Why are you using an array instead of a container?

          -Mike
          [color=blue]
          >[/color]


          Comment

          • Jonathan Mcdougall

            #6
            Re: array pointer question

            >let's say i have a 2D array, how would i access it with a pointer?[color=blue]
            >
            >int x [2][2];
            >
            >func(x);
            >
            >void func(int *x) {
            >//i want to fill it with a number, like 1
            >}[/color]

            The same as an int :

            int x = 5;
            f(x);

            // takes an int
            void f(int i)
            {
            i = 2;
            }

            The reason for which 'i' is of type 'int' is because 'x' is of type
            int.

            Now if 'x' is of type 'int[2][2]', well 'i' will be too :

            void f(int i[2][2])
            {
            i[0][0] = 4;
            }


            But if you allocated the array with new, you probably did something
            like

            int size_1 = 2;
            int size_2 = 2;

            int **x = new int*[size_1]

            for ( int i = 0; i<size_1; ++i)
            x[i] = new int[size_2];


            Since 'x' is now of type 'int**', that is what 'i' will be :

            void f(int **i)
            {
            i[0][0] = 4;
            }

            The 'problem' with that is now you have no idea about the bounds of
            'i'. You have two solutions. Either you pass the information too :

            void f(int **i, int size_1, int size_2)
            {
            }

            or you use something like the zero-terminated c-style string and
            always set the last element to a given value which means end-of-array.

            But know that arrays are always passed by address, that is, modifying
            its value in f() will modify 'x' too.


            Jonathan

            Comment

            • Dmitrii PapaGeorgio

              #7
              Re: array pointer question

              i'm using array and not a container since my teacher is a fruit cake.
              thanks-

              Mike Wahler wrote:[color=blue]
              > "Dmitrii PapaGeorgio" <vrenna@woh.rr. comNOSPAM> wrote in message
              > news:zw0db.7757 4$xx4.7951757@t wister.neo.rr.c om...
              >[color=green]
              >>let's say i have a 2D array, how would i access it with a pointer?
              >>
              >>int x [2][2];
              >>
              >>func(x);
              >>
              >>void func(int *x) {[/color]
              >
              >
              > That type will only work for a single dimension array.
              >
              >[color=green]
              >>//i want to fill it with a number, like 1
              >>}[/color]
              >
              >
              > #include <cstdlib>
              > #include <iostream>
              >
              > void func(int (*x)[2])
              > {
              > x[0][0] = 1;
              > x[0][1] = 1;
              > x[1][0] = 1;
              > x[1][1] = 1;
              > }
              >
              > int main()
              > {
              > int x [2][2] = {0};
              > func(x);
              >
              > for(size_t i = 0; i < sizeof x / sizeof *x; ++i)
              > for(size_t j = 0; j < sizeof *x / sizeof **x; ++j)
              > std::cout << "x[" << i << "][" << j << "] == "
              > << x[i][j] << '\n';
              >
              > return 0;
              > }
              >
              > Why are you using an array instead of a container?
              >
              > -Mike
              >
              >
              >
              >[/color]

              Comment

              • WW

                #8
                Re: array pointer question

                Dmitrii PapaGeorgio wrote:[color=blue]
                > inside the function when i try to assign a value i get an error saying
                > it's not a pointer or array like
                > x[1][2] = 1;[/color]

                Because inside the function you have no array, you have a pointer to the
                first element of the array:

                +-+ +---+---+---+---+
                |x|-->|0,0|0,1|1,0|1 ,1|
                +-+ +---+---+---+---+
                0 1 2 3

                Each box represent an integer. The indices inside the boxes are n,m as:
                x[n][m].

                --
                WW aka Attila


                Comment

                • WW

                  #9
                  Re: array pointer question

                  Dmitrii PapaGeorgio wrote:[color=blue]
                  > i'm using array and not a container since my teacher is a fruit cake.
                  > thanks-[/color]
                  [SNIP]

                  Fruitcakes top post. Your teacher wants you to learn arrays.

                  --
                  WW aka Attila


                  Comment

                  • Kevin Goodsell

                    #10
                    Re: array pointer question

                    Jonathan Mcdougall wrote:
                    [color=blue]
                    > The same as an int :
                    >
                    > int x = 5;
                    > f(x);
                    >
                    > // takes an int
                    > void f(int i)
                    > {
                    > i = 2;
                    > }
                    >
                    > The reason for which 'i' is of type 'int' is because 'x' is of type
                    > int.
                    >
                    > Now if 'x' is of type 'int[2][2]', well 'i' will be too :[/color]

                    Actually, in nearly all contexts 'x' will be of type int (*)[2] (pointer
                    to array of two ints). Array names usually become pointers to the type
                    of the objects stored in the array. There are exceptions to this, for
                    example when the name is used as the operand of the sizeof or unary &
                    operators.
                    [color=blue]
                    >
                    > void f(int i[2][2])[/color]

                    Equivalent to

                    void f(int (*i)[2])
                    [color=blue]
                    > {
                    > i[0][0] = 4;
                    > }
                    >
                    >
                    > But if you allocated the array with new, you probably did something
                    > like
                    >
                    > int size_1 = 2;
                    > int size_2 = 2;
                    >
                    > int **x = new int*[size_1]
                    >
                    > for ( int i = 0; i<size_1; ++i)
                    > x[i] = new int[size_2];
                    >
                    >
                    > Since 'x' is now of type 'int**', that is what 'i' will be :
                    >
                    > void f(int **i)
                    > {
                    > i[0][0] = 4;
                    > }
                    >
                    > The 'problem' with that is now you have no idea about the bounds of
                    > 'i'.[/color]

                    This isn't a new problem. You had the same problem before:

                    void f(int i[2][2])
                    {
                    //...
                    }

                    This doesn't give the size of the array i (which isn't actually an
                    array), it just looks like it does. That first '2' is absolutely useless
                    - the compiler ignores it.

                    -Kevin
                    --
                    My email address is valid, but changes periodically.
                    To contact me please use the address from a recent posting.

                    Comment

                    • Mike Wahler

                      #11
                      Re: array pointer question


                      "Dmitrii PapaGeorgio" <vrenna@woh.rr. comNOSPAM> wrote in message
                      news:cz0db.7757 5$xx4.7952606@t wister.neo.rr.c om...[color=blue]
                      > Dmitrii PapaGeorgio wrote:[color=green]
                      > > let's say i have a 2D array, how would i access it with a pointer?
                      > >
                      > > int x [2][2];
                      > >
                      > > func(x);
                      > >
                      > > void func(int *x) {
                      > > //i want to fill it with a number, like 1
                      > > }
                      > >[/color]
                      >
                      > forgot...the array is being passed as func(x[0])[/color]

                      That means that a pointer to x[0][0] is being passed.

                      #include <iostream>
                      void func(int *x)
                      {
                      x[0] = 1;
                      x[1] = 1;
                      x[2] = 1;
                      x[3] = 1;
                      }

                      int main()
                      {
                      int x[2][2] = {0};
                      size_t i = 0;
                      size_t j = 0;
                      func(x[0]);
                      for(i = 0; i < sizeof x / sizeof *x; ++i)
                      for(j = 0; j < sizeof *x / sizeof **x; ++j)
                      std::cout << "x[" << i << "][" << j << "] == "
                      << x[i][j] << '\n';
                      return 0;
                      }

                      -Mike


                      }

                      -Mike


                      Comment

                      • Mike Wahler

                        #12
                        Re: array pointer question

                        "Dmitrii PapaGeorgio" <vrenna@woh.rr. comNOSPAM> wrote in message
                        news:bM0db.7757 8$xx4.7957122@t wister.neo.rr.c om...[color=blue]
                        > i'm using array and not a container since my teacher is a fruit cake.[/color]

                        It's rarely a good idea to use epithets to describe
                        someone who probably has much more knowledge than you do.
                        Are you sure your teacher doesn't read posts here?
                        If (s)he does, and sees this post from you, how do
                        you think you'll be treated in his/her class in the
                        future? Just a thought ....

                        Your post didn't mention anything about a teacher or
                        that you're taking a class. Your teacher is probably
                        trying to teach you about arrays. That doesn't make
                        him/her a 'fruitcake', but someone who is teaching
                        part of the language. Arrays do have their place
                        in C++ programs, but often a container is a better
                        choice for some tasks. That issue does not apply
                        here, since your array is just part of a learning
                        exercise and not a 'real' application.

                        BTW please don't top post.

                        -Mike


                        Comment

                        • Dmitrii PapaGeorgio

                          #13
                          Re: array pointer question

                          thanks guys - better lectures in here than in class...hope she doesn't
                          read this thread (teacher)

                          Kevin Goodsell wrote:[color=blue]
                          > Jonathan Mcdougall wrote:
                          >[color=green]
                          >> The same as an int :
                          >>
                          >> int x = 5;
                          >> f(x);
                          >>
                          >> // takes an int
                          >> void f(int i)
                          >> {
                          >> i = 2;
                          >> }
                          >>
                          >> The reason for which 'i' is of type 'int' is because 'x' is of type
                          >> int.
                          >>
                          >> Now if 'x' is of type 'int[2][2]', well 'i' will be too :[/color]
                          >
                          >
                          > Actually, in nearly all contexts 'x' will be of type int (*)[2] (pointer
                          > to array of two ints). Array names usually become pointers to the type
                          > of the objects stored in the array. There are exceptions to this, for
                          > example when the name is used as the operand of the sizeof or unary &
                          > operators.
                          >[color=green]
                          >>
                          >> void f(int i[2][2])[/color]
                          >
                          >
                          > Equivalent to
                          >
                          > void f(int (*i)[2])
                          >[color=green]
                          >> {
                          >> i[0][0] = 4;
                          >> }
                          >>
                          >>
                          >> But if you allocated the array with new, you probably did something
                          >> like
                          >>
                          >> int size_1 = 2;
                          >> int size_2 = 2;
                          >>
                          >> int **x = new int*[size_1]
                          >>
                          >> for ( int i = 0; i<size_1; ++i)
                          >> x[i] = new int[size_2];
                          >>
                          >>
                          >> Since 'x' is now of type 'int**', that is what 'i' will be :
                          >>
                          >> void f(int **i)
                          >> {
                          >> i[0][0] = 4;
                          >> }
                          >>
                          >> The 'problem' with that is now you have no idea about the bounds of
                          >> 'i'.[/color]
                          >
                          >
                          > This isn't a new problem. You had the same problem before:
                          >
                          > void f(int i[2][2])
                          > {
                          > //...
                          > }
                          >
                          > This doesn't give the size of the array i (which isn't actually an
                          > array), it just looks like it does. That first '2' is absolutely useless
                          > - the compiler ignores it.
                          >
                          > -Kevin[/color]

                          Comment

                          • Kevin Goodsell

                            #14
                            Re: array pointer question

                            Dmitrii PapaGeorgio wrote:[color=blue]
                            > thanks guys - better lectures in here than in class...hope she doesn't
                            > read this thread (teacher)[/color]

                            This is the third time this has been mentioned in this thread: Please
                            don't top-post. Read section 5 of the FAQ for posting guidelines.



                            -Kevin
                            --
                            My email address is valid, but changes periodically.
                            To contact me please use the address from a recent posting.

                            Comment

                            • Jonathan Mcdougall

                              #15
                              Re: array pointer question

                              > > Now if 'x' is of type 'int[2][2]', well 'i' will be too :[color=blue]
                              >
                              > Actually, in nearly all contexts 'x' will be of type int (*)[2] (pointer
                              > to array of two ints). Array names usually become pointers to the type
                              > of the objects stored in the array. There are exceptions to this, for
                              > example when the name is used as the operand of the sizeof or unary &
                              > operators.[/color]

                              Thanks for clarification.
                              [color=blue][color=green]
                              > > void f(int i[2][2])[/color]
                              >
                              > Equivalent to
                              >
                              > void f(int (*i)[2])[/color]

                              Yes, but not (imho) better/clearer.

                              [color=blue][color=green]
                              > > void f(int **i)
                              > > {
                              > > i[0][0] = 4;
                              > > }
                              > >
                              > > The 'problem' with that is now you have no idea about the bounds of
                              > > 'i'.[/color]
                              >
                              > This isn't a new problem. You had the same problem before:[/color]

                              Yes, but since you wrote the dimension manually before compiling, you
                              know its size and can use magic numbers/constants. That is what I meant.


                              Comment

                              Working...