c++ - Pointers getting allocated the same memory space -
I am writing a program in C ++ which uses the following function that has two character pointers (of course Working as a telegram) Its formal argument:
zero-techward (four * dstr, const char *) {int i = 0, j = 0; printf ("\ n% s", dstr); getch (); Exit (0); While (i.lifa (from [i]) // before the positioning I ++ in line; do {dstr [j] = to [i]; J ++; I ++; } While (from Islafa ([i])); dstr [j] = '\ 0'; // terminating a word with a nonsense character}; My problem is that the two strings are DST and they are allocated to the same memory space.
As a result, the changes being made in DST are being reflected in which I do not want to don.
So, to make sure that they have allocated different storage spaces?
The only case that does not handle the code that you provide satisfactorily is the case that is dstr> From & amp; Amp; dstr & lt; + strlen (to) to . To fix this, you have three options:
-
You write your function in a manner that ensures that the operation is free Whether or not the two strings overlap. This is likely to involve a temporary buffer.
-
The function declares document that the function will not handle overlapping buffers properly, and make sure that the calling code does not violate this restriction in this case. For both programmers and compilers (which will be able to make additional customization), consider adding this concept to the Restrict keyword in the function arguments for the document. This is also a good idea. -
You go in the opposite direction and change the function signature to void takeword. (Four * string) and the document that it will move the first word to the beginning of the string and add a redundant tap character.
Comments
Post a Comment