Char, pointer, cast and string questions

/*1*/ const char *const letter = 'A';

/*2*/ const char *const letter = "Stack Overflow";

Why is 1 invalid but 2 valid?

a letter is a pointer to which an address must be assigned. Are quoted strings addresses? I assume that is why No. 2 is valid and that single quotes are not considered addresses?

Also, what is the difference between these two types of casting ?:

static_cast<>and ().

And finally, if var is a char variable, why does cout <& var <go outside? Why should I use it for void *?

Thank you for your patience with the newbies.

+3
source share
6 answers

'A' , a char, 65 41 16, - ASCII.

"Stack", , , {'S', 't', 'a', 'c', 'k', '\0'}, .

" static_cast ()" , , .

, char var = 'x'; cout << &var ..., , &var - char *, , - cout nul \0 , . :

#include <iostream>
int main() {
    //int q1 = 0;
    char xx = 'x';
    //int q2 = 0;
    std::cout << &xx << std::endl;
    return 0;
}

:

x~Í"

q, , , x ). , C, - , . .

+8

- char, A. , char..

array of characters , , .

char letter [] = { 'H', 'e', 'l', 'l', 'o', '\0' };
char letter [] = "Hello"; 

letter.

0
0

"A" - (, "65" ). , C - , , . , , , , "" , 65.., , , , .

, ( "A" ), , . - , , , , , , .

, static_cast () (type) , static_cast < > () , ( ). (, const_cast (value), () , .

0

char ( int, ), , char l = 'A';, ASCII A l.

char* letters = "foo",

char letters[4];
letters[0] = 'f';
...
letters[3] = '\0';
0

One of the ways that I usually follow is to use the typeid operator to understand the types I'm dealing with.

Thoug type_info :: name returns the string defined by the implementation, usually it is quietly explanatory many times in order to understand what we are dealing with. The output below is shown when compiling with gcc

#include <iostream>
#include <typeinfo>
using namespace std;

int main(){
   cout << typeid('A').name() << endl;                      // prints 'c' meaning char
   cout << typeid("Stack Overflow").name() << endl;         // prints 'A15_c' meaning
                                                            // array of 15 characters
}
0
source

Source: https://habr.com/ru/post/1760688/


All Articles