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

  Рет қаралды 16,780

Portfolio Courses

Portfolio Courses

Күн бұрын

Пікірлер: 31
@XM7D_Abasset
@XM7D_Abasset 2 жыл бұрын
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 Жыл бұрын
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: kzbin.info/www/bejne/sKauoomKl66Lhas
@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 Жыл бұрын
Glad I bumped into this video. The concept is perfectly explained. Thank you 🙏
@PortfolioCourses
@PortfolioCourses Жыл бұрын
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 ай бұрын
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 8 ай бұрын
Where was this video when I was learning C as an 18-year-old university student in 1988? 😊
@comoyun
@comoyun 6 ай бұрын
i am 18 and cs student and watching these videos with chatgpt explaining further and very lucky but lazy af
@benjamindreyer9884
@benjamindreyer9884 2 жыл бұрын
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 жыл бұрын
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 жыл бұрын
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 жыл бұрын
@@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 Жыл бұрын
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: kzbin.info/www/bejne/amHcgZyDeMycrrs. 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: kzbin.info/www/bejne/qH6yd2ton6ikatE/. Hopefully this helps!
Dynamic Memory Allocation | C Programming Tutorial
31:51
Portfolio Courses
Рет қаралды 92 М.
How To Return A String From A Function | C Programming Tutorial
13:51
Portfolio Courses
Рет қаралды 17 М.
А я думаю что за звук такой знакомый? 😂😂😂
00:15
Денис Кукояка
Рет қаралды 5 МЛН
What type of pedestrian are you?😄 #tiktok #elsarca
00:28
Elsa Arca
Рет қаралды 40 МЛН
Lamborghini vs Smoke 😱
00:38
Topper Guild
Рет қаралды 51 МЛН
Как Я Брата ОБМАНУЛ (смешное видео, прикол, юмор, поржать)
00:59
C_67 C Program to concatenate two strings | with strcat() and without strcat()
21:30
Dynamically Allocate Memory For An Array Of Strings | C Programming Example
12:10
Why I Use C | Prime Reacts
13:00
ThePrimeTime
Рет қаралды 180 М.
Create Your Own strcat() Function | C Programming Example
8:12
Portfolio Courses
Рет қаралды 7 М.
Dynamically Allocate A 2D Array | C Programming Tutorial
15:58
Portfolio Courses
Рет қаралды 40 М.
What Is Dynamic Programming and How To Use It
14:28
CS Dojo
Рет қаралды 1,6 МЛН
why do void* pointers even exist?
8:17
Low Level
Рет қаралды 389 М.
How To Safely Handle realloc() Failure | C Programming Example
11:11
Portfolio Courses
Рет қаралды 2,4 М.
Master Pointers in C:  10X Your C Coding!
14:12
Dave's Garage
Рет қаралды 329 М.
String In Char Array VS. Pointer To String Literal | C Programming Tutorial
9:58
А я думаю что за звук такой знакомый? 😂😂😂
00:15
Денис Кукояка
Рет қаралды 5 МЛН