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://federicobellucci.net/

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


Article Categories:
Công Nghệ

Comments

  • 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!

    Brotcrunsher July 23, 2020 3:31 pm Reply
  • Thanks for the awesome videos!

    Stephen Planck July 23, 2020 3:31 pm Reply
  • You look like elon musk

    Chris Fritzke July 23, 2020 3:31 pm Reply
  • I actually very much like the way you write your code. I mean: when you write the idea then make affectations and nominations. thank you.

    Tar Ik July 23, 2020 3:31 pm Reply
  • #include <iostream>
    int main()
    {
    char* name = "Cherno";
    name[2] = 'a';
    }
    error: cannot use "const char" value to initialize "char *" entity

    NeviX July 23, 2020 3:31 pm Reply
  • Video starts at @5:00

    Rico C July 23, 2020 3:31 pm Reply
  • But i thought the string are just pointers so technically you are passing it by reference?

    Xrafter July 23, 2020 3:31 pm Reply
  • If you add a 0 character to the end of a C# string it messes it up in visual studio when you try to observe the value.

    julian mcfarlane July 23, 2020 3:31 pm Reply
  • let do it with your activity not saying.

    Sầu Đâu July 23, 2020 3:31 pm Reply
  • The Cherno is a god sent gift lol

    DuhBaconStrip July 23, 2020 3:31 pm Reply
  • Wow, very clear explanation

    Jerferson Matos July 23, 2020 3:31 pm Reply
  • super helpful!! thanks

    Onur Ucar July 23, 2020 3:31 pm Reply
  • another EXCELLENT video!

    Reean July 23, 2020 3:31 pm Reply
  • Replacing my Netflix binging with TheCherno binging. Loving these videos!

    Danil Ivanov July 23, 2020 3:31 pm Reply
  • And here I thought I understood strings.

    Vivek Kumar Bhagat July 23, 2020 3:31 pm Reply
  • What is that accent? Aussie?

    Vivek Kumar Bhagat July 23, 2020 3:31 pm Reply
  • Great video.

    DubTron 666 July 23, 2020 3:31 pm Reply
  • Whats your IDE?

    Daniel Björkman July 23, 2020 3:31 pm Reply
  • char* is a mutable pointer to a mutable character/string.

    const char* is a mutable pointer to an immutable character/string. You cannot change the contents of the location(s) this pointer points to. Also, compilers are required to give error messages when you try to do so. For the same reason, conversion from const char * to char* is deprecated.

    char* const is an immutable pointer (it cannot point to any other location) but the contents of location at which it points are mutable.

    const char* const is an immutable pointer to an immutable character/string.

    Keshav jha July 23, 2020 3:31 pm Reply
  • 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?

    Rodrigo Macedo July 23, 2020 3:31 pm Reply
  • эх буржуйская кодировка

    demmordor July 23, 2020 3:31 pm Reply
  • 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

    Lei Xun July 23, 2020 3:31 pm Reply
  • Love your stuff, I am a student learning C++, and your channel is my go to when I need help

    Wes Gause July 23, 2020 3:31 pm Reply
  • when passing an object as argument, copy is made on stack, 17:18

    cosmin cosma July 23, 2020 3:31 pm Reply
  • really good thank you

    שקד יעקובי July 23, 2020 3:31 pm Reply
  • 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?

    Daniyel Zhumankulov July 23, 2020 3:31 pm Reply
  • cool

    血涩狼 July 23, 2020 3:31 pm Reply
  • 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?

    soumyadeep pathak July 23, 2020 3:31 pm Reply
  • Cherno!

    I copied your code, and you have no errors.

    My Code:

    main()

    {

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

    Luke Nukem July 23, 2020 3:31 pm 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?

    Nikolas Kaminsky July 23, 2020 3:31 pm Reply
  • 17:05 Why on the heap if you not using the word new?

    MikeOnTheBox July 23, 2020 3:31 pm Reply
  • The best C++ series on YouTube.

    Injae Lee July 23, 2020 3:31 pm 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

    Yekna July 23, 2020 3:31 pm Reply
  • How is the Qstring arg method used?

    tinashe chaza July 23, 2020 3:31 pm Reply
  • Cool! Useful

    Dima Rysev July 23, 2020 3:31 pm Reply
  • 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?

    eric c July 23, 2020 3:31 pm Reply
  • These videos are so helpful, you are the MAN!

    CacheTaFace July 23, 2020 3:31 pm Reply

Leave a Comment

Your email address will not be published. Required fields are marked *