#include<iostream>
#include <cmath>
using std::cin;
using std::cout;
using std::endl;
void doSomethingElsePointer(int* i);
void doSomethingElse(int i);
void doRef(int &i);
void array_pointer();
int main() {
int i = 100;
int *p = &i;
doSomethingElse(i); // passes i by references since doSomethingElse() receives it
// by reference, but the syntax makes it appear as if i is passed
// by value
cout << "doSomethingElse=" << i << endl;
doSomethingElsePointer(&i);
doSomethingElsePointer(p);
cout << "-----doSomethingElsePointer=" << i << endl;
doRef(i);
cout << "-----doRef after =" << i << endl;
array_pointer();
return 0;
}
void array_pointer() // receives i as a reference
{
/////////////////////////////
int numbers[5];
int * p;
p = numbers; *p = 10;
p++; *p = 20;
p = &numbers[2]; *p = 30;
p = numbers + 3; *p = 40;
p = numbers; *(p+4) = 50;
for (int n=0; n<5; n++)
cout << numbers[n] << ", ";
}
void doSomethingElse(int i) // receives i as a reference
{
i = 9;
cout << i << endl;
}
void doSomethingElsePointer(int* i)
{
*i = 33;
cout << "doSomethingElsePointer = "<< *i << endl;
}
void doRef(int &i)
{
i = 99;
cout << "doRef="<<i << endl;
}
#include <cmath>
using std::cin;
using std::cout;
using std::endl;
void doSomethingElsePointer(int* i);
void doSomethingElse(int i);
void doRef(int &i);
void array_pointer();
int main() {
int i = 100;
int *p = &i;
doSomethingElse(i); // passes i by references since doSomethingElse() receives it
// by reference, but the syntax makes it appear as if i is passed
// by value
cout << "doSomethingElse=" << i << endl;
doSomethingElsePointer(&i);
doSomethingElsePointer(p);
cout << "-----doSomethingElsePointer=" << i << endl;
doRef(i);
cout << "-----doRef after =" << i << endl;
array_pointer();
return 0;
}
void array_pointer() // receives i as a reference
{
/////////////////////////////
int numbers[5];
int * p;
p = numbers; *p = 10;
p++; *p = 20;
p = &numbers[2]; *p = 30;
p = numbers + 3; *p = 40;
p = numbers; *(p+4) = 50;
for (int n=0; n<5; n++)
cout << numbers[n] << ", ";
}
void doSomethingElse(int i) // receives i as a reference
{
i = 9;
cout << i << endl;
}
void doSomethingElsePointer(int* i)
{
*i = 33;
cout << "doSomethingElsePointer = "<< *i << endl;
}
void doRef(int &i)
{
i = 99;
cout << "doRef="<<i << endl;
}
No comments:
Post a Comment