Passing by reference

Discussion in 'C Programming' started by LL, Feb 28, 2009.

  1. LL

    LL Guest

    Please comment on the following code for style and accuracy. More
    specifically, could you clarify whether passing by reference using
    pointer and the function's subsequent usage were done correctly?

    #include <stdio.h>

    int a_[]={1,2,3,4,5};
    int* a_ptr=a_;
    int b[]={5,4,3,2,1};

    // Passing ptr to array by reference
    void subtract_arr(int*& arr1, int* arr2) {
    for (int i=0; i<5; i++) {
    arr1-=arr2;
    }
    }

    // Passing by reference using pointers
    void subtract_arr(int** arr1, int* arr2) {
    for (int i=0; i<5; i++) {
    arr1-=arr2;
    }
    }

    main() {
    subtract_arr(a_ptr,b);
    for (int j=0; j<5; j++) {
    printf("%d ", a_[j]); // -4 -2 0 2 4
    }

    subtract_arr(&a_ptr,b);
    for (int j=0; j<5; j++) {
    printf("%d ", a_[j]); // same output
    }
    }
     
    LL, Feb 28, 2009
    #1
    1. Advertising

  2. LL

    user923005 Guest

    On Feb 27, 11:58 pm, LL <> wrote:
    > Please comment on the following code for style and accuracy. More
    > specifically, could you clarify whether passing by reference using
    > pointer and the function's subsequent usage were done correctly?
    >
    > #include <stdio.h>
    >
    > int a_[]={1,2,3,4,5};
    > int* a_ptr=a_;
    > int b[]={5,4,3,2,1};
    >
    > // Passing ptr to array by reference
    > void subtract_arr(int*& arr1, int* arr2) {
    >   for (int i=0; i<5; i++) {
    >     arr1-=arr2;
    >   }
    >
    > }
    >
    > // Passing by reference using pointers
    > void subtract_arr(int** arr1, int* arr2) {
    >   for (int i=0; i<5; i++) {
    >     arr1-=arr2;
    >   }
    >
    > }
    >
    > main() {
    >   subtract_arr(a_ptr,b);
    >   for (int j=0; j<5; j++) {
    >     printf("%d ", a_[j]); // -4 -2 0 2 4
    >   }
    >
    >   subtract_arr(&a_ptr,b);
    >   for (int j=0; j<5; j++) {
    >     printf("%d ", a_[j]); // same output
    >   }
    >
    > }


    This is neither valid C nor valid C++.
    There are no references in C.
    In C, everything is passed by value, so we can crudely simulate a
    reference by passing a copy of the address.
     
    user923005, Feb 28, 2009
    #2
    1. Advertising

  3. LL

    LL Guest

    On Sat, 28 Feb 2009 00:06:51 -0800, user923005 wrote:

    > On Feb 27, 11:58 pm, LL <> wrote:
    >> Please comment on the following code for style and accuracy. More
    >> specifically, could you clarify whether passing by reference using
    >> pointer and the function's subsequent usage were done correctly?
    >>
    >> #include <stdio.h>
    >>
    >> int a_[]={1,2,3,4,5};
    >> int* a_ptr=a_;
    >> int b[]={5,4,3,2,1};
    >>
    >> // Passing ptr to array by reference
    >> void subtract_arr(int*& arr1, int* arr2) {
    >>   for (int i=0; i<5; i++) {
    >>     arr1-=arr2;
    >>   }
    >>
    >> }
    >>
    >> // Passing by reference using pointers void subtract_arr(int** arr1,
    >> int* arr2) {
    >>   for (int i=0; i<5; i++) {
    >>     arr1-=arr2;
    >>   }
    >>
    >> }
    >>
    >> main() {
    >>   subtract_arr(a_ptr,b);
    >>   for (int j=0; j<5; j++) {
    >>     printf("%d ", a_[j]); // -4 -2 0 2 4
    >>   }
    >>
    >>   subtract_arr(&a_ptr,b);
    >>   for (int j=0; j<5; j++) {
    >>     printf("%d ", a_[j]); // same output
    >>   }
    >>
    >> }

    >
    > This is neither valid C nor valid C++. There are no references in C.
    > In C, everything is passed by value, so we can crudely simulate a
    > reference by passing a copy of the address.


    It compiles using g++. Could you also correct my terminology if that's
    possible?
     
    LL, Feb 28, 2009
    #3
  4. LL

    LL Guest

    On Sat, 28 Feb 2009 07:58:49 +0000, LL wrote:

    > Please comment on the following code for style and accuracy. More
    > specifically, could you clarify whether passing by reference using
    > pointer and the function's subsequent usage were done correctly?
    >
    > #include <stdio.h>
    >
    > int a_[]={1,2,3,4,5};
    > int* a_ptr=a_;
    > int b[]={5,4,3,2,1};
    >
    > // Passing ptr to array by reference
    > void subtract_arr(int*& arr1, int* arr2) {
    > for (int i=0; i<5; i++) {
    > arr1-=arr2;
    > }
    > }
    >
    > // Passing by reference using pointers void subtract_arr(int** arr1,
    > int* arr2) {
    > for (int i=0; i<5; i++) {
    > arr1-=arr2;
    > }
    > }
    >
    > main() {
    > subtract_arr(a_ptr,b);
    > for (int j=0; j<5; j++) {
    > printf("%d ", a_[j]); // -4 -2 0 2 4
    > }
    >


    > subtract_arr(&a_ptr,b);
    > for (int j=0; j<5; j++) {
    > printf("%d ", a_[j]); // same output
    > }

    Actually this part is meaningless.

    > }
     
    LL, Feb 28, 2009
    #4
  5. LL

    LL Guest

    On Sat, 28 Feb 2009 07:58:49 +0000, LL wrote:

    > Please comment on the following code for style and accuracy. More
    > specifically, could you clarify whether passing by reference using
    > pointer and the function's subsequent usage were done correctly?
    >
    > #include <stdio.h>
    >
    > int a_[]={1,2,3,4,5};
    > int* a_ptr=a_;
    > int b[]={5,4,3,2,1};
    >
    > // Passing ptr to array by reference
    > void subtract_arr(int*& arr1, int* arr2) {
    > for (int i=0; i<5; i++) {
    > arr1-=arr2;
    > }
    > }
    >
    > // Passing by reference using pointers void subtract_arr(int** arr1,
    > int* arr2) {
    > for (int i=0; i<5; i++) {
    > arr1-=arr2;
    > }
    > }
    >
    > main() {
    > subtract_arr(a_ptr,b);
    > for (int j=0; j<5; j++) {
    > printf("%d ", a_[j]); // -4 -2 0 2 4
    > }
    >
    > subtract_arr(&a_ptr,b);
    > for (int j=0; j<5; j++) {
    > printf("%d ", a_[j]); // same output
    > }
    > }


    How to rewrite passing by reference using pointers? Is there a such thing?

    #include <stdio.h>

    int a_[]={1,2,3,4,5};
    int* a_ptr=a_;
    int b[]={5,4,3,2,1};

    // Passing ptr to array by reference
    void subtract_arr(int*& arr1, int* arr2) {
    for (int i=0; i<5; i++) {
    arr1-=arr2;
    }
    }

    // Passing by reference using pointers
    void subtract_arr(int** arr1, int* arr2) {
    /* Have to rewrite this part */
    for (int i=0; i<5; i++) {
    arr1-=arr2;
    }
    for (int j=0; j<5; j++) {
    printf("%d ", arr1[j]);
    }
    }

    main() {
    subtract_arr(a_ptr,b);
    for (int j=0; j<5; j++) {
    printf("%d ", a_[j]); // -4 -2 0 2 4
    }

    subtract_arr(&a_ptr,b);
    }
     
    LL, Feb 28, 2009
    #5
  6. LL

    Guest

    On 28 Feb, 08:12, LL <> wrote:
    > On Sat, 28 Feb 2009 00:06:51 -0800, user923005 wrote:
    > > On Feb 27, 11:58 pm, LL <> wrote:


    > >> Please comment on the following code for style and accuracy. More
    > >> specifically, could you clarify whether passing by reference using
    > >> pointer and the function's subsequent usage were done correctly?

    >
    > >> #include <stdio.h>

    >
    > >> int a_[]={1,2,3,4,5};
    > >> int* a_ptr=a_;
    > >> int b[]={5,4,3,2,1};

    >
    > >> // Passing ptr to array by reference
    > >> void subtract_arr(int*& arr1, int* arr2) {
    > >> for (int i=0; i<5; i++) {
    > >> arr1-=arr2;
    > >> }

    >
    > >> }

    >
    > >> // Passing by reference using pointers void subtract_arr(int** arr1,
    > >> int* arr2) {
    > >> for (int i=0; i<5; i++) {
    > >> arr1-=arr2;
    > >> }

    >
    > >> }

    >
    > >> main() {


    this is not valid C++ it should be

    int main()

    > >> subtract_arr(a_ptr,b);
    > >> for (int j=0; j<5; j++) {
    > >> printf("%d ", a_[j]); // -4 -2 0 2 4
    > >> }

    >
    > >> subtract_arr(&a_ptr,b);
    > >> for (int j=0; j<5; j++) {
    > >> printf("%d ", a_[j]); // same output
    > >> }

    >
    > >> }

    >
    > > This is neither valid C nor valid C++. There are no references in C.
    > > In C, everything is passed by value, so we can crudely simulate a
    > > reference by passing a copy of the address.

    >
    > It compiles using g++.


    which is a C++ compiler.
    If you want a C++ answer please repost to comp.lang.c++

    > Could you also correct my terminology if that's
    > possible.


    your subtract function could be re-written in C as

    void subtract_arr(int *arr1, int *arr2)
    {
    int i;
    for (i = 0; i < 5; i++)
    arr1 -= arr2;
    }

    int main (void)
    {
    int a [] = {1, 2, 3, 4, 5};
    int b [] = {1, 1, 1, 1, 1};
    subtract_arr (a, b);
    return 0;
    }
     
    , Feb 28, 2009
    #6
  7. LL

    LL Guest

    On Sat, 28 Feb 2009 09:48:31 +0000, LL wrote:

    > On Sat, 28 Feb 2009 07:58:49 +0000, LL wrote:
    >
    >> Please comment on the following code for style and accuracy. More
    >> specifically, could you clarify whether passing by reference using
    >> pointer and the function's subsequent usage were done correctly?
    >>
    >> #include <stdio.h>
    >>
    >> int a_[]={1,2,3,4,5};
    >> int* a_ptr=a_;
    >> int b[]={5,4,3,2,1};
    >>
    >> // Passing ptr to array by reference
    >> void subtract_arr(int*& arr1, int* arr2) {
    >> for (int i=0; i<5; i++) {
    >> arr1-=arr2;
    >> }
    >> }
    >>
    >> // Passing by reference using pointers void subtract_arr(int** arr1,
    >> int* arr2) {
    >> for (int i=0; i<5; i++) {
    >> arr1-=arr2;
    >> }
    >> }
    >>
    >> main() {
    >> subtract_arr(a_ptr,b);
    >> for (int j=0; j<5; j++) {
    >> printf("%d ", a_[j]); // -4 -2 0 2 4
    >> }
    >>
    >> subtract_arr(&a_ptr,b);
    >> for (int j=0; j<5; j++) {
    >> printf("%d ", a_[j]); // same output
    >> }
    >> }

    >
    > How to rewrite passing by reference using pointers? Is there a such
    > thing?
    >
    > #include <stdio.h>
    >
    > int a_[]={1,2,3,4,5};
    > int* a_ptr=a_;
    > int b[]={5,4,3,2,1};
    >
    > // Passing ptr to array by reference
    > void subtract_arr(int*& arr1, int* arr2) {
    > for (int i=0; i<5; i++) {
    > arr1-=arr2;
    > }
    > }
    >
    > // Passing by reference using pointers void subtract_arr(int** arr1,
    > int* arr2) {
    > /* Have to rewrite this part */
    > for (int i=0; i<5; i++) {
    > arr1-=arr2;
    > }
    > for (int j=0; j<5; j++) {
    > printf("%d ", arr1[j]);
    > }
    > }
    >
    > main() {
    > subtract_arr(a_ptr,b);
    > for (int j=0; j<5; j++) {
    > printf("%d ", a_[j]); // -4 -2 0 2 4
    > }
    >
    > subtract_arr(&a_ptr,b);
    > }


    #include <stdio.h>

    int a_[]={1,2,3,4,5};
    int* a_ptr=a_;
    int b[]={5,4,3,2,1};

    // Passing ptr to array by reference
    void subtract_arr(int*& arr1, int* arr2) {
    for (int i=0; i<5; i++) {
    arr1-=arr2;
    }
    }

    // Passing by reference using pointers
    void psubtract_arr(int** arr1, int* arr2) {
    /* Have to rewrite this part
    Can't get this to work!!! */
    for (int i=0; i<5; i++) {
    *arr1-=*arr2;
    }
    }

    main() {
    subtract_arr(a_ptr,b);
    for (int j=0; j<5; j++) {
    printf("%d ", a_[j]); // -4 -2 0 2 4
    }

    // Modified
    int** pa_ptr=a_ptr;
    psubtract_arr(pa_ptr,b);
    for (int j=0; j<5; j++) {
    printf("%d ", a_[j]);
    }
    }
     
    LL, Feb 28, 2009
    #7
  8. LL

    LL Guest

    Passing by pointer

    On Sat, 28 Feb 2009 10:19:33 +0000, LL wrote:

    > On Sat, 28 Feb 2009 09:48:31 +0000, LL wrote:
    >
    >> On Sat, 28 Feb 2009 07:58:49 +0000, LL wrote:
    >>
    >>> Please comment on the following code for style and accuracy. More
    >>> specifically, could you clarify whether passing by reference using
    >>> pointer and the function's subsequent usage were done correctly?
    >>>
    >>> #include <stdio.h>
    >>>
    >>> int a_[]={1,2,3,4,5};
    >>> int* a_ptr=a_;
    >>> int b[]={5,4,3,2,1};
    >>>
    >>> // Passing ptr to array by reference
    >>> void subtract_arr(int*& arr1, int* arr2) {
    >>> for (int i=0; i<5; i++) {
    >>> arr1-=arr2;
    >>> }
    >>> }
    >>>
    >>> // Passing by reference using pointers void subtract_arr(int** arr1,
    >>> int* arr2) {
    >>> for (int i=0; i<5; i++) {
    >>> arr1-=arr2;
    >>> }
    >>> }
    >>>
    >>> main() {
    >>> subtract_arr(a_ptr,b);
    >>> for (int j=0; j<5; j++) {
    >>> printf("%d ", a_[j]); // -4 -2 0 2 4
    >>> }
    >>>
    >>> subtract_arr(&a_ptr,b);
    >>> for (int j=0; j<5; j++) {
    >>> printf("%d ", a_[j]); // same output
    >>> }
    >>> }

    >>
    >> How to rewrite passing by reference using pointers? Is there a such
    >> thing?
    >>
    >> #include <stdio.h>
    >>
    >> int a_[]={1,2,3,4,5};
    >> int* a_ptr=a_;
    >> int b[]={5,4,3,2,1};
    >>
    >> // Passing ptr to array by reference
    >> void subtract_arr(int*& arr1, int* arr2) {
    >> for (int i=0; i<5; i++) {
    >> arr1-=arr2;
    >> }
    >> }
    >>
    >> // Passing by reference using pointers void subtract_arr(int** arr1,
    >> int* arr2) {
    >> /* Have to rewrite this part */
    >> for (int i=0; i<5; i++) {
    >> arr1-=arr2;
    >> }
    >> for (int j=0; j<5; j++) {
    >> printf("%d ", arr1[j]);
    >> }
    >> }
    >>
    >> main() {
    >> subtract_arr(a_ptr,b);
    >> for (int j=0; j<5; j++) {
    >> printf("%d ", a_[j]); // -4 -2 0 2 4
    >> }
    >>
    >> subtract_arr(&a_ptr,b);
    >> }

    >
    > #include <stdio.h>
    >
    > int a_[]={1,2,3,4,5};
    > int* a_ptr=a_;
    > int b[]={5,4,3,2,1};
    >
    > // Passing ptr to array by reference
    > void subtract_arr(int*& arr1, int* arr2) {
    > for (int i=0; i<5; i++) {
    > arr1-=arr2;
    > }
    > }
    >
    > // Passing by reference using pointers void psubtract_arr(int** arr1,
    > int* arr2) {
    > /* Have to rewrite this part
    > Can't get this to work!!! */
    > for (int i=0; i<5; i++) {
    > *arr1-=*arr2;
    > }
    > }
    >
    > main() {
    > subtract_arr(a_ptr,b);
    > for (int j=0; j<5; j++) {
    > printf("%d ", a_[j]); // -4 -2 0 2 4
    > }
    >
    > // Modified
    > int** pa_ptr=a_ptr;
    > psubtract_arr(pa_ptr,b);
    > for (int j=0; j<5; j++) {
    > printf("%d ", a_[j]);
    > }
    >


    Refer to http://library.thinkquest.org/C0111571/manual.php?tid=53

    Shoudn't the output of the following program be 2? Why is it 1?

    #include <iostream>
    using namespace std;

    void pinc(int* x) {
    x++;
    }

    main() {
    int xx=1;
    cout << xx << endl;

    int* pxx=&xx;
    pinc(pxx);
    cout << xx << endl; // Shouldn't the output be 2?
    }
     
    LL, Feb 28, 2009
    #8
  9. LL

    LL Guest

    Re: Passing by pointer

    On Sat, 28 Feb 2009 10:36:02 +0000, LL wrote:

    > On Sat, 28 Feb 2009 10:19:33 +0000, LL wrote:
    >
    >> On Sat, 28 Feb 2009 09:48:31 +0000, LL wrote:
    >>
    >>> On Sat, 28 Feb 2009 07:58:49 +0000, LL wrote:
    >>>
    >>>> Please comment on the following code for style and accuracy. More
    >>>> specifically, could you clarify whether passing by reference using
    >>>> pointer and the function's subsequent usage were done correctly?
    >>>>
    >>>> #include <stdio.h>
    >>>>
    >>>> int a_[]={1,2,3,4,5};
    >>>> int* a_ptr=a_;
    >>>> int b[]={5,4,3,2,1};
    >>>>
    >>>> // Passing ptr to array by reference
    >>>> void subtract_arr(int*& arr1, int* arr2) {
    >>>> for (int i=0; i<5; i++) {
    >>>> arr1-=arr2;
    >>>> }
    >>>> }
    >>>>
    >>>> // Passing by reference using pointers void subtract_arr(int** arr1,
    >>>> int* arr2) {
    >>>> for (int i=0; i<5; i++) {
    >>>> arr1-=arr2;
    >>>> }
    >>>> }
    >>>>
    >>>> main() {
    >>>> subtract_arr(a_ptr,b);
    >>>> for (int j=0; j<5; j++) {
    >>>> printf("%d ", a_[j]); // -4 -2 0 2 4
    >>>> }
    >>>>
    >>>> subtract_arr(&a_ptr,b);
    >>>> for (int j=0; j<5; j++) {
    >>>> printf("%d ", a_[j]); // same output
    >>>> }
    >>>> }
    >>>
    >>> How to rewrite passing by reference using pointers? Is there a such
    >>> thing?
    >>>
    >>> #include <stdio.h>
    >>>
    >>> int a_[]={1,2,3,4,5};
    >>> int* a_ptr=a_;
    >>> int b[]={5,4,3,2,1};
    >>>
    >>> // Passing ptr to array by reference
    >>> void subtract_arr(int*& arr1, int* arr2) {
    >>> for (int i=0; i<5; i++) {
    >>> arr1-=arr2;
    >>> }
    >>> }
    >>>
    >>> // Passing by reference using pointers void subtract_arr(int** arr1,
    >>> int* arr2) {
    >>> /* Have to rewrite this part */
    >>> for (int i=0; i<5; i++) {
    >>> arr1-=arr2;
    >>> }
    >>> for (int j=0; j<5; j++) {
    >>> printf("%d ", arr1[j]);
    >>> }
    >>> }
    >>>
    >>> main() {
    >>> subtract_arr(a_ptr,b);
    >>> for (int j=0; j<5; j++) {
    >>> printf("%d ", a_[j]); // -4 -2 0 2 4
    >>> }
    >>>
    >>> subtract_arr(&a_ptr,b);
    >>> }

    >>
    >> #include <stdio.h>
    >>
    >> int a_[]={1,2,3,4,5};
    >> int* a_ptr=a_;
    >> int b[]={5,4,3,2,1};
    >>
    >> // Passing ptr to array by reference
    >> void subtract_arr(int*& arr1, int* arr2) {
    >> for (int i=0; i<5; i++) {
    >> arr1-=arr2;
    >> }
    >> }
    >>
    >> // Passing by reference using pointers void psubtract_arr(int** arr1,
    >> int* arr2) {
    >> /* Have to rewrite this part
    >> Can't get this to work!!! */
    >> for (int i=0; i<5; i++) {
    >> *arr1-=*arr2;
    >> }
    >> }
    >>
    >> main() {
    >> subtract_arr(a_ptr,b);
    >> for (int j=0; j<5; j++) {
    >> printf("%d ", a_[j]); // -4 -2 0 2 4
    >> }
    >>
    >> // Modified
    >> int** pa_ptr=a_ptr;
    >> psubtract_arr(pa_ptr,b);
    >> for (int j=0; j<5; j++) {
    >> printf("%d ", a_[j]);
    >> }
    >>
    >>

    > Refer to http://library.thinkquest.org/C0111571/manual.php?tid=53
    >
    > Shoudn't the output of the following program be 2? Why is it 1?
    >
    > #include <iostream>
    > using namespace std;
    >

    void pinc(int* x) {
    *x++;
    Modified here

    > }
    >
    > main() {
    > int xx=1;
    > cout << xx << endl;
    >
    > int* pxx=&xx;
    > pinc(pxx);
    > cout << xx << endl; // Shouldn't the output be 2?
    > }
     
    LL, Feb 28, 2009
    #9
  10. LL

    LL Guest

    Re: Passing by pointer

    On Sat, 28 Feb 2009 10:36:02 +0000, LL wrote:

    > On Sat, 28 Feb 2009 10:19:33 +0000, LL wrote:
    >
    >> On Sat, 28 Feb 2009 09:48:31 +0000, LL wrote:
    >>
    >>> On Sat, 28 Feb 2009 07:58:49 +0000, LL wrote:
    >>>
    >>>> Please comment on the following code for style and accuracy. More
    >>>> specifically, could you clarify whether passing by reference using
    >>>> pointer and the function's subsequent usage were done correctly?
    >>>>
    >>>> #include <stdio.h>
    >>>>
    >>>> int a_[]={1,2,3,4,5};
    >>>> int* a_ptr=a_;
    >>>> int b[]={5,4,3,2,1};
    >>>>
    >>>> // Passing ptr to array by reference
    >>>> void subtract_arr(int*& arr1, int* arr2) {
    >>>> for (int i=0; i<5; i++) {
    >>>> arr1-=arr2;
    >>>> }
    >>>> }
    >>>>
    >>>> // Passing by reference using pointers void subtract_arr(int** arr1,
    >>>> int* arr2) {
    >>>> for (int i=0; i<5; i++) {
    >>>> arr1-=arr2;
    >>>> }
    >>>> }
    >>>>
    >>>> main() {
    >>>> subtract_arr(a_ptr,b);
    >>>> for (int j=0; j<5; j++) {
    >>>> printf("%d ", a_[j]); // -4 -2 0 2 4
    >>>> }
    >>>>
    >>>> subtract_arr(&a_ptr,b);
    >>>> for (int j=0; j<5; j++) {
    >>>> printf("%d ", a_[j]); // same output
    >>>> }
    >>>> }
    >>>
    >>> How to rewrite passing by reference using pointers? Is there a such
    >>> thing?
    >>>
    >>> #include <stdio.h>
    >>>
    >>> int a_[]={1,2,3,4,5};
    >>> int* a_ptr=a_;
    >>> int b[]={5,4,3,2,1};
    >>>
    >>> // Passing ptr to array by reference
    >>> void subtract_arr(int*& arr1, int* arr2) {
    >>> for (int i=0; i<5; i++) {
    >>> arr1-=arr2;
    >>> }
    >>> }
    >>>
    >>> // Passing by reference using pointers void subtract_arr(int** arr1,
    >>> int* arr2) {
    >>> /* Have to rewrite this part */
    >>> for (int i=0; i<5; i++) {
    >>> arr1-=arr2;
    >>> }
    >>> for (int j=0; j<5; j++) {
    >>> printf("%d ", arr1[j]);
    >>> }
    >>> }
    >>>
    >>> main() {
    >>> subtract_arr(a_ptr,b);
    >>> for (int j=0; j<5; j++) {
    >>> printf("%d ", a_[j]); // -4 -2 0 2 4
    >>> }
    >>>
    >>> subtract_arr(&a_ptr,b);
    >>> }

    >>
    >> #include <stdio.h>
    >>
    >> int a_[]={1,2,3,4,5};
    >> int* a_ptr=a_;
    >> int b[]={5,4,3,2,1};
    >>
    >> // Passing ptr to array by reference
    >> void subtract_arr(int*& arr1, int* arr2) {
    >> for (int i=0; i<5; i++) {
    >> arr1-=arr2;
    >> }
    >> }
    >>
    >> // Passing by reference using pointers void psubtract_arr(int** arr1,
    >> int* arr2) {
    >> /* Have to rewrite this part
    >> Can't get this to work!!! */
    >> for (int i=0; i<5; i++) {
    >> *arr1-=*arr2;
    >> }
    >> }
    >>
    >> main() {
    >> subtract_arr(a_ptr,b);
    >> for (int j=0; j<5; j++) {
    >> printf("%d ", a_[j]); // -4 -2 0 2 4
    >> }
    >>
    >> // Modified
    >> int** pa_ptr=a_ptr;
    >> psubtract_arr(pa_ptr,b);
    >> for (int j=0; j<5; j++) {
    >> printf("%d ", a_[j]);
    >> }
    >>
    >>

    > Refer to http://library.thinkquest.org/C0111571/manual.php?tid=53
    >
    > Shoudn't the output of the following program be 2? Why is it 1?
    >
    > #include <iostream>
    > using namespace std;
    >
    > void pinc(int* x) {
    > x++;
    > }
    >
    > main() {
    > int xx=1;
    > cout << xx << endl;
    >
    > int* pxx=&xx;
    > pinc(pxx);
    > cout << xx << endl; // Shouldn't the output be 2?
    > }


    Also refer to http://www.daniweb.com/forums/thread33443.html
     
    LL, Feb 28, 2009
    #10
  11. LL

    LL Guest

    Re: Passing by pointer

    On Sat, 28 Feb 2009 10:38:19 +0000, LL wrote:

    > On Sat, 28 Feb 2009 10:36:02 +0000, LL wrote:
    >
    >> On Sat, 28 Feb 2009 10:19:33 +0000, LL wrote:
    >>
    >>> On Sat, 28 Feb 2009 09:48:31 +0000, LL wrote:
    >>>
    >>>> On Sat, 28 Feb 2009 07:58:49 +0000, LL wrote:
    >>>>
    >>>>> Please comment on the following code for style and accuracy. More
    >>>>> specifically, could you clarify whether passing by reference using
    >>>>> pointer and the function's subsequent usage were done correctly?
    >>>>>
    >>>>> #include <stdio.h>
    >>>>>
    >>>>> int a_[]={1,2,3,4,5};
    >>>>> int* a_ptr=a_;
    >>>>> int b[]={5,4,3,2,1};
    >>>>>
    >>>>> // Passing ptr to array by reference
    >>>>> void subtract_arr(int*& arr1, int* arr2) {
    >>>>> for (int i=0; i<5; i++) {
    >>>>> arr1-=arr2;
    >>>>> }
    >>>>> }
    >>>>>
    >>>>> // Passing by reference using pointers void subtract_arr(int** arr1,
    >>>>> int* arr2) {
    >>>>> for (int i=0; i<5; i++) {
    >>>>> arr1-=arr2;
    >>>>> }
    >>>>> }
    >>>>>
    >>>>> main() {
    >>>>> subtract_arr(a_ptr,b);
    >>>>> for (int j=0; j<5; j++) {
    >>>>> printf("%d ", a_[j]); // -4 -2 0 2 4
    >>>>> }
    >>>>>
    >>>>> subtract_arr(&a_ptr,b);
    >>>>> for (int j=0; j<5; j++) {
    >>>>> printf("%d ", a_[j]); // same output
    >>>>> }
    >>>>> }
    >>>>
    >>>> How to rewrite passing by reference using pointers? Is there a such
    >>>> thing?
    >>>>
    >>>> #include <stdio.h>
    >>>>
    >>>> int a_[]={1,2,3,4,5};
    >>>> int* a_ptr=a_;
    >>>> int b[]={5,4,3,2,1};
    >>>>
    >>>> // Passing ptr to array by reference
    >>>> void subtract_arr(int*& arr1, int* arr2) {
    >>>> for (int i=0; i<5; i++) {
    >>>> arr1-=arr2;
    >>>> }
    >>>> }
    >>>>
    >>>> // Passing by reference using pointers void subtract_arr(int** arr1,
    >>>> int* arr2) {
    >>>> /* Have to rewrite this part */
    >>>> for (int i=0; i<5; i++) {
    >>>> arr1-=arr2;
    >>>> }
    >>>> for (int j=0; j<5; j++) {
    >>>> printf("%d ", arr1[j]);
    >>>> }
    >>>> }
    >>>>
    >>>> main() {
    >>>> subtract_arr(a_ptr,b);
    >>>> for (int j=0; j<5; j++) {
    >>>> printf("%d ", a_[j]); // -4 -2 0 2 4
    >>>> }
    >>>>
    >>>> subtract_arr(&a_ptr,b);
    >>>> }
    >>>
    >>> #include <stdio.h>
    >>>
    >>> int a_[]={1,2,3,4,5};
    >>> int* a_ptr=a_;
    >>> int b[]={5,4,3,2,1};
    >>>
    >>> // Passing ptr to array by reference
    >>> void subtract_arr(int*& arr1, int* arr2) {
    >>> for (int i=0; i<5; i++) {
    >>> arr1-=arr2;
    >>> }
    >>> }
    >>>
    >>> // Passing by reference using pointers void psubtract_arr(int** arr1,
    >>> int* arr2) {
    >>> /* Have to rewrite this part
    >>> Can't get this to work!!! */
    >>> for (int i=0; i<5; i++) {
    >>> *arr1-=*arr2;
    >>> }
    >>> }
    >>>
    >>> main() {
    >>> subtract_arr(a_ptr,b);
    >>> for (int j=0; j<5; j++) {
    >>> printf("%d ", a_[j]); // -4 -2 0 2 4
    >>> }
    >>>
    >>> // Modified
    >>> int** pa_ptr=a_ptr;
    >>> psubtract_arr(pa_ptr,b);
    >>> for (int j=0; j<5; j++) {
    >>> printf("%d ", a_[j]);
    >>> }
    >>>
    >>>

    >> Refer to http://library.thinkquest.org/C0111571/manual.php?tid=53
    >>
    >> Shoudn't the output of the following program be 2? Why is it 1?
    >>
    >> #include <iostream>
    >> using namespace std;
    >>

    > void pinc(int* x) {
    > *x++;
    > Modified here
    >
    >> }
    >>
    >> main() {
    >> int xx=1;
    >> cout << xx << endl;
    >>
    >> int* pxx=&xx;
    >> pinc(pxx);
    >> cout << xx << endl; // Shouldn't the output be 2?
    >> }


    #include <iostream>
    using namespace std;

    void pinc(int* x) {
    *x++;
    }

    main() {
    int xx=1;
    cout << xx << endl;

    pinc(&xx);
    cout << xx << endl; // Still gives 1, not 2!
    }
     
    LL, Feb 28, 2009
    #11
  12. LL

    LL Guest

    Re: Passing by pointer

    On Sat, 28 Feb 2009 10:49:32 +0000, LL wrote:

    > On Sat, 28 Feb 2009 10:38:19 +0000, LL wrote:
    >
    >> On Sat, 28 Feb 2009 10:36:02 +0000, LL wrote:
    >>
    >>> On Sat, 28 Feb 2009 10:19:33 +0000, LL wrote:
    >>>
    >>>> On Sat, 28 Feb 2009 09:48:31 +0000, LL wrote:
    >>>>
    >>>>> On Sat, 28 Feb 2009 07:58:49 +0000, LL wrote:
    >>>>>
    >>>>>> Please comment on the following code for style and accuracy. More
    >>>>>> specifically, could you clarify whether passing by reference using
    >>>>>> pointer and the function's subsequent usage were done correctly?
    >>>>>>
    >>>>>> #include <stdio.h>
    >>>>>>
    >>>>>> int a_[]={1,2,3,4,5};
    >>>>>> int* a_ptr=a_;
    >>>>>> int b[]={5,4,3,2,1};
    >>>>>>
    >>>>>> // Passing ptr to array by reference
    >>>>>> void subtract_arr(int*& arr1, int* arr2) {
    >>>>>> for (int i=0; i<5; i++) {
    >>>>>> arr1-=arr2;
    >>>>>> }
    >>>>>> }
    >>>>>>
    >>>>>> // Passing by reference using pointers void subtract_arr(int**
    >>>>>> arr1, int* arr2) {
    >>>>>> for (int i=0; i<5; i++) {
    >>>>>> arr1-=arr2;
    >>>>>> }
    >>>>>> }
    >>>>>>
    >>>>>> main() {
    >>>>>> subtract_arr(a_ptr,b);
    >>>>>> for (int j=0; j<5; j++) {
    >>>>>> printf("%d ", a_[j]); // -4 -2 0 2 4
    >>>>>> }
    >>>>>>
    >>>>>> subtract_arr(&a_ptr,b);
    >>>>>> for (int j=0; j<5; j++) {
    >>>>>> printf("%d ", a_[j]); // same output
    >>>>>> }
    >>>>>> }
    >>>>>
    >>>>> How to rewrite passing by reference using pointers? Is there a such
    >>>>> thing?
    >>>>>
    >>>>> #include <stdio.h>
    >>>>>
    >>>>> int a_[]={1,2,3,4,5};
    >>>>> int* a_ptr=a_;
    >>>>> int b[]={5,4,3,2,1};
    >>>>>
    >>>>> // Passing ptr to array by reference
    >>>>> void subtract_arr(int*& arr1, int* arr2) {
    >>>>> for (int i=0; i<5; i++) {
    >>>>> arr1-=arr2;
    >>>>> }
    >>>>> }
    >>>>>
    >>>>> // Passing by reference using pointers void subtract_arr(int** arr1,
    >>>>> int* arr2) {
    >>>>> /* Have to rewrite this part */
    >>>>> for (int i=0; i<5; i++) {
    >>>>> arr1-=arr2;
    >>>>> }
    >>>>> for (int j=0; j<5; j++) {
    >>>>> printf("%d ", arr1[j]);
    >>>>> }
    >>>>> }
    >>>>>
    >>>>> main() {
    >>>>> subtract_arr(a_ptr,b);
    >>>>> for (int j=0; j<5; j++) {
    >>>>> printf("%d ", a_[j]); // -4 -2 0 2 4
    >>>>> }
    >>>>>
    >>>>> subtract_arr(&a_ptr,b);
    >>>>> }
    >>>>
    >>>> #include <stdio.h>
    >>>>
    >>>> int a_[]={1,2,3,4,5};
    >>>> int* a_ptr=a_;
    >>>> int b[]={5,4,3,2,1};
    >>>>
    >>>> // Passing ptr to array by reference
    >>>> void subtract_arr(int*& arr1, int* arr2) {
    >>>> for (int i=0; i<5; i++) {
    >>>> arr1-=arr2;
    >>>> }
    >>>> }
    >>>>
    >>>> // Passing by reference using pointers void psubtract_arr(int** arr1,
    >>>> int* arr2) {
    >>>> /* Have to rewrite this part
    >>>> Can't get this to work!!! */
    >>>> for (int i=0; i<5; i++) {
    >>>> *arr1-=*arr2;
    >>>> }
    >>>> }
    >>>>
    >>>> main() {
    >>>> subtract_arr(a_ptr,b);
    >>>> for (int j=0; j<5; j++) {
    >>>> printf("%d ", a_[j]); // -4 -2 0 2 4
    >>>> }
    >>>>
    >>>> // Modified
    >>>> int** pa_ptr=a_ptr;
    >>>> psubtract_arr(pa_ptr,b);
    >>>> for (int j=0; j<5; j++) {
    >>>> printf("%d ", a_[j]);
    >>>> }
    >>>>
    >>>>
    >>> Refer to http://library.thinkquest.org/C0111571/manual.php?tid=53
    >>>
    >>> Shoudn't the output of the following program be 2? Why is it 1?
    >>>
    >>> #include <iostream>
    >>> using namespace std;
    >>>

    >> void pinc(int* x) {
    >> *x++;
    >> Modified here
    >>
    >>> }
    >>>
    >>> main() {
    >>> int xx=1;
    >>> cout << xx << endl;
    >>>
    >>> int* pxx=&xx;
    >>> pinc(pxx);
    >>> cout << xx << endl; // Shouldn't the output be 2?
    >>> }

    >
    > #include <iostream>
    > using namespace std;
    >
    > void pinc(int* x) {
    > *x++;
    > }
    >
    > main() {
    > int xx=1;
    > cout << xx << endl;
    >
    > pinc(&xx);
    > cout << xx << endl; // Still gives 1, not 2!
    > }


    I'll just accept this is the case.

    #include <iostream>
    using namespace std;

    void pinc(int* x) {
    cout << ++(*x) << endl;
    }

    main() {
    int xx=1;
    cout << xx << endl;

    pinc(&xx);
    // cout << xx << endl; // Still gives 1, not 2!
    }
     
    LL, Feb 28, 2009
    #12
  13. LL

    Ike Naar Guest

    Re: Passing by pointer

    In article <49a917ba$0$32001$>,
    LL <> wrote:
    > void pinc(int* x) {
    > *x++;
    > }


    The expression *x++ is evaluated as *(x++)
    but it looks like you want (*x)++
     
    Ike Naar, Feb 28, 2009
    #13
  14. LL

    LL Guest

    Re: Passing by pointer

    On Sat, 28 Feb 2009 11:00:27 +0000, Ike Naar wrote:

    > In article <49a917ba$0$32001$>, LL
    > <> wrote:
    >> void pinc(int* x) {
    >> *x++;
    >> }

    >
    > The expression *x++ is evaluated as *(x++) but it looks like you want
    > (*x)++


    #include <iostream>
    using namespace std;

    void pinc(int* x) {
    ++(*x);
    }

    main() {
    int xx=1;
    cout << xx << endl;

    pinc(&xx);
    cout << xx << endl; // Gives 2!
    }
     
    LL, Feb 28, 2009
    #14
  15. LL

    LL Guest

    Re: Passing by pointer

    On Sat, 28 Feb 2009 11:00:27 +0000, Ike Naar wrote:

    > In article <49a917ba$0$32001$>, LL
    > <> wrote:
    >> void pinc(int* x) {
    >> *x++;
    >> }

    >
    > The expression *x++ is evaluated as *(x++) but it looks like you want
    > (*x)++


    Just for the record this works too.

    #include <iostream>
    using namespace std;

    void parrinc(int** x) {
    ++(*x[0]);
    }

    main() {
    int a[]={1,2};
    int* pa=a;
    int** ppa=&pa;
    parrinc(ppa);
    cout << *ppa[0] << endl; // Gives 2
    }
     
    LL, Feb 28, 2009
    #15
  16. LL

    LL Guest

    On Sat, 28 Feb 2009 07:58:49 +0000, LL wrote:

    > Please comment on the following code for style and accuracy. More
    > specifically, could you clarify whether passing by reference using
    > pointer and the function's subsequent usage were done correctly?
    >
    > #include <stdio.h>
    >
    > int a_[]={1,2,3,4,5};
    > int* a_ptr=a_;
    > int b[]={5,4,3,2,1};
    >
    > // Passing ptr to array by reference
    > void subtract_arr(int*& arr1, int* arr2) {
    > for (int i=0; i<5; i++) {
    > arr1-=arr2;
    > }
    > }
    >
    > // Passing by reference using pointers void subtract_arr(int** arr1,
    > int* arr2) {
    > for (int i=0; i<5; i++) {
    > arr1-=arr2;
    > }
    > }
    >
    > main() {
    > subtract_arr(a_ptr,b);
    > for (int j=0; j<5; j++) {
    > printf("%d ", a_[j]); // -4 -2 0 2 4
    > }
    >
    > subtract_arr(&a_ptr,b);
    > for (int j=0; j<5; j++) {
    > printf("%d ", a_[j]); // same output
    > }
    > }

    This gives segmentation fault error when run.

    #include <stdio.h>

    int a_[]={1,2,3,4,5};
    int* a_ptr=a_;
    int b[]={5,4,3,2,1};

    // Passing ptr to array by reference
    void subtract_arr(int*& arr1, int* arr2) {
    for (int i=0; i<5; i++) {
    arr1-=arr2;
    }
    }

    // Passing by reference using pointers
    void psubtract_arr(int** arr1, int* arr2) {
    for (int i=0; i<5; i++) {
    *arr1-=arr2;
    }
    }

    main() {
    subtract_arr(a_ptr,b);
    for (int j=0; j<5; j++) {
    printf("%d ", a_[j]); // Segmentation fault
    }

    psubtract_arr(&a_ptr,b);
    for (int j=0; j<5; j++) {
    printf("%d ", a_[j]);
    }
    }
     
    LL, Feb 28, 2009
    #16
  17. LL

    Flash Gordon Guest

    Re: Passing by pointer

    LL wrote:

    <snip>

    > main() {
    > int xx=1;
    > cout << xx << endl;
    >
    > pinc(&xx);
    > // cout << xx << endl; // Still gives 1, not 2!
    > }


    It won't do you much good to continue to post C++ to a C group. You have
    already been told about comp.lang.c++ where you will find there are
    actual C++ exports. I know you saw that post because you replied to it!
    If, on the other hand, you want to avoid C++ exports looking at your
    code then comp.lang.lisp would do you just as well.
    --
    Flash Gordon
     
    Flash Gordon, Feb 28, 2009
    #17
  18. LL

    LL Guest

    On Sat, 28 Feb 2009 12:06:12 +0000, Richard Heathfield wrote:

    > LL said:
    >
    > <snip>
    >
    >> This gives segmentation fault error when run.
    >>
    >> #include <stdio.h>
    >>
    >> int a_[]={1,2,3,4,5};
    >> int* a_ptr=a_;
    >> int b[]={5,4,3,2,1};
    >>
    >> // Passing ptr to array by reference

    >
    > C doesn't have pass-by-reference. This has already been explained to
    > you.
    >
    >> void subtract_arr(int*& arr1, int* arr2) {

    >
    > That isn't legal C. It may possibly be legal C++, and you may have
    > intended this, in which case I suggest you take it up with
    > comp.lang.c++. If you didn't realise it wasn't C, now you do.
    >
    > <snip>


    Sure thing. I got this wrapped up now.

    Fixed:

    #include <stdio.h>

    int a_[]={1,2,3,4,5};
    int* a_ptr=a_;
    int b[]={5,4,3,2,1};

    // Passing ptr to array by reference
    void subtract_arr(int*& arr1, int* arr2) {
    for (int i=0; i<5; i++) {
    arr1-=arr2;
    }
    }

    // Passing by reference using pointers
    void psubtract_arr(int** arr1, int* arr2) {
    for (int i=0; i<5; i++) {
    (*arr1)-=arr2; // This fixed it
    }
    }

    main() {
    subtract_arr(a_ptr,b);
    for (int j=0; j<5; j++) {
    printf("%d ", a_[j]);
    }

    int** pa_ptr=&a_ptr;
    psubtract_arr(pa_ptr,b);
    for (int j=0; j<5; j++) {
    printf("%d ", a_[j]);
    }
    }
     
    LL, Feb 28, 2009
    #18
  19. LL <> wrote:
    > On Sat, 28 Feb 2009 07:58:49 +0000, LL wrote:


    > > Please comment on the following code for style and accuracy. More
    > > specifically, could you clarify whether passing by reference using
    > > pointer and the function's subsequent usage were done correctly?
    > >
    > > #include <stdio.h>
    > >
    > > int a_[]={1,2,3,4,5};
    > > int* a_ptr=a_;
    > > int b[]={5,4,3,2,1};
    > >
    > > // Passing ptr to array by reference
    > > void subtract_arr(int*& arr1, int* arr2) {
    > > for (int i=0; i<5; i++) {
    > > arr1-=arr2;
    > > }
    > > }
    > >
    > > // Passing by reference using pointers void subtract_arr(int** arr1,
    > > int* arr2) {
    > > for (int i=0; i<5; i++) {
    > > arr1-=arr2;
    > > }
    > > }
    > >
    > > main() {
    > > subtract_arr(a_ptr,b);
    > > for (int j=0; j<5; j++) {
    > > printf("%d ", a_[j]); // -4 -2 0 2 4
    > > }
    > >
    > > subtract_arr(&a_ptr,b);
    > > for (int j=0; j<5; j++) {
    > > printf("%d ", a_[j]); // same output
    > > }
    > > }

    > This gives segmentation fault error when run.


    > #include <stdio.h>


    > int a_[]={1,2,3,4,5};
    > int* a_ptr=a_;


    Ok, now 'a_ptr' is a pointer to the first element of 'a_'.

    > int b[]={5,4,3,2,1};


    > // Passing ptr to array by reference
    > void subtract_arr(int*& arr1, int* arr2) {
    > for (int i=0; i<5; i++) {
    > arr1-=arr2;
    > }
    > }


    This is obviously C++, the 'int*&' stuff makes no sense in C.
    If this is correct C++ you have to ask in comp.lang.c+.

    > // Passing by reference using pointers


    There is no passing by reference in C.

    > void psubtract_arr(int** arr1, int* arr2) {


    Here, the way you call the function, 'arr1' is a pointer to
    the pointer that points to the first element of 'a_'.

    > for (int i=0; i<5; i++) {
    > *arr1-=arr2;


    The [] operator binds stronger than the unary '*' operator.
    And 'x' can always replaced by '*(x+i)', so what you try
    to do here is

    **( arr1 + i ) += arr2[ i ];

    Since 'arr1' isn't an array, but just a pointer to a pointer
    this fails the moment you try to do it with 'i' set to 1
    since then you try to dereference an invalid pointer.
    Getting a segmentation fault here or some time later is one
    of the possible outcomes.

    What you could do is

    ( * arr1 )[ i ] -= arr2[ i ];

    But that's rather useless since when you call a function
    and have the name of an array as one of its arguments then
    the value the function receives is a pointer to the first
    element of that array. So your function could be simply

    void psubtract_arr( int * arr1, int * arr2 ) {
    int i;
    for ( i = 0; i < 5; i++ )
    arr1[ i ] -= arr2[ i ];
    }

    and when you call it either like this

    psubtract( a_, b );

    or maybe also like this

    psubtract( a_ptr, b );

    then it already operates on the elements of 'a_'.

    If you use the fix proposed above you just have a function
    that does exactly the same, it's just a bit more complicated
    and possibly slower since additional dereferencing has to be
    done. So there are only disadvantages and no gains.

    > }
    > }


    > main() {
    > subtract_arr(a_ptr,b);
    > for (int j=0; j<5; j++) {
    > printf("%d ", a_[j]); // Segmentation fault
    > }


    > psubtract_arr(&a_ptr,b);
    > for (int j=0; j<5; j++) {
    > printf("%d ", a_[j]);
    > }
    > }


    Would you please decide what language you want to use and
    go to comp.lang.c++ if you decide on C++? And be so kind
    to stop posting a new long message every few minutes with-
    out relevant new information, this is not an IRC.

    Regards, Jens
    --
    \ Jens Thoms Toerring ___
    \__________________________ http://toerring.de
     
    Jens Thoms Toerring, Feb 28, 2009
    #19
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. sam pal
    Replies:
    3
    Views:
    550
    E. Robert Tisdale
    Jul 16, 2003
  2. Victor Bazarov
    Replies:
    25
    Views:
    894
    E. Robert Tisdale
    Mar 23, 2005
  3. pereges
    Replies:
    7
    Views:
    440
    pereges
    Jun 1, 2008
  4. Ramon F Herrera
    Replies:
    8
    Views:
    483
    Pascal J. Bourguignon
    Sep 13, 2009
  5. Replies:
    2
    Views:
    150
Loading...

Share This Page