Is there any Stray or Dangling Pointers in this program?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
|
#include <cstdlib>
#include <iostream>
using namespace std;
int array(int *x);
int main(int argc, char** argv)
{
int x[10];
int y[10];
array(x);
array(y);
for(int i=0;i<10;i++)
cout << " x: " << *(x+i);
cout << endl;
for(int i=0;i<10;i++)
cout << " x: " << *(y+i);
cout << endl;
return 0;
}
int array(int *x)
{
for(int i=0;i<10;i++)
*(x+i)=i;
}
|
No, you are fine.
Just fyi:
1 2 3 4
|
*(x+i) // <- this
x[i] // <- is the same as this
// most people find the latter to be more readable.
|
Is it possible to pass an array by reference?
Last edited on
Yeah... that's basically what you're doing.
You can't pass them by value. At least not without putting them in some kind of container (like a struct or something).
THX
Topic archived. No new replies allowed.