Value char * p_c = new char ['1', '2', '3', '4'];

Consider

char *p_c = new char['1', '2', '3', '4'];

Is this syntax correct? If so, what is he doing?

I do not know why, but the compiler allows this syntax! What will he do regarding memory? I cannot access the variable using *p_c. How to determine the size and number of elements present?

+4
source share
3 answers

Your code is syntactically correct C ++ if it is rather weird and I don't think it does what you intended:

new char['1', '2', '3', '4']evaluated as new char['4']due to the operation of the operator comma. (Previous elements are evaluated from left to right, but the value of the expression is the right-most element.)

So your statement is equivalent char *p_c = new char['4'];

'4' - char , , (ASCII, EBCDIC & c. , , .).

, '4' size_t. ASCII 52.

+16

:

identifier = new Type[<expression>];

<expression> ++ , std::size_t. .

<expression> := '1', '2', '3', '4'

'4', std::size_t, (52); , :

char* p_c = new char['4'];
+5
char *p_c = new char['1', '2', '3', '4'];

:

char *p_c = new char['4'];

- . Comma , ( ).

'4' 52 ASCII ( ASCII, C ++, ASCII).

, :

char *p_c = new char[52];
+2

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


All Articles