What's the difference between
int* a = 0;
and
int* a = 10;
?
int* adeclares a variable aas a pointer to an integer.
int* a
a
=0and =10assign the variable a value.
=0
=10
Note that ais a pointer, its value must be an address.
An address 0has a special meaning: it NULL, represents a null pointer.
0
NULL
The address 10does not matter: it is a random memory address. Since this is not NULL, most functions will consider it as a valid address, dereference it, and thus create problems (undefined behavior) for your application.
10
. !
int *a = 0;
. a int 0 NULL, , . , , , , NULL - .
int
int *a = 10;
. int, 10. -, , int 10 . , 10, , int - . a , a, NULL, , a, 10 - , , // .
0 .
10 . (.. , ), !
, NULL.
-, , .
, . , .
, , , , 10. , undefined .
Here we assign a value to the pointer, int * a = 0; means int a * = NULL; However, in C ++, int * a = 10 does not compile because "Converting from an integer type to a pointer type requires reinterpret_cast, casting in C style or function style" because the compiler considers 10 to be an integral type and not pointer.
Source: https://habr.com/ru/post/1785175/More articles:GlDrawArrays problem - opengl-esFind the maximum possible memory allocation - c ++Writing sub and string concatenation - perlObject Expected Error, javascript, jQuery - javascriptTinyMCE: when I save the content, my HTML code changes. How can I save my HTML? - javascriptPowershell Null or Empty Command Line Options - powershellSelect category tree in Entity Framework - c #Entity structure. Self-reflection table .. get records about depth = x? - c #How to reset a value to its original state if it is changed - javascriptะงัะพ ะพะทะฝะฐัะฐะตั ะฒ OpenXML? - spreadsheetmlAll Articles