Preparation
- char *p & char p[]
char *p = "hello_world!"
<=equivalent to=>
char p[] = "hello_world!"
‘p’ means a pointer to character string, located in the stack. However, the real charactors content of "hello_world!"
were stored in the RO AREA where it’s not allowed to be overriden.
- char p[20] = “hello_world!”
‘p’ represents that the array name of a charactor array, meawhile, that indicates that the stack address of the first element of a charactor array(&p[0]
)
p --> 'h'
'e'
'l'
'l'
'o'
'_'
'w'
'o'
'r'
'l'
'd'
'!'
'\0'
Access Operation
char str[20] = "hello_world!";
char *p = str; ( or char *p = &str[0];)
- Pointer Access
if we wanna get the content of the designated index position in the charactors array.
p[index] <=> *(p+index)
eg: str[1] ==p[1]
==*(p+1)
== ‘e’
- Mindset Within Structure Array
typedef struct _st{
char *p;
...
struct _st *next;
}MSDN_ST, *MSDN_ST_PTR;
MSDN_ST st[10] ;
MSDN_ST_PTR pst = &st[0];
The same with above charactor pointer access operation. we could do as following if we wanna get the element of the designated index position in the structure array.
eg: st[2] <=> pst[2] <=> *(pst+2)
st[2].p = "hello_world!";
pst[2].p = "hello_world!";
(*(pst+2)).p = "hello_world!";
(MSDN_ST_PTR )(pst+2)->p = "hello_world!"; // Keep in mind do memory mallocation for pointer `p` before assignment operation.