List of all available Websheets
Viewing cpp/pointers/OneOrMany by redekopp@usc.edu. You have unsaved changes. Log in above to create, edit, preview and publish Websheets.
Property | Value |
---|---|
Description html markup shown to student | Example showing that a pointer can point to a single (scalar) value or an array of values. The person writing the function makes an assumption and the perons calling it should follow it. |
Public permissions | |
Remarks Comments, history, license, etc. | Originally by Mark Redekopp (redekopp@usc.edu) |
Engine | |
Template / Reference solution |
using namespace std; // ptr can point to one int or an array of ints // It is up to the programmer to ensure that the // assumptions of the function writer match // what the user passes void set_int(int* ptr, int size) { \[ \show: *ptr = 5; // same as ptr[0] = 5; if(size > 1){ ptr[1] = 7; // only safe if // an array of size > 1 } ]\ } int main() { int val1=0, vals_array[2] = {0,0}; // Pass a pointer to a single int set_int(&val1, 1); // Pass a pointer to an array set_int(vals_array, 5); // Print the results cout << "val1 = " << val1 << endl; cout << "vals_array: " << vals_array[0] << " " << vals_array[1] << endl; return 0; } |
C++ test suite json list of stdin/args tests e.g. [{"stdin":"hi", "args":["4", "5"]}, {"stdin":"noargs"}] to just run once with no input use [{}] | [ {} ] |
Solution visibility | |
Is example? i.e., just a demo |
Note: problems are open-source by default (see 'Public permissions'). Assumed license for open problems is Creative Commons 4.0 Attribution-ShareAlike unless specified in 'Remarks'.