c - How do you insert a string within a dynamic target using realloc()? -


I am struggling with memory allocation I wanted to input one string to another and I created two functions Stop working at the same place - realloc These functions are very similar In the first one, I copy around the characters around in a temporary string and when I first try to copy the temporary string that place Is where i am Utian meet. In the second function, I copy the first string (from the given position) to the end of a temporary string, re-assigning the first string (this is where I get the errors) and I get everything from the given position Give it Then I add the second string and temporarily add the string first. Here my code is the first task:

  // str2 - is a string that I string in the first string (str) // certain position (pos) void ins (char ** str, char * Str2), int pauses {// the first and second strings of int len ​​= strlen (str [0]), len2 = strlen (str2), i, j, l = 0; // Create temporary string characters * s = (char *) malloc ((lane + len2) * sizeof (char)); // (i = 0; i & lt; pos; i ++) s [i] = str [0] [i] = To copy the first part of the first string; Copying the second string for // (j = 0; j & lt; len2; j ++) s [i + j] = str2 [j]; // Copy the second part of the first string to copy (int k = pos; k  

Second function:

  zero in 2 (core ** str, char * str2, int pos) {// the first and second string of length Int Len = strlen (str [0]), len2 = strlen (str2); // Create a temporary string and copy // from the given position four * s = (char *) malloc ((lenan - pos) * sizeof (char)); Streakpie (S, St [0] + Pause); // is being allocated again for the string, which removes part of [0] = (four *) relux (str [0], (lenan + len2) * size (four)) from the given position / Will be connected; Str [0] [pos] = '\ 0'; // adding another string and temporary string strakat (str [0], str 2); Dome (str [0], S); // free, temporary string free (s); S = NULL; }  

If you are I think What you are trying to do, you need a realloc () to handle the incoming string, in fact it has already been dynamically allocated (This would be better):

  #include & lt; Stdio.h & gt; # Include & lt; Stdlib.h & gt; #include & lt; String.h & gt; Zero Ince (Four ** SR, Const four * SR2, Size_T Poss) {// First and second strings size size_t len ​​= strlen (* str); Size_t len2 = strlen (str2); // Regenerate new Stroll * tmp = realloc (* str, len + len2 + 1); If (tmp! = NULL) {* str = tmp; Memmove (tmp + pos + len2, tmp + pos, len-pos); Memcopy (TMP + Paus, St2, Lane 2); Tmp [len + len2] = 0; }} Int main () {char * str = strdup ("A simple string"); Four s2 [] = "insert"; Printf ("% s \ n", str); INS (and SR, S2, 9); Printf ("% s \ n", str); Free (STR); Return 0; }  

Output

  A simple string is a simple insert string  

How it works

  • Both pass-in strings are sent through strlen () to get their length. Once we have those that we know how big the resulting buffer should be.
  • Once we buffer realloc () , the original contents are preserved, but we (potentially) open the first string to open a hole for the second string To move. This change, if done, may be required to move overlapped storage (as in the sample). To copy such memory, memmove () is used memcpy () , memmove () support the Library function copy Where the source and destination fields can overlap.
  • Once a hole is made, we do not need any other string in the memcpy () position strcpy () because we already have Know the length.
  • We end the last slot by termination 0 , ending with the completion of the blank end string and operation

Note I have invalid pos (outside of range), zero string, if str2 is empty (or zero), etc. You do not have this cleanliness, but I hope this is clear how it can be done.


Comments

Popular posts from this blog

import - Python ImportError: No module named wmi -

Editing Python Class in Shell and SQLAlchemy -

c# - MySQL Parameterized Select Query joining tables issue -