Công Nghệ

How Strings Work in C++ (and how to use them)

Patreon ►
Instagram ►
Twitter ►
Slack ►

In this video we’re going to talk about how strings work in C++. Strings are groups of characters between double quotes: “this is an example of a string”. We use them to store everything from people’s names to entire paragraphs of text, and we can use them like any other variable. Strings are really just char arrays at the end of the day, and this video takes a deeper look at how they work.

std::string API reference ►

Pointers ►
Arrays ►

Series Playlist ►

Gear I use:
BEST laptop for programming! ►
My FAVOURITE keyboard for programming! ►
FAVOURITE monitors for programming! ►

MAIN Camera ►
MAIN Lens ►
Second Camera ►
Microphone ►

Nguồn: https://vinhtrinh.com.vn

Xem thêm bài viết khác: https://vinhtrinh.com.vn/cong-nghe/

Công Nghệ
Canon 50mm f/1.8 STM – Ống kính ai dùng Canon cũng nên mua!
Công Nghệ
Sony G Master รีวิวเรียงตัว ฉบับดูจบแล้วเลือกถูกเลย!! | KP | KhuiPhai
Công Nghệ
Bị TRUY NÃ còn lên Facebook 'THÁCH THỨC' công an tìm ra mình và cái kết
  • You've made a mistake at 7:03 "[…] otherwise you can just leave it as a char pointer that is totally fine.", implying that it is okay to change a char in that array.

    No it is not! That is undefined behavior and leaving a pointer to a String literal non const it deprecated since C++11. You must not change any value of the C-String-Literal!

  • Hi Cherno. Where do you stand on using char* instead of string class when coding in C++? Would you always favor the usage of string class or can you think of a reason why not to?

  • эх буржуйская кодировка

  • My takeaways:
    1. C style string 5:30, and why he uses char* 9:00, because "anything inside a double quote is a char array" 14:10
    2. C++ style string 11:04
    3. Passing a string to function 16:13, it is better to pass by reference to avoid copy

  • Love your stuff, I am a student learning C++, and your channel is my go to when I need help

  • when passing an object as argument, copy is made on stack, 17:18

  • really good thank you

  • hi Cherno, I hope you will answer when I wrote char n[ ]={'q', 'e'}; it printed without garbage even I did not implement a null character explicitly. Or is that what you said in the video applies only to the debug mode?

  • cool

  • const* char name = "Cherno";
    is giving me error : C++ a value of type cannot be used to initialize an entity of type.
    Can someone explain this to me?

  • Cherno!

    I copied your code, and you have no errors.

    My Code:



    . . char* name = "Luke";


    Resulting Error:

    E0144: a value of type "const char *" cannot be used to initialize an entity of type "char *"

    BUT then I see in your next video that you have to cast (char*) "";
    Why don't you have to cast that in this video???

  • N!ckyN!cky

    Author Reply

    char* name = "Cherno" gives an error message:
    A value of type "const char*" cannot be used to initialize an entity of type "char *".
    Why does it work in the video?

  • 17:05 Why on the heap if you not using the word new?

  • The best C++ series on YouTube.

  • YeknaYekna

    Author Reply

    You no longer need to use #include <string> in order to print strings at least that's what happens to me while using CodeBlocks

  • How is the Qstring arg method used?

  • Cool! Useful

  • char* name = "Cherno"; it's a mistake cause a value of type "const char *" cannot be used to initialize an entity of type" char * why doesn't your IDE underline the text with a red line? WHYYY?

  • These videos are so helpful, you are the MAN!

  • what programming platform you use

  • Your eyes freaked me out completely.

  • You told that string in function arguments dynamically allocated to heap, but it is not. Function arguments is a auto memory (stack allocation).

  • I was able to use 'cout' to print the string to the console without including the string.h header file. But you said it will be an error. How come?

  • char* name ="Cherno"; //E0144 a value of type "const char *" cannot be used to initialize an entity of type "char * – VS 2019

  • Hi why does "cout<<name<<endl;" return the name Cherno and not the address of the character 'C'?

  • Hi can anyone explain why we can declare char* example = "cherno" but not int* example = {1,2,3,4,5}

  • JonJon

    Author Reply

    reason why im just passing on my comp sci tests

  • 6:08
    I wrote same code in my vs2019 but it's giving error stating
    "cannot convert from const char[7] to char *"

  • I tried your example
    char name2[6] = {'c','h','e','r','n','o'}

    and my output was fine, no strange ascii characters or anything. Maybe C++ has updated it since this video?

  • const char * string = "String" is the same as declaring and initializing a const char array?

  • If I want to change a string and pass it by reference, without the const word… for example:

    void ModifyString(std::string& string)
    string+=" – Modified String!";

    am I copying the string to the function?

  • what is the IDE u r using?……it looks good and more user friendly

  • Hi cherno,
    As far I understand the string size when printing is determined by a zero..
    So if i wanted to print this string "cherno007"…
    Then it will print only cherno….and 007 won't be as 0 is considered as the null termination…

  • Oh Man…. This is like Game Of Thrones! a Marathon I can only leave if I fall asleep…! Damn it!

  • Are his videos sped up?

  • Yan, you are a Godsend!

  • Since when is char* variable = "Cherno" a fucking array? And if so, why aren't you deferencing it when you use it later?!!??!!??!!?!?!?!?!?!?!?!?!? These are things you really should talk about. Useless video.

  • Watch at 1.25x thanks me later

  • 8:37

    name in char* name is a pointer variable
    and pointer variable is an integer(address value)
    then how cout<<name; is printing a string instead of integer?

  • Good stuff. VERY helpful. Thank you! Im learning so much

  • when you wrote name2 and then print it , it shows some random characters and you explained about them , but when i do this (using vs code , gcc c++11) i dont get anythin like that. why ?

  • now I know, thanks man

  • I need ur help can u help me plz???