How do you insert a string into a dynamic target using realloc ()?

I am struggling with memory allocation. I wanted to enter a string in another, and I made two functions that stopped working in one place - realloc. These features are very similar. First, I copy char to char to a temporary line, and when I try to copy a temporary line to the first, this is the place where I get errors. In the second function, I copy the end of the first line (from a given position) to a temporary line, redistribute the first line (this is where I get errors) and delete everything in me from this position. Then I add a second line and a temporary one for the first line. Here is my code. First function:

// str2 - is a string that I want to input in first string(str)
// at certain position (pos)

void ins (char **str, char *str2, int pos)
{
        // lenght of first and second strings
        int len = strlen(str[0]),
            len2 = strlen(str2),
            i, j, l = 0;

        // creating temporary string
        char *s = (char *) malloc ((len + len2) * sizeof(char));
        // copying first part of first string
        for (i = 0; i < pos; i++)
                s[i] = str[0][i];
        // copying second string
        for (j = 0; j < len2; j++)
                s[i + j] = str2[j]; 
        // copying second part of first string
        for (int k = pos; k < len; k++)
        {
                s[i + j + l] = str[0][k];
                l++;
        }

        // reallocating additional space for second string
        // and copying temporary string to first string
        str[0] = (char *) realloc (str[0], (len + len2) * sizeof(char));
        strcpy(str[0], s);

        free(s);
        s = NULL;
}

Second function:

void ins2 (char **str,char *str2, int pos)
{
        // lenght of first and second string
        int len = strlen(str[0]),
            len2 = strlen(str2);

        // creating a temporary string and copying
        // from the given position
        char *s = (char *) malloc ((len - pos) * sizeof(char));
        strcpy(s, str[0] + pos);

        // reallocating space for string that will be added
        // deleting part of it from the given position
        str[0] = (char *) realloc(str[0], (len + len2) * sizeof(char));
        str[0][pos] = '\0';

        // adding second string and temporary string
        strcat(str[0], str2);
        strcat(str[0], s);

        // be free, temporary string
        free(s);
        s = NULL;
}
+4
source share
1 answer

, , , , realloc(), , ():

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void ins (char **str, const char *str2, size_t pos)
{
    // lenght of first and second strings
    size_t len = strlen(*str);
    size_t len2 = strlen(str2);

    // reallocate new string
    char *tmp = realloc(*str, len + len2 + 1);
    if (tmp != NULL)
    {
        *str = tmp;
        memmove(tmp+pos+len2, tmp+pos, len-pos);
        memcpy(tmp+pos, str2, len2);
        tmp[len+len2] = 0;
    }
}

int main()
{
    char *str = strdup("A simple string");
    char s2[] = "inserted ";

    printf("%s\n", str);
    ins(&str, s2, 9);
    printf("%s\n", str);

    free(str);
    return 0;
}

A simple string
A simple inserted string

  • strlen() . , , .
  • realloc() , , () , . , , ( ). memmove(). memcpy(), memmove() , .
  • , memcpy() . strcpy(), .
  • , 0,

. , - pos ( ), NULL-, , str2 ( NULL) .. , , , , .

+2

Source: https://habr.com/ru/post/1538433/


All Articles