Your Fifth Day in C (Understanding C Strings) - Crash Course in C Programming

preview_player
Показать описание

►Lesson Description: In this series of lessons I provide you with a crash course in the C programming language. In this lesson I will discuss how strings are represented in the C language. In addition, I'll also talk about the difference between a C string and a String literal. Throughout this discussion we will learn about some of the important C library string functions, implement two of them from scratch (strlen and strcat), and then wrap up discussion showing why it is important to have a null terminating byte your C strings.

00:00 Introduction
00:47 C-strings are an array of characters
01:58 The Null terminating byte
3:33 Modifying characters in the C-string (i.e. character array)
5:40 Exercise: Writing a string length function
8:20 The strlen function in string.h
9:30 'const' in C
10:58 size_t datatype in C
12:35 Working with a dynamically allocated string with malloc (i.e. char*)
14:55 String Literals in C
16:30 C puts in the null terminator for string literals
17:03 String literals are read-only
21:52 Exercise: Writing a string append function
31:57 Bug fixes and a refresher on pass-by-value
34:55 A working ''StringAppend'' function demo
35:34 Comparing our StringAppend with strcat in C Standard Library\
36:36 Refactoring our StringAppend
40:05 Simplifying our StringAppend with realloc
44:45 Improving our StringAppend performance with memcpy
47:20 What happens without the Null Byte?
50:20 Review on temporary, stack allocated memory
52:03 Wrap up and Conclusion

►Please like and subscribe to help the channel!
Рекомендации по теме
Комментарии
Автор

This been the most difficult so far. Specially when understanding why the dst string can't be modified. I think I need some exercises with pointers to better understand that.

kowalski
Автор

I am really enjoying this series. Thank you, Shah!

Ptr-NG
Автор

I don't like strings in any language, it's even more suffering here :D It's good to see that everyone makes mistakes :) And it's great that they're not constantly discussing basics here, making videos takes effort and I appreciate that you did it (although you could work on video editing :D)

rudolfbouzek
Автор

HI, thanks for the great series, much appreciated topic that not a lot making videos about! Thats the first time on fifth day I actually didn't understood smth - I was a bit confused of why your first version of append function, where you mutated first argument instead of returning appended string in return statement didn't worked out. Could you explain it in short terms mb? Cheers!

KyrychenkoAnton
Автор

Wonderful explanation as always. Thank you.

Curious what happens to the memory allocated to the string in the block scope if its is correctly null terminated. Will it be reclaimed and erased or it is not reclaimed till the function returns?

Couple of doubts
1. Isn’t it redundant to use realloc if we are using memcpy and vice versa?
2. If we are using realloc or memcpy, then is it really necessary(or a good practice) to return a new string as we are making changes to the destination string memory itself?
3. I created different versions of StringAppend as below(shorter versions) and it works fine, can you please point me if I’m doing it wrong?

```// 1. Using memcpy

void StringAppend(char* dest, const char* src){
size_t str1len = strlen(dest);
size_t str2len = strlen(src);
memcpy(dest+str1len, src, str2len);
dest[str1len+str2len] = '\0';
}
```

```// 2. Using realloc (We don’t need the initial loop to copy the src1)

void StringAppend(char* dest, const char* src){
size_t str1len = strlen(dest);
size_t str2len = strlen(src);

char* newstring = (char*)realloc(dest, sizeof(char)* (str1len + str2len + 1));

//copy the second string to the first string
int i=0;
while(i<str2len){
newstring[str1len+i] = src[i];
i++;
}

newstring[str1len+str2len] = '\0';
}
```

Testing
```
int main(){
//testing append
char* message =
message[0] = 'h';
message[1] = 'i';
message[2] = '\0';
StringAppend(message, " there");
printf("%s \n", message);
return 0;
}
```

Thank you again.

dhanushs
Автор

You make a mess with naming variables and changing them after. And your naming style makes my eyes painful. You should change this first, then learn how to use IDE and other stuffs. You can type well, but first learn the machine, not the language which a tool. And please, don't say nothing about assembly language. You don't know it well to make C standard library functions. Play with stuffs you already know and be happy.

meaningfulname
join shbcf.ru