Passing INT Array by reference

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

    Passing INT Array by reference

    Can you pass an int array by reference to a function and modify
    selective elements?

    Here is my code:

    #include <stdio.h>

    #define COLUMNSIZE 30
    #define ASIZE 5
    int calcfldpos(int *row, int *column, int *numArray)
    {
    int i=0;
    printf("\tbefor e-->++numArray=%d\ n",*numArray );
    *numArray+=4;
    printf("\t after-->++numArray=%d\ n",*numArray );
    return(1);
    }
    int updintArray(int *numArray[5])
    {
    int i=0;
    printf("\nupdin tArray\n");
    for ( i=0; i<ASIZE; i++ )
    {
    printf("numArra y[%d]=%d\n",i,numArr ay[i]);
    *numArray[i]+=1;
    printf("numArra y[%d]=%d\n\n",i,numA rray[i]);
    }
    return(0);
    }
    int main(int argc,char** argv)
    {
    int row=10;
    int column=0;
    int i=0;
    int numArray[ASIZE];

    numArray[0]=0; numArray[1]=1; numArray[2]=2; numArray[3]=3;
    numArray[4]=4;

    for ( i=0; i<ASIZE; i++ )
    {
    printf("numArra y[%d]=%d\n",i,numArr ay[i]);
    }

    printf("before->calcfldpos:\tn umArray[2] = %d\n",numArray[2]);
    calcfldpos(&row ,&column,&numAr ray[2]);
    printf(" after->calcfldpos:\tn umArray[2] = %d\n",numArray[2]);
    for ( i=0; i<ASIZE; i++ )
    {
    printf("numArra y[%d]=%d\n",i,numArr ay[i]);
    }
    printf("Now updintArray\n") ;
    updintArray(&nu mArray);
    return(0);
    }

    I'm getting a core dumb on this statement:
    *numArray[i]+=1;

    I do not know how the pointer are messed up?
    Any comment would be appreciated.

  • Barry Schwarz

    #2
    Re: Passing INT Array by reference

    On 14 Jul 2005 13:59:51 -0700, "Jeff K" <jfkearnssr@gma il.com> wrote:
    [color=blue]
    >Can you pass an int array by reference to a function and modify
    >selective elements?[/color]

    C passes by value exclusively. The technique used for arrays does
    allow the called function to updated the arrays directly.
    [color=blue]
    >
    >Here is my code:
    >[/color]

    snip
    [color=blue]
    >int updintArray(int *numArray[5])
    >{
    > int i=0;
    > printf("\nupdin tArray\n");
    > for ( i=0; i<ASIZE; i++ )
    > {
    > printf("numArra y[%d]=%d\n",i,numArr ay[i]);
    > *numArray[i]+=1;
    > printf("numArra y[%d]=%d\n\n",i,numA rray[i]);
    > }
    > return(0);
    >}
    >int main(int argc,char** argv)
    >{
    > int row=10;
    > int column=0;
    > int i=0;
    > int numArray[ASIZE];
    >[/color]

    snip
    [color=blue]
    > for ( i=0; i<ASIZE; i++ )
    > {
    > printf("numArra y[%d]=%d\n",i,numArr ay[i]);
    > }
    > printf("Now updintArray\n") ;[/color]

    Please learn to indent consistently. It will save you (and us) a lot
    of aggravation.
    [color=blue]
    > updintArray(&nu mArray);
    > return(0);
    >}
    >
    >I'm getting a core dumb on this statement:
    > *numArray[i]+=1;[/color]

    How did you get past the compilation stage. updint is expecting an
    array of 5 pointer to int (int *[5]). You pass it a pointer to an
    array of 5 int (int (*)[5]). These types are not compatible an should
    result in some diagnostic.[color=blue]
    >
    >I do not know how the pointer are messed up?
    >Any comment would be appreciated.[/color]



    <<Remove the del for email>>

    Comment

    • Suman

      #3
      Re: Passing INT Array by reference



      Barry Schwarz wrote:[color=blue]
      > On 14 Jul 2005 13:59:51 -0700, "Jeff K" <jfkearnssr@gma il.com> wrote:
      >[color=green]
      > >Can you pass an int array by reference to a function and modify
      > >selective elements?[/color]
      >
      > C passes by value exclusively. The technique used for arrays does
      > allow the called function to updated the arrays directly.
      >[color=green]
      > >
      > >Here is my code:
      > >[/color]
      >
      > snip
      >[color=green]
      > >int updintArray(int *numArray[5])
      > >{
      > > int i=0;
      > > printf("\nupdin tArray\n");
      > > for ( i=0; i<ASIZE; i++ )
      > > {
      > > printf("numArra y[%d]=%d\n",i,numArr ay[i]);
      > > *numArray[i]+=1;
      > > printf("numArra y[%d]=%d\n\n",i,numA rray[i]);
      > > }
      > > return(0);
      > >}
      > >int main(int argc,char** argv)
      > >{
      > > int row=10;
      > > int column=0;
      > > int i=0;
      > > int numArray[ASIZE];
      > >[/color]
      >
      > snip
      >[color=green]
      > > for ( i=0; i<ASIZE; i++ )
      > > {
      > > printf("numArra y[%d]=%d\n",i,numArr ay[i]);
      > > }
      > > printf("Now updintArray\n") ;[/color]
      >
      > Please learn to indent consistently. It will save you (and us) a lot
      > of aggravation.
      >[color=green]
      > > updintArray(&nu mArray);
      > > return(0);
      > >}
      > >
      > >I'm getting a core dumb on this statement:
      > > *numArray[i]+=1;[/color]
      >
      > How did you get past the compilation stage. updint is expecting an
      > array of 5 pointer to int (int *[5]). You pass it a pointer to an
      > array of 5 int (int (*)[5]). These types are not compatible an should
      > result in some diagnostic.[/color]

      He didn't.[color=blue][color=green]
      > >
      > >I do not know how the pointer are messed up?
      > >Any comment would be appreciated.[/color][/color]

      Here are my efforts at fixing the code:
      The warnings I found with gcc 4.0.0 are embedded in the code as well.

      #include <stdio.h>

      #define COLUMNSIZE 30
      #define ASIZE 5
      int
      calcfldpos(int *row, int *column, int *numArray)
      {
      /* was: unused variable
      int i = 0;
      */
      printf("\tbefor e-->++numArray= %d\n", *numArray);
      *numArray += 4;
      printf("\t after-->++numArray=%d\ n", *numArray);
      return (1);

      }

      int
      /* updintArray(int *numArray[5]) */
      updintArray(int numArray[5])
      {
      int i = 0;
      printf("\nupdin tArray\n");
      for (i = 0; i < ASIZE; i++) {
      /* was: format '%d' expects type 'int', but argument 3
      has type 'int *' */
      printf("numArra y[%d]=%d\n", i, numArray[i]);
      /* *numArray[i] += 1; */
      numArray[i] += 1;
      /* was: format '%d' expects type 'int', but argument 3
      has type 'int *' */
      printf("numArra y[%d]=%d\n\n", i, numArray[i]);
      }
      return (0);
      }

      int
      /* main(int argc, char **argv) */
      main(void)
      {
      int row = 10;
      int column = 0;
      int i = 0;
      int numArray[ASIZE];
      numArray[0] = 0;
      numArray[1] = 1;
      numArray[2] = 2;
      numArray[3] = 3;
      numArray[4] = 4;

      for (i = 0; i < ASIZE; i++) {
      printf("numArra y[%d]=%d\n", i, numArray[i]);
      }

      printf("before->calcfldpos:\ tn umArray[2] = %d\n",
      numArray[2]);
      calcfldpos(&row , &column, &numArray[2]);
      printf(" after->calcfldpos:\tn umArray[2 ] = %d\n",
      numArray[2]);
      for (i = 0; i < ASIZE; i++) {
      printf("numArra y[%d]=%d\n", i, numArray[i]);
      }
      printf("Now updintArray\n") ;
      /* passing argument 1 of 'updintArray' from incompatible
      pointer type
      updintArray(&nu mArray);
      */
      updintArray(num Array);
      return (0);
      }

      This was compiled with:
      gcc -std=c99 -Wall -pedantic -ansi test.c

      Whether this is what the OP wanted to try out, I am not too sure.

      Comment

      • ranjeet.gupta@gmail.com

        #4
        Re: Passing INT Array by reference



        Jeff K wrote:[color=blue]
        > Can you pass an int array by reference to a function and modify
        > selective elements?[/color]

        Call by refrence is In C++ not in C, In C their is only call by value
        and below also in your code you have called by value :-)

        you have passed the copy of address, so passing the address (call
        by value) we modify the content in the passed address
        (call by value),

        I hope now you are clear that there is nothing such as call by
        refrence in C, Yes In C++ we have the Call by Refrence, Dont get
        confuse....any way i did some modification in the code and
        placed my comment also.
        [color=blue]
        >
        > Here is my code:
        >
        > #include <stdio.h>
        >
        > #define COLUMNSIZE 30
        > #define ASIZE 5
        > int calcfldpos(int *row, int *column, int *numArray)
        > {
        > int i=0;
        > printf("\tbefor e-->++numArray=%d\ n",*numArray );
        > *numArray+=4;
        > printf("\t after-->++numArray=%d\ n",*numArray );
        > return(1);
        > }
        > int updintArray(int *numArray[5])[/color]
        ^^^^^^^^^^^
        change to *numArray // Why specifing index ?? when you
        // are passing the base address of
        the
        // array in the main function.[color=blue]
        > {[/color]

        int *numArrayPtr = numArray; // Declare the Pointer
        // Which holds the
        address
        // of the passed
        argument.[color=blue]
        > int i=0;
        > printf("\nupdin tArray\n");
        > for ( i=0; i<ASIZE; i++ )
        > {
        > printf("numArra y[%d]=%d\n",i,numArr ay[i]);
        > *numArray[i]+=1;[/color]
        ^^^^^^^^^^^^^^
        This will compile But Give the
        memory allocation problem......

        So solution is that you have to take the pointer varibale and replace
        the above with this
        *numArrayPtr +=1;

        Regards
        Ranjeet
        [color=blue]
        > printf("numArra y[%d]=%d\n\n",i,numA rray[i]);
        > }
        > return(0);
        > }
        > int main(int argc,char** argv)
        > {
        > int row=10;
        > int column=0;
        > int i=0;
        > int numArray[ASIZE];
        >
        > numArray[0]=0; numArray[1]=1; numArray[2]=2; numArray[3]=3;
        > numArray[4]=4;
        >
        > for ( i=0; i<ASIZE; i++ )
        > {
        > printf("numArra y[%d]=%d\n",i,numArr ay[i]);
        > }
        >
        > printf("before->calcfldpos:\tn umArray[2] = %d\n",numArray[2]);
        > calcfldpos(&row ,&column,&numAr ray[2]);
        > printf(" after->calcfldpos:\tn umArray[2] = %d\n",numArray[2]);
        > for ( i=0; i<ASIZE; i++ )
        > {
        > printf("numArra y[%d]=%d\n",i,numArr ay[i]);
        > }
        > printf("Now updintArray\n") ;
        > updintArray(&nu mArray);
        > return(0);
        > }
        >
        > I'm getting a core dumb on this statement:
        > *numArray[i]+=1;
        >
        > I do not know how the pointer are messed up?
        > Any comment would be appreciated.[/color]

        Comment

        • Emmanuel Delahaye

          #5
          Re: Passing INT Array by reference

          Jeff K wrote on 14/07/05 :[color=blue]
          > Can you pass an int array by reference to a function and modify
          > selective elements?[/color]

          Well, all parameters are passed by value in C. Arrays are kinda
          special. WHat is passed is a copy of its address or of the address of
          it's first element via some pointer to array or to element
          rescpectively.

          #1

          f ((T *)arr[10])
          {
          }

          #2
          f (T arr[10])
          {
          }
          or
          f (T arr[])
          {
          }
          or
          f (T *arr)
          {
          }


          The first way (pointer to array) keeps the size info, and the second
          one (pointer to element) has no size info. An extra size parameter
          should help...


          #include <stdio.h>

          typedef int T;

          int f (T (*arr)[10])
          {
          printf ("sizeof arr[0] = %u\n", (unsigned) sizeof *arr[0]);
          printf ("sizeof arr = %u\n", (unsigned) sizeof *arr);
          return 0;
          }

          int g (T *arr)
          {
          printf ("sizeof arr[0] = %u\n", (unsigned) sizeof arr[0]);
          printf ("sizeof arr = %u\n", (unsigned) sizeof arr);
          return 0;
          }


          int main (void)
          {

          T a[10];

          f (&a);
          g (a);
          return 0;
          }

          --
          Emmanuel
          The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
          The C-library: http://www.dinkumware.com/refxc.html

          "Clearly your code does not meet the original spec."
          "You are sentenced to 30 lashes with a wet noodle."
          -- Jerry Coffin in a.l.c.c++

          Comment

          • Emmanuel Delahaye

            #6
            Re: Passing INT Array by reference

            Jeff K wrote on 14/07/05 :[color=blue]
            > <snipped some buggy code>[/color]

            Code fixed and enhanced. Ask for details if you don't understand...

            #include <stdio.h>

            /* useful and reusable trick to get the number of elements of an array
            * (I insist : *array*) ...
            */
            #define NELEM(a) (sizeof(a)/sizeof*(a))

            int calcfldpos (int *numArray)
            {
            printf ("\tbefore-->++numArray=%d\ n", *numArray);
            *numArray += 4;
            printf ("\t after-->++numArray=%d\ n", *numArray);
            return (1);
            }

            int updintArray (int *numArray, size_t size)
            {
            size_t i;
            printf ("\nupdintArray \n");
            for (i = 0; i < size; i++)
            {
            printf ("numArray[%d]=%d\n", i, numArray[i]);
            numArray[i] += 1;
            printf ("numArray[%d]=%d\n\n", i, numArray[i]);
            }
            return (0);
            }

            int main ()
            {

            int numArray[5] = {0,1,2,3,4};
            size_t i;

            for (i = 0; i < NELEM(numArray) ; i++)
            {
            printf ("numArray[%d]=%d\n", i, numArray[i]);
            }

            printf ("before->calcfldpos:\tn umArray[2] = %d\n", numArray[2]);
            calcfldpos (numArray);
            printf (" after->calcfldpos:\tn umArray[2] = %d\n", numArray[2]);
            for (i = 0; i < NELEM(numArray) ; i++)
            {
            printf ("numArray[%d]=%d\n", i, numArray[i]);
            }
            printf ("Now updintArray\n") ;
            updintArray (numArray, NELEM(numArray) );
            return (0);
            }

            --
            Emmanuel
            The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
            The C-library: http://www.dinkumware.com/refxc.html

            "Mal nommer les choses c'est ajouter du malheur au
            monde." -- Albert Camus.

            Comment

            • Barry Schwarz

              #7
              Re: Passing INT Array by reference

              On 14 Jul 2005 21:59:13 -0700, "Suman" <skarpio@gmail. com> wrote:
              [color=blue]
              >
              >
              >Barry Schwarz wrote:[color=green]
              >> On 14 Jul 2005 13:59:51 -0700, "Jeff K" <jfkearnssr@gma il.com> wrote:[/color][/color]
              snip[color=blue][color=green][color=darkred]
              >> >int updintArray(int *numArray[5])
              >> >{[/color][/color][/color]
              snip[color=blue][color=green][color=darkred]
              >> >}
              >> >int main(int argc,char** argv)
              >> >{
              >> > int row=10;
              >> > int column=0;
              >> > int i=0;
              >> > int numArray[ASIZE];[/color][/color][/color]
              snip[color=blue][color=green][color=darkred]
              >> > updintArray(&nu mArray);
              >> > return(0);
              >> >}
              >> >
              >> >I'm getting a core dumb on this statement:
              >> > *numArray[i]+=1;[/color]
              >>
              >> How did you get past the compilation stage. updint is expecting an
              >> array of 5 pointer to int (int *[5]). You pass it a pointer to an
              >> array of 5 int (int (*)[5]). These types are not compatible an should
              >> result in some diagnostic.[/color]
              >
              >He didn't.[/color]

              Look again. numArray is an array of int. &numArray has type pointer
              to array of int. The call to updintArray has an argument with an
              incompatible type which requires a diagnostic.


              <<Remove the del for email>>

              Comment

              • Suman

                #8
                Re: Passing INT Array by reference



                Barry Schwarz wrote:[color=blue]
                > On 14 Jul 2005 21:59:13 -0700, "Suman" <skarpio@gmail. com> wrote:
                >[color=green]
                > >
                > >
                > >Barry Schwarz wrote:[color=darkred]
                > >> On 14 Jul 2005 13:59:51 -0700, "Jeff K" <jfkearnssr@gma il.com> wrote:[/color][/color]
                > snip[color=green][color=darkred]
                > >> >int updintArray(int *numArray[5])
                > >> >{[/color][/color]
                > snip[color=green][color=darkred]
                > >> >}
                > >> >int main(int argc,char** argv)
                > >> >{
                > >> > int row=10;
                > >> > int column=0;
                > >> > int i=0;
                > >> > int numArray[ASIZE];[/color][/color]
                > snip[color=green][color=darkred]
                > >> > updintArray(&nu mArray);
                > >> > return(0);
                > >> >}
                > >> >
                > >> >I'm getting a core dumb on this statement:
                > >> > *numArray[i]+=1;
                > >>
                > >> How did you get past the compilation stage. updint is expecting an
                > >> array of 5 pointer to int (int *[5]). You pass it a pointer to an
                > >> array of 5 int (int (*)[5]). These types are not compatible an should
                > >> result in some diagnostic.[/color]
                > >
                > >He didn't.[/color]
                >
                > Look again. numArray is an array of int. &numArray has type pointer
                > to array of int. The call to updintArray has an argument with an
                > incompatible type which requires a diagnostic.[/color]

                What I meant was, he didn't get through the compilation stage.
                I quoted more than necessary, which caused the confusion. I compiled
                his code as is, and found some errors/warnings, which I *tried* to
                fix (since I am not too sure of his intent), and posted.

                Any confusion caused is regretted.

                Suman.

                Comment

                Working...