tags:

views:

82

answers:

4

Hi, I have to define an interface. The API in my homework is stated below:

int generate_codes(char * ssn, char * student_id);

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

they school program will take ssn's and use my code to generate the student id.

  1. now from an API perspective should I not be using const char * for both parameters.
  2. should the studentid not be passed in by reference? rather than by pointer?
  3. 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 *?

my app code looks something like

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<<"student id= "<<test<<" Pass/fail= "<<value<<std::endl;

delete [] studnt_id;
return 0;

I basically got an error about << not being compatible with the right hand side of the operand. When i changed the code to

std::cout<<"student id= "<<test.c_str()<<" Pass/fail= "<<value<<std::endl; 

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

std::string str_studnt_id = studnt_id;

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:

studnt_id = str_studnt_id.c_str();

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.

+1  A: 

Well that error seems strange. Are you sure it is in the correct line, because it looks OK to me. As for you function. As for you questions:

  1. From a C++-perspective you should be using const std::string & for an input parameter and std::string & for an output parameter. This would look like this:

    bool generate_codes( const std::string & ssn, std::string & student_id )

    The return-value should be boolif you want to express pass or fail.

  2. See my answer to 1.

  3. Your construction of an std::string from a const char * looks OK, assuming that the const char * is really NULL-terminated.

EDIT After reading your question again, I recommend you to think about the difference between changing a pointer and changing the value it points at.

Space_C0wb0y
Indeed I did. Fixed.
Space_C0wb0y
A: 

Inside the function the string variable is allocated on the stack. When you return from the function, the variable is destroyed, hence the garbage value outside

Something you could do would be like this

void my_func(std::string* pmy_string)
{
  std::string &my_string = *pmy_string;
  // use my_string
}


string my_string;

my_func(&my_string);
Eric
Why not pass the string as reference in the first place?
Space_C0wb0y
for one that is not what the api asks for. which means I reckon I could do it but I still need to use char * instead of std::string. I am just not sure how to pass in the address of pointer vs a copy of the address of the pointer which is being destroyed and I am losing everything. so how would I do the above with a char * and then convert it to a string so I can send it to my internal function?
djones2010
A: 

char * student_id will not work as an output parameter as the pointer is passed by value, not by reference. Passing it as a char** would work. Something like the following (though it really should be using std::string and not char*):

int generate_codes(char * ssn, char ** student_id)
{
    *student_id = new char[2];
    (*student_id)[0] = 'Z';
    (*student_id)[1] = '\0';
    return 0;
}
cpalmer
-1 This is not correct. When passing a `char *` the pointer is passed by value and cannot be changed, but the value it points to can. This is however very unsafe.
Space_C0wb0y
Right, that's why the code I posted has an argument of char**
cpalmer
The point is, that `char *` works as an output parameter if the caller allocates the memory for the function to store the value in.
Space_C0wb0y
well in this case I am going to be sending this code to another windows system However, I just want to make sure it works and the only guarentee that i have is that char * will always work vs std::string which may or may not work. I suspect that is why he told us to use char *. But that does not stop me from using std::string internally. So I guess if I were using pointers how would I assign the address of the pointer to the temp string that i have in the function the modify the value placed in that address. thus, std::string = address of char * i am going to be outputting.
djones2010
A: 
  • Prefer references over use of pointers.
  • Pass ssn as const reference so generate_codes can not modify ssn.
  • Use std::string unless it is too slow.

How about:

#include <string>
#include <iostream>

int generate_codes(const std::string& ssn, std::string& student_id)
{
 student_id = "STU-" + ssn;
 return 0;
}

int main()
{
 std::string ssn = "987098765";
 std::string student_id;
 int value = generate_codes(ssn, student_id);
 std::cout << "student id= " << student_id << " Pass/fail= " << value << std::endl;
 return 0;
}

Outputs:

student id= STU-987098765 Pass/fail= 0

Eddy Pronk