Term
int main() { int x = 0; int* ptr = &x; *ptr = 5; cout << x << endl; }
|
|
Definition
|
|
Term
int main() { int x = 0; int* ptr = &1; *ptr = 1; cout << x << endl; }
|
|
Definition
ERROR, & only takes locator values (variables) |
|
|
Term
int main() { int x = 0; int* ptr = &(x++) ; *ptr = 1; cout << x << endl; }
|
|
Definition
ERROR, x++ returns a temporary value and not the actual x variable (check operators) |
|
|
Term
int main() { int x = 1; int* ptr = &(++x) ; cout << *ptr << endl; }
|
|
Definition
returns 2, ++x returns the actual variable x (check operators) |
|
|
Term
void get(int& x){ ++x; }
int main(){ int y = 1; get(y); cout << y << endl; }
|
|
Definition
returns 2, works because y is a variable with a locator address |
|
|
Term
int main(){ int x = 12; int& y = x;
x++; cout << y << endl; }
|
|
Definition
13, because the address of y is the same as x so has the same value |
|
|
Term
int main(){ int x = 12; int& y = &x;
x++; cout << y << endl; }
|
|
Definition
ERROR, &x is an address, not a variable |
|
|
Term
void f(int& x) { cout << "x = " << x << endl; }
int main(){ int y = 0; f(y); }
|
|
Definition
|
|
Term
void f(int& x) { cout << "x = " << x << endl; }
int main(){ f(1); }
|
|
Definition
Error, 1 is not a variable |
|
|
Term
void f(const int& x) { cout << "x = " << x << endl; }
int main(){ f(1); }
|
|
Definition
1... const gives 1 a temporary address |
|
|
Term
void f(const int& x) { x++; }
int main(){ f(1); }
|
|
Definition
Error, x is a const variable, cant be edited. Nothing else wrong here |
|
|
Term
int& f(const int& x) { return x; }
int main() { int x = f(1); x++;
cout << x; }
|
|
Definition
Error, but only because you are returning a const int&, expected int& |
|
|
Term
const int& f(const int& x) { return x; }
int main() { int x = f(1); x++;
cout << x; }
|
|
Definition
|
|
Term
const int& f(int& x) { return x; }
int main() { int y = 0; int x = f(y); x++;
cout << x; }
|
|
Definition
1... this somehow works... |
|
|
Term
int& f(int& x) { return x; }
int main() { int y; f(y) = 12;
cout << y; }
|
|
Definition
12, f(x) returned the actual variable y. |
|
|
Term
int& f() { static int x = 1; cout << x << endl; return x; }
int main() { f() = 12; f(); }
|
|
Definition
1
12
static variables in functions initialized once and stay for the rest of the program existence.
this was on the midterm |
|
|
Term
Im sorry
void f(int* ptr) { cout << *ptr << endl; delete ptr; }
int main() { int* ptr = new int(1); f(ptr); cout << *ptr << endl; }
|
|
Definition
1
junk (because ptr deleted)
|
|
|
Term
Im sorry
void f(const int* ptr) { cout << *ptr << endl; delete ptr; }
int main() { int* ptr = new int(1); f(ptr); cout << *ptr << endl; }
|
|
Definition
1
Junk (ptr deleted)
Works... const int* ptr = pointer that points to a const int.
|
|
|
Term
Im sorry
void f(const int* const ptr) { cout << *ptr << endl; delete ptr; }
int main() { int* ptr = new int(1); f(ptr); cout << *ptr << endl; }
|
|
Definition
1
Junk (deleted pointer)
idk why this works, I hate this |
|
|