C language study notes 14

Address pointer:

The definition of a variable in the program, will be assigned an address in memory to that variable during compilation can be found by visiting the variables needed to address this. This variable is called a pointer address of the variable.

Shaped like:

1000 is shown above the address pointer variable i.

Variables and pointers:

Address of the variable is the link between the two variables and a pointer, if a variable contains the address of another variable, it can be understood as a first variable point to the second variable.

After the point is, the address to be embodied as a variable pointer variable is pointing to the address, so the address of a variable is assigned to the pointer variable, the pointer variable to point to the variable.

Shaped like:

The address of the variable i is stored in the pointer variable p, p is on the point i.

Pointer variable:

If there is a special variable used to store the address of another variable, he is the pointer variable.

Pointer type:

C language to have a special type of variable address storing means, i.e., pointer type.

1, the general format of the pointer variable:

Defined pointer variable:

Type Description * variable name;

Wherein * indicates that the variable is a pointer variable, the variable name is the name of the pointer variable defined, this type description pointer indicates the data type of the variable points of the variable.

2, pointer variable assignment:

Assigned to the pointer variable can only be given an address, but can not confer any other data.

C language provides address operator "&" to indicate the address of the variable.

The general form is:

& variable name;

Such as: & a variable indicating the address of a, & b represents the address of the variable b.

(1) the definition of indicator variables simultaneously assigned.

int a;

int *p=&a;

After re-assignment (2) define a pointer variable.

int a;

int * p;

p=&a;

note:

If you do not add assignment "*." After you have defined a pointer variable

Problem: simulation scenarios: output standard answer, assuming Z is the class homework students the best students, we all like to copy his answers,

A classmate classmate one day to find Z: I refer to the work by, Z student said: Y standard answer in there, Y say the answer is 10. code show as below:

Note: No value assigned to a pointer variable.

3, pointer variable references:

Reference pointer variable is a form of indirect access for variables.

* Pointer variable

Meaning a reference value of the pointer variable points.

Problem: Given any two variables, the pointer to program the two exchanging data. code show as below:

4, "&" and "*" operator:

运算符"&"是一个返回操作数地址的单目运算符,叫做取地址运算符。

p=&i;

就是将变量i的内存地址赋给p。

运算符" * "是单目运算符,叫做指针运算符。

作用:返回指定的地址内的变量值。

q=*p;

5、"&*"和"*&"的区别:

int a;

p=&a;

习题:9头羊渡河,他们找来一支能载3头羊的木头,如果只有一头羊会划木船,那么至少几次能全部渡过河?代码如下:

指针自增自减运算:

基本整型变量i在内存中占4个字节,指针是指向变量i的地址的,这里的p++不是简单的在地址上加1,而是指向下一个存放基本整型数的地址。

 

Guess you like

Origin www.cnblogs.com/www-bokeyuan-com/p/11206227.html