Should the pointer be initialized before use, then how to understand char * p?

new student something puzzle about a pointer;

As I learned from books, before using a pointer, it must be initialized, so we usually use this as

int a = 12;

int * p = &a; 

therefore, I understand why it is int* p = 12wrong, because it does not have an address;

then I find something today during coding, i.e. from this:

char * months[12] = {"Jan", "Feb", "Mar", "April", "May" , "Jun", "Jul"    
,"Aug","Sep","Oct","Nov","Dec"};

Then another commonly used situation occurred to me. It:

char *p = "string"; (this is ok , why int * a = 12 can't be allowed ?)

I am puzzled. when is it initialized and how? and why int * a = 12can not be automatically initialized? maybe something about memory location.

+4
source share
9 answers

Primarily:

int a = 12;
int* p = &a;

, &a - .

int* p = 12;

- , 12 . , 12 , int* p = &12; ( , ).

, . , :

int a[] = {1, 3, 7, 13};

.

int* p = a; // magic!

- a, *p == 1. p[0] ( 1), p[1] == 3, p[3] == 7 p[4] == 13.

char* foo = "bar" , "bar" - : . . :

"bar"[0] == 'b'
"bar"[1] == 'a'
"bar"[2] == 'r'

( ), . , char* foo = "bar" .

, C99, . , int* p = (int [3]){1, 2, 3}; . int , , C, , .

+8

int* p = 12 , . p .
char *p = "string" , , p .

+3

.

C ++ , 12, int. int int*, : . int *p = 12; .

C , "abc", (, abc char). " " char* ( char) - , . , char *p = "abc";.

: undefined ( C, ++). ( ) ++, const char *.

+3

gcc :

char* p = "hello";

, "" const char *.

:

const char* p = "hello";

, , "" , .

+2

char const*. char*.

C ++ :

char carr[] = "abc";
char carr[10] = "abc";
char const* cp = "abc";

:

int iarr[] = {1, 2, 3};
int iarr[10] = {1, 2, 3};

, :

int const* ip = {1, 2, 3};

, , char const*.

0

int* p = 12 , , , . , , int int *, . , p 12, . , , undefined. , *(int*)12, , .

0

C, , , char (, , lvalue, ); C90 . 12 - , . &(12), . , , . - ; lvalues.

++ , ++ . ++ "", rvalues ​​( ), .

C99 .

0

.

int*  p;
int  *p;
int * p;

: p, NULL, .

,

int * p = 12;

, p 12.

C ++ - , , .

- .

"90210". . , .

"" . , , . 90210 - , [] - .

,

int * p = 12;

, "12" - , - p.

int * p = &12;

, 12, , p.

char* p = "hello";

.

12;      // evaluates to an integer value of 12
"hello"; // evaluates to an integer value which specifies the location of the characters { 'h', 'e', 'l', 'l', 'o', 0 } in memory, with a pointer cast.

int i = 12; // legal
char h = 'h'; // legal
const char* p = "hello"; // legal
uintptr_t p = "hello"; // legal

C ++ , , .

,

"The quick brown fox jumped over the lazy dog"

CPU (32 64 ). , ( ). , C/++ .

0

stackoverflow : NULL. , .

, ( ).

, , ,

int *p = 12;

, :

int *p;
p = malloc(sizeof(p));  /* pointer p holds address of allocated memory */
*p = 12;

:

int *p;
const int a = 12;
p = &a;            /* pointer p holds address of variable a */

Why did the char *p = "string";others answer correctly above. This is just another way to initialize a pointer.

Also, similarly, in one of the answers here on stackoverflow, I found a really good option for your case:

int *p = &(int){12};
0
source

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


All Articles