an array function... - I need help

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • DDP3000@gmail.com

    an array function... - I need help

    void F(int **A, int N)
    {
    int i,j;
    for(i=0;i<N;i++ )
    for(j=0;j<N;j++ )
    A[i][j]=((i+j)%2==0)?1 :-1;
    }

    I have never used such a thing before, so it might be a really stupid
    question but I cannot find the answer.

    I have that function, but I don't know how to use it.
    How should I call this function? How can I pass the first parameter?

    Thank you
  • James Kuyper

    #2
    Re: an array function... - I need help

    DDP3000@gmail.c om wrote:
    void F(int **A, int N)
    {
    int i,j;
    for(i=0;i<N;i++ )
    for(j=0;j<N;j++ )
    A[i][j]=((i+j)%2==0)?1 :-1;
    }
    >
    I have never used such a thing before, so it might be a really stupid
    question but I cannot find the answer.
    >
    I have that function, but I don't know how to use it.
    How should I call this function? How can I pass the first parameter?
    >
    Thank you

    The simplest way is as follows:

    #define DIM 4

    int twod[DIM][DIM];
    int *pointers[DIM] = {twod[0], twod[1], twod[2], twod[3]};

    func(pointers, DIM);

    A more typical approach would be to dynamically allocate each element of
    pointers[] using malloc(). However, you would still call func() the same
    way, no matter how pointers[] is initialized.

    Comment

    • DDP3000@gmail.com

      #3
      Re: an array function... - I need help

      On Oct 24, 2:47 pm, James Kuyper <jameskuy...@ve rizon.netwrote:
      DDP3...@gmail.c om wrote:
      void F(int **A, int N)
      {
      int i,j;
      for(i=0;i<N;i++ )
      for(j=0;j<N;j++ )
      A[i][j]=((i+j)%2==0)?1 :-1;
      }
      >
      I have never used such a thing before, so it might be a really stupid
      question but I cannot find the answer.
      >
      I have that function, but I don't know how to use it.
      How should I call this function? How can I pass the first parameter?
      >
      Thank you
      >
      The simplest way is as follows:
      >
              #define DIM 4
      >
              int twod[DIM][DIM];
              int *pointers[DIM] = {twod[0], twod[1], twod[2], twod[3]};
      >
              func(pointers, DIM);
      >
      A more typical approach would be to dynamically allocate each element of
      pointers[] using malloc(). However, you would still call func() the same
      way, no matter how pointers[] is initialized.- Hide quoted text -
      >
      - Show quoted text -
      Thank you. It worked.
      But I have one more question, please.
      I ve printed the values and I realised that twod[0] points to the
      memory address of twod[0][0], twod[1] to twod[1][0] etc. I don't get
      it where twod[] is declared. Using &twod[][] works as well.

      Comment

      • jameskuyper@verizon.net

        #4
        Re: an array function... - I need help

        DDP3...@gmail.c om wrote:
        On Oct 24, 2:47�pm, James Kuyper <jameskuy...@ve rizon.netwrote:
        DDP3...@gmail.c om wrote:
        void F(int **A, int N)
        {
        int i,j;
        for(i=0;i<N;i++ )
        for(j=0;j<N;j++ )
        A[i][j]=((i+j)%2==0)?1 :-1;
        }
        I have never used such a thing before, so it might be a really stupid
        question but I cannot find the answer.
        I have that function, but I don't know how to use it.
        How should I call this function? How can I pass the first parameter?
        Thank you
        The simplest way is as follows:

        � � � � #define DIM 4

        � � � � int twod[DIM][DIM];
        � � � � int *pointers[DIM] = {twod[0], twod[1], twod[2], twod[3]};

        � � � � func(pointers, DIM);

        A more typical approach would be to dynamically allocate each element of
        pointers[] using malloc(). However, you would still call func() the same
        way, no matter how pointers[] is initialized.
        >
        Thank you. It worked.
        But I have one more question, please.
        I ve printed the values and I realised that twod[0] points to the
        memory address of twod[0][0], twod[1] to twod[1][0] etc. I don't get
        it where twod[] is declared. Using &twod[][] works as well.
        I sometimes use the notation pointers[] when writing about C code in
        English, to make it clear that "pointers" is a C array. If that's what
        you're doing, too, then twod[] is declared on the line which says:

        int twod[DIM][DIM];

        That answer seems too obvious, so I suspect that you're really asking
        a different question. Could you explain it in more detail?

        I'm not sure what you mean by your comment about &twod[][]. There's no
        context in which you could legally write that in a C program. However,
        &twod[0][1], for instance, is a perfectly valid int* which points at
        twod[0][1]. I'm not quite sure what you mean when you say it "works".
        How are you using it?

        Comment

        • DDP3000@gmail.com

          #5
          Re: an array function... - I need help

          On Oct 24, 8:32 pm, jameskuy...@ver izon.net wrote:
          DDP3...@gmail.c om wrote:
          On Oct 24, 2:47 pm, James Kuyper <jameskuy...@ve rizon.netwrote:
          DDP3...@gmail.c om wrote:
          void F(int **A, int N)
          {
          int i,j;
          for(i=0;i<N;i++ )
          for(j=0;j<N;j++ )
          A[i][j]=((i+j)%2==0)?1 :-1;
          }
          >
          I have never used such a thing before, so it might be a really stupid
          question but I cannot find the answer.
          >
          I have that function, but I don't know how to use it.
          How should I call this function? How can I pass the first parameter?
          >
          Thank you
          >
          The simplest way is as follows:
          >
          #define DIM 4
          >
          int twod[DIM][DIM];
          int *pointers[DIM] = {twod[0], twod[1], twod[2], twod[3]};
          >
          func(pointers, DIM);
          >
          A more typical approach would be to dynamically allocate each elementof
          pointers[] using malloc(). However, you would still call func() the same
          way, no matter how pointers[] is initialized.
          >
          Thank you. It worked.
          But I have one more question, please.
          I ve printed the values and I realised that twod[0] points to the
          memory address of twod[0][0], twod[1] to twod[1][0] etc. I don't get
          it where twod[] is declared. Using &twod[][] works as well.
          >
          I sometimes use the notation pointers[] when writing about C code in
          English, to make it clear that "pointers" is a C array. If that's what
          you're doing, too, then twod[] is declared on the line which says:
          >
                  int twod[DIM][DIM];
          >
          That answer seems too obvious, so I suspect that you're really asking
          a different question. Could you explain it in more detail?
          >
          I'm not sure what you mean by your comment about &twod[][]. There's no
          context in which you could legally write that in a C program. However,
          &twod[0][1], for instance, is a perfectly valid int* which points at
          twod[0][1]. I'm not quite sure what you mean when you say it "works".
          How are you using it?- Hide quoted text -
          >
          - Show quoted text -
          -I just omitted the content of the []. Sorry for the trouble.
          -I m not very experienced with pointers and arrays and I don't know if
          I understand it right.
          The twod[0] points to twod[0][0] and twod[0]+n to twod[0][n] but the
          next line of the array is not a continuation of the first line, it has
          a new "base" pointer twod[1] that points to twod[1][0] and twod[1]+n
          points to twod[1][n]. Right?
          -Instead of int* pointers[DIM] = {twod[0], twod[1], twod[2], twod[3]};
          I could use int *pointers[DIM] = {&twod[0][0], &twod[1][0], &twod[2]
          [0], &twod[3][0]}; Right?

          Comment

          • jameskuyper@verizon.net

            #6
            Re: an array function... - I need help

            DDP3...@gmail.c om wrote:
            On Oct 24, 8:32�pm, jameskuy...@ver izon.net wrote:
            DDP3...@gmail.c om wrote:
            On Oct 24, 2:47 pm, James Kuyper <jameskuy...@ve rizon.netwrote:
            ....
            #define DIM 4
            ....
            � � � � int twod[DIM][DIM];
            ....
            The twod[0] points to twod[0][0] and twod[0]+n to twod[0][n] but the
            next line of the array is not a continuation of the first line, it has
            a new "base" pointer twod[1] that points to twod[1][0] and twod[1]+n
            points to twod[1][n]. Right?
            Yes. Keep in mind that the array twod[1] must be come immediately
            after twod[0], so it's not a completely different "base" pointer.
            Therefore, you might expect that twod[0]+DIM must point at the same
            location as twod[1], and on most real systems those are two pointer
            which will compare equal and can both be dereferenced to access the
            same int object in memory. However, the standard says that
            dereferencing twod[0]+DIM has undefined behavior, so you should avoid
            writing code which relies upon doing so.
            -Instead of int* pointers[DIM] = {twod[0], twod[1], twod[2], twod[3]};
            I could use int *pointers[DIM] = {&twod[0][0], &twod[1][0], &twod[2]
            [0], &twod[3][0]}; Right?
            Correct; the meaning is exactly the same, your way just involves more
            typing than mine. Some people prefer the extra typing, in the belief
            that the meaning is clearer. The shorter method seems clearer to me,
            but that may be because I've been programming in C since 1978.

            Comment

            • DDP3000@gmail.com

              #7
              Re: an array function... - I need help

              Thank you!

              Comment

              • DDP3000@gmail.com

                #8
                Re: an array function... - I need help

                On 24 Ïêô, 23:53, jameskuy...@ver izon.net wrote:
                DDP3...@gmail.c om wrote:
                On Oct 24, 8:32 pm, jameskuy...@ver izon.net wrote:
                DDP3...@gmail.c om wrote:
                On Oct 24, 2:47 pm, James Kuyper <jameskuy...@ve rizon.netwrote:
                ...
                #define DIM 4
                ...
                int twod[DIM][DIM];
                ...
                The twod[0] points to twod[0][0] and twod[0]+n to twod[0][n] but the
                next line of the array is not a continuation of the first line, it has
                a new "base" pointer twod[1] that points to twod[1][0] and twod[1]+n
                points to twod[1][n]. Right?
                >
                Yes. Keep in mind that the array twod[1] must be come immediately
                after twod[0], so it's not a completely different "base" pointer.
                Therefore, you might expect that twod[0]+DIM must point at the same
                location as twod[1], and on most real systems those are two pointer
                which will compare equal and can both be dereferenced to access the
                same int object in memory. However, the standard says that
                dereferencing twod[0]+DIM has undefined behavior, so you should avoid
                writing code which relies upon doing so.
                >
                -Instead of int* pointers[DIM] = {twod[0], twod[1], twod[2], twod[3]};
                I could use int *pointers[DIM] = {&twod[0][0], &twod[1][0], &twod[2]
                [0], &twod[3][0]}; Right?
                >
                Correct; the meaning is exactly the same, your way just involves more
                typing than mine. Some people prefer the extra typing, in the belief
                that the meaning is clearer. The shorter method seems clearer to me,
                but that may be because I've been programming in C since 1978.
                Thank you

                Comment

                Working...