Character pointer and string
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.