Passing by reference

L

LL

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

user923005

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

LL

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

LL

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

LL

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);
}
 
G

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() {


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;
}
 
L

LL

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]);
}
}
 
L

LL

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?
}
 
L

LL

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
 
L

LL

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
 
L

LL

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!
}
 
L

LL

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!
}
 
L

LL

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!
}
 
L

LL

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

LL

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]);
}
}
 
F

Flash Gordon

LL wrote:

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

LL

LL said:

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]);
}
}
 
J

Jens Thoms Toerring

LL said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top