string copying

Could someone please explain why:

 
    errormsg = "error loading dll";


makes a copy of errormsg? (not what I want)

Here is the function. I want errormsg to be returned whereas what is happening is that a copy of errormsg is being made and the error message is put into the copy, not the one in the argument.

1
2
3
4
5
6
7
8
9
10
11
extern "C" _declspec(dllexport) void test (
                        unsigned long *status,
                        const int     &nPh,
                        char          *errormsg,
                        int           *len)
{
  if (!initialize()) {
     errormsg = "error loading dll";
     return;
  }
}
Could someone please explain why:
errormsg = "error loading dll";
makes a copy of errormsg? (not what I want)
It does not. What it really does, is pointing errormsg pointer to your string literal.
Note that function argument is passed by value and so its change will not be reflected on caller.

You need to change not the argument, but the buffer it points too:
strcpy(errormsg, "error loading dll"); Note that errormsg should point to a buffer of sufficient size, or you have problems.
Topic archived. No new replies allowed.