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

preview_player
Показать описание
Рекомендации по теме
Комментарии
Автор

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

XM_XCodez
Автор

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...

airrsongs
Автор

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

staz
Автор

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.

TiagoSilva-wkse
Автор

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;
}

justcurious
Автор

why didnt you use malloc i wanna learn why

alxbudn
Автор

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

comoyun
Автор

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 ?

sarker_sudi
Автор

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

benjamindreyer
Автор

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

mensaswede
Автор

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

ElhamShadab
Автор

how do you append without using calloc

phililezondi
Автор

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

danielatoche