String append (i.e. concatenation) with dynamic memory allocation | C Programming Example

Поделиться
HTML-код
  • Опубликовано: 1 дек 2024

Комментарии • 31

  • @XM7D_Abasset
    @XM7D_Abasset 2 года назад +5

    Best channel in C programming, thank you very much Dr for helping us ❤

    • @PortfolioCourses
      @PortfolioCourses  2 года назад

      Thank you very much for the kind words Mohammed, and you're very welcome! :-)

  • @airrsongs
    @airrsongs Год назад +2

    Love the channel.
    Technically, this isn't appending strings but rather joining strings. 😉
    My simplified approach would be:
    char *join_strings(char *s1, char *s2) {
    int size = strlen(s1) + strlen(s2) + 1;
    char * buf = calloc(size, sizeof(char));
    snprintf(buf, size, "%s%s", s1, s2);
    return buf;
    }
    snprintf uses the same format options as printf; the first argument is the destination buffer, the second argument is the number of characters to place in the buffer, for those wondering...

    • @PortfolioCourses
      @PortfolioCourses  Год назад

      I’m glad you enjoy the channel, and thanks for sharing your code! :-) What is in this video is technically an append (or concatenation), though all these terms are used a bit loosely in practice. The word join for strings usually refers to an operation that joins together potentially multiple strings and may insert separator characters between them, like this: ruclips.net/video/zuKqVTbIXOI/видео.html

    • @airrsongs
      @airrsongs Год назад

      @@PortfolioCourses I see your point, especially about the terms being used a bit loosely. Still, in my mind at least, append implies an in-place concatenation. But it's all good, I'll check out your other video!
      Also, thanks for all the content!

  • @staz8671
    @staz8671 Год назад +1

    Glad I bumped into this video. The concept is perfectly explained. Thank you 🙏

    • @PortfolioCourses
      @PortfolioCourses  Год назад +1

      You’re welcome, I’m glad you found the video too! :-)

  • @TiagoSilva-wk6se
    @TiagoSilva-wk6se 9 месяцев назад

    Correct me if I'm wrong, but I understood that you were going to append s2 to s1. I mean, the code appends perfectly and I learned quite a lot, but I tried to do an implementation where the function had void return and appended s2 to s1 instead and it was actually harder.

  • @justcurious1940
    @justcurious1940 11 месяцев назад

    Thanks for the video : I'm not sure but I think that the last line is not necessary (s[size-1] = '\0'; ) because Calloc does it for u 🙃 :
    char *string(char *s1, char *s2){
    int s1_length = strlen(s1);
    int s2_length = strlen(s2);
    int size = s1_length + s2_length +1;
    char *s = calloc(size, sizeof(char));
    for(int i = 0 ; i < s1_length ; i++)
    s[i] = s1[i];
    for(int i = 0 ; i < s2_length ; i++)
    s[i+s1_length] = s2[i];
    return s;
    }

  • @comoyun
    @comoyun 6 месяцев назад +1

    everything is fine but white background really hurts my eyes especially at night

  • @sarker_sudi21
    @sarker_sudi21 Год назад

    Thank you for the explanation. I had one question.
    The function string_append receives two char pointer, but when u are passing in those parameters in the main you are passing in char arrays s1 and s2 instead of passing pointers (like char *s1 = "abc";). How does this still work ?

    • @mjsaedy6637
      @mjsaedy6637 Год назад

      In C when an array is passed as an argument to a function, it decays into a pointer. This means that inside the function it becomes a pointer.

  • @ElhamShadab
    @ElhamShadab Год назад

    Hi , Is it possible to concatenate multiple strings in multithreading environment like below?
    1) capture all strings in character pointer array variable
    2) calculate the size of strings and store it in array
    3) allocate dynamic memory using total size (after summation of array defined in point 2)
    4) now call multiple threads to operate on different portion of buffer e.g. if buffer of 100 bytes were allocated then thread0 working for two strings starting from index 0 to index 19 (assuming some of bytes of both the strings are 20) and thread1 working on next two strings starting from index 20 to 39 (assuming some of bytes of both the strings are 20).....

    • @PortfolioCourses
      @PortfolioCourses  Год назад

      That's probably possible Moahammad, though I'm not sure if that would really improve performance. With memory, accessing it using many threads instead of one, doesn't tend to improve performance, due to the way hardware works (we can think of all the threads as accessing memory through the same "pipe", even if we have more threads using the pipe, we have the same pipe with the same limits on speed, etc).

  • @alxbudn
    @alxbudn 18 дней назад

    why didnt you use malloc i wanna learn why

  • @mensaswede4028
    @mensaswede4028 7 месяцев назад

    Where was this video when I was learning C as an 18-year-old university student in 1988? 😊

    • @comoyun
      @comoyun 6 месяцев назад +1

      i am 18 and cs student and watching these videos with chatgpt explaining further and very lucky but lazy af

  • @benjamindreyer9884
    @benjamindreyer9884 2 года назад +1

    What if the paramaters are also dynamically allocated on the heap, do you have to free them in order to avoid a memory leak?

    • @PortfolioCourses
      @PortfolioCourses  2 года назад

      Yes, that's correct. :-)

    • @benjamindreyer9884
      @benjamindreyer9884 2 года назад

      @@PortfolioCourses I played with it and wrote a function that works, its really much the same as yours. Thank you for explaining it so well!

    • @PortfolioCourses
      @PortfolioCourses  2 года назад +1

      You’re welcome Benjamin! :-)

  • @danielatoche5274
    @danielatoche5274 2 года назад

    hi, is it possible to allocate memory for a str that I get with scanf or gets?

    • @PortfolioCourses
      @PortfolioCourses  2 года назад +3

      Great question Daniel! :-) Yes, but you would need to either dynamically allocate the memory *before* calling scanf or gets, OR you could use a regular char array with scanf or gets and then after copying the string from the char array to space that you dynamically allocate. If you use the 2nd approach, you could initially make the char array very large, but then create only the amount of space need for the dynamically allocated memory based on the string length entered. Maybe something like this:
      char buffer[4096];
      scanf("%s", buffer);
      char *string = malloc(sizeof(char) * (strlen(buffer) + 1));
      strcpy(string, buffer);
      Then you're only using the amount of space necessary to store the string, and no more than that.

    • @danielatoche5274
      @danielatoche5274 2 года назад +1

      @@PortfolioCourses thank you very much!

    • @PortfolioCourses
      @PortfolioCourses  2 года назад

      @@danielatoche5274 You're very welcome! :-D

  • @phililezondi8813
    @phililezondi8813 Год назад

    how do you append without using calloc

    • @PortfolioCourses
      @PortfolioCourses  Год назад +1

      Great question Philile! :-) I'm assuming that you mean "how do you append without using dynamic memory allocation (malloc, calloc, realloc)". You could use the built-in strcat() function: ruclips.net/video/40yPiMCgixY/видео.html. Though you would need to make sure that you destination char array was large enough to hold the concatenated string. You could also make your own strcat() function like this, if you wanted to see how it can be implemented: ruclips.net/video/rMOF82jCq4o/видео.html/. Hopefully this helps!