Note that there are some explanatory texts on larger screens.

plurals
  1. POfunction parameter used to store value
    text
    copied!<p>I have to define an interface. The API in my homework is stated below:</p> <pre><code>int generate_codes(char * ssn, char * student_id); </code></pre> <p>int denotes 0 or 1 for pass or fail. studentid is an output param should return a 6 digit id. ssn is a 9 digit input param</p> <p>they school program will take ssn's and use my code to generate the student id. </p> <ol> <li>now from an API perspective should I not be using const char * for both parameters.</li> <li>should the studentid not be passed in by reference? rather than by pointer?</li> <li>can someone tell me how i can easily use the pointer in my test app which uses my api to get the pointer such that it prints a std::string from a char *? </li> </ol> <p>my app code looks something like</p> <pre><code>const char * ssn = "987098765" const char * studnt_id = new char [7]; int value = -1; value = generate_codes(ssn,studnt_id); std::string test(studnt_id); std::cout&lt;&lt;"student id= "&lt;&lt;test&lt;&lt;" Pass/fail= "&lt;&lt;value&lt;&lt;std::endl; delete [] studnt_id; return 0; </code></pre> <p>I basically got an error about &lt;&lt; not being compatible with the right hand side of the operand. When i changed the code to </p> <pre><code>std::cout&lt;&lt;"student id= "&lt;&lt;test.c_str()&lt;&lt;" Pass/fail= "&lt;&lt;value&lt;&lt;std::endl; </code></pre> <p>then it worked but i get garbage for the value. not sure how to do get the value form the pointer. THe value inside the function prints just fine. but when i try to print it outside of the function it prints garbage. Inside the above function I do set the studndt_id like so</p> <pre><code>std::string str_studnt_id = studnt_id; </code></pre> <p>this is to take a string param to my real funciton that does the code generation. when I am ready with the code and want to return it i do the following:</p> <pre><code>studnt_id = str_studnt_id.c_str(); </code></pre> <p>should that make the address of the str_studnt point to the address of studnt_id and thus any changes I make to the value that its pointing to it should reflect outside the function? This API is used using char * but my functions use std::string.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload