Timeline for answer to Write C++ string into char* by Hunter McMillen
Current License: CC BY-SA 3.0
Post Revisions
9 events
| when toggle format | what | by | license | comment | |
|---|---|---|---|---|---|
| Dec 9, 2011 at 16:53 | comment | added | Benjamin Lindley |
Also, it would need to be declared as const char *.
|
|
| Dec 9, 2011 at 16:43 | comment | added | Benjamin Lindley | @Hunter: With strcpy, you are copying to the string that the pointer points at. buf is a local copy of whatever pointer was passed in at the call site. Even though it is itself local, the thing it points to is still the same thing that was pointed to by the pointer that it was copied from. And if your assumption was correct, then there is no need to make the char* a parameter. It should just be declared inside the function. | |
| Dec 9, 2011 at 16:40 | comment | added | Hunter McMillen |
I was assuming that the OP abstracted away other code in this function, and that buf is actually used for something. That may or may not be the case, but even using strcpy() wouldn't this still be true?
|
|
| Dec 9, 2011 at 16:39 | comment | added | matthias |
So is version. Even if you make buf a char**, it would be pointing to invalid memory when the function returns.
|
|
| Dec 9, 2011 at 16:37 | comment | added | Benjamin Lindley | @Hunter: I mean that when this function is called, it's effects are not visible to the user. buf is a local variable. | |
| Dec 9, 2011 at 16:37 | comment | added | Terry Li | @BenjaminLindley What you meant? | |
| Dec 9, 2011 at 16:36 | comment | added | Hunter McMillen | @BenjaminLindley What do you mean by that? | |
| Dec 9, 2011 at 16:36 | comment | added | Benjamin Lindley | Your code has no observable behavior. | |
| Dec 9, 2011 at 16:35 | history | answered | Hunter McMillen | CC BY-SA 3.0 |