C language pointer learning notes

xiaoxiao2021-03-19  196

EXAMPLE 1;

INT A, B, * P, * q; * p = a; p = & a; p = q; b = * P;

P = a; error, because P is actually an address, and A is a integer value P = * q; error, because this sentence means: assigns the value of the unit points to which the address Q is assigned to the address P. And in fact, P is an address, can't save * p = & a; error

Note that if * P is defined, * p indicate the value of the unit pointed to by the address (pointer) P, and P represents an address, the pointer P itself can only store the address of the variable it points to, without storing the variable value, * P does not say the value of the variable, which actually represents a value or storage area pointing to the variable, and the specific meaning, such as b = * p represents the operator * Access to the address of the address, but if so * P = & a, it is wrong because * p It is pointing to a storage area instead of an address.

EXAMPLE 2;

Char s [] = "China"; char * p; p = s; this inside, * P and S [0], why? Because the pointer P is pointing to s [0], then * P can be said to be the content of the referenced S [0].

One thing we have to keep in mind that the array name is the address of an array to start an element. Therefore, here, there may be p = s; and P = a in the above example is wrong.

EXAMPLE 3;

Have the following procedure:

/ * Strlen function: Returns the length of string S * /

Int Strlen (Char * S)

{Char * p = s; while (* p! = '/ 0') p ; return p-s;}

In this program, there is a char * p = S, this sentence is not wrong, this sentence is equivalent to char * p; p = s;

This article is a little notes when learning C pointers. If there is a mistake, he also hopes that the human armorine basement is pointed out.

转载请注明原文地址:https://www.9cbs.com/read-130045.html

New Post(0)