Get the App
SLTechnology News&Howtos  ›  Servers  › 

Character pointer and string

Shulou Source: shulou.com Published: 2022-06-02 19:08:16 09月24日 Update

Void getmemory (char p)

{

P = (char) malloc

Strcpy (p, "hello world")

}

Int main ()

{

Char * str=NULL

Getmemory (str)

Printf ("% SSPO", str)

Free (str)

Return 0

} what will be the problem?

[standard answer] the program crashes, malloc in getmemory cannot return dynamic memory, and free () is dangerous for str operations.

Refer to the code on the network:

Void getmemory (char * p)

{

P = (char) malloc

Strcpy (p, "hello world")

}

Int main ()

{

Char str=NULL

Getmemory & str)

Printf ("% SSPO", str)

Free (str)

Return 0

}

Personal comments:

Char str=NULL; is equivalent to defining a string str, which is also a character pointer str.

Getmemory (& str); passes a string address.

Char * p can be thought of as (char) p, which means the pointer p of the string str.

P is the string str. Is also a single-character pointer str.

Tags: Character string pointer danger personal code memory dynamic word address meaning standard annotation program answer question reference Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno vpn Apple OPPO Reno NVidia MariaDB