C style strings, Pointers, arrays

前端 未结 3 668
自闭症患者
自闭症患者 2021-02-01 09:46

I\'m having trouble understanding what a C-style string is. Happy early New Year

What I know: A pointer holds a memory address. Dereferencing the pointer will give you

3条回答
  •  清酒与你
    2021-02-01 10:22

    I know that an array name is a pointer to the first element in an array.

    No, it isn't. It only gets converted into one implicitly (in most cases).

    Do the double quotes around "Hi you" somehow tell the program: "hey I know you are a pointer, and you are initialized to the location of 'H', but because I see these double quotes, skip forward 1 byte in memory location and printout everything you see until you reach a \0"?

    No, they don't. It's the type that matters.

    std::ostream::operator<< has an overload for a generic pointer (void *) and an overload for const char *. So when you write cout << "some char array or pointer";, it will invoke that overload, and not the one for other pointer types. This overload behaves differently: instead of printing the numeric value of the pointer, it prints out everything until a NUL terminator.

提交回复
热议问题