C Pointer
Learning C language pointer easy and fun. Through a pointer, you can simplify the implementation of some of the C programming tasks, there are some tasks, such as dynamic memory allocation, no pointer is unenforceable. So, I want to become a good C programmer, learning pointers are necessary.
As you know, each variable has a memory location, each memory location defines the address-of operator access can use the hyphen (&), which represents an address in memory. Consider the following examples, which will define the output variable address:
#include <stdio.h> int main () { int var1; char var2[10]; printf("var1 变量的地址: %x\n", &var1 ); printf("var2 变量的地址: %x\n", &var2 ); return 0; }
When the above code is compiled and executed, it produces the following results:
var1 变量的地址: bff5a400 var2 变量的地址: bff5a3f6
By way of example above, we understand what a memory address and how to access it. Let's look at what is a pointer.
What is a pointer?
A pointer is a variable whose value is the address of another variable, namely, the direct address of the memory location.Before Like other variables or constants, you must use a pointer memory address of other variables, it is declared. The general form of a pointer variable declaration is:
type *var-name;
Here, type is a pointer to the base type, it must be a valid C data type,var-name is the name of the pointer variable. Is used to declare a pointer asterisk * multiplication use the asterisk is the same. However, in this statement, the asterisk is used to specify a variable is a pointer. The following is a valid pointer declaration:
int *ip; /* 一个整型的指针 */ double *dp; /* 一个 double 型的指针 */ float *fp; /* 一个浮点型的指针 */ char *ch /* 一个字符型的指针 */
All pointers actual data type of the value, whether it is an integer, float, string, or other data types are the same, is a hexadecimal number represents a long memory address. The only difference between the different types of data pointer, the pointer is a variable or constant data types.
How to use the pointer?
Will frequently use the pointer when the following actions: define a pointer variable, the variable address assigned to the pointer, access the value of the pointer variable available addresses. This is done by using the unary* operator to return the value of the variable is located in the operand address specified.The following examples relate to these actions:
#include <stdio.h> int main () { int var = 20; /* 实际变量的声明 */ int *ip; /* 指针变量的声明 */ ip = &var; /* 在指针变量中存储 var 的地址 */ printf("Address of var variable: %x\n", &var ); /* 在指针变量中存储的地址 */ printf("Address stored in ip variable: %x\n", ip ); /* 使用指针访问值 */ printf("Value of *ip variable: %d\n", *ip ); return 0; }
When the above code is compiled and executed, it produces the following results:
Address of var variable: bffd8b3c Address stored in ip variable: bffd8b3c Value of *ip variable: 20
The C NULL pointer
At the time of variable declarations, if not the exact address can be assigned, assign a NULL value for the pointer variable is a good programming practice. Fu NULL pointer value is called anull pointer.
A NULL pointer is defined in the standard library zero constants. Consider the following program:
#include <stdio.h> int main () { int *ptr = NULL; printf("ptr 的值是 %x\n", ptr ); return 0; }
When the above code is compiled and executed, it produces the following results:
ptr 的值是 0
On most operating systems, the program does not allow access to memory address 0, because the memory is reserved for the operating system. However, the memory address 0 has a special significance, it indicates that the pointer does not point to an accessible memory location. But according to the convention, if the pointer contains a null value (zero value), it is assumed that it does not point to anything.
To check for a null pointer, you can use the if statement, as follows:
if(ptr) /* 如果 p 非空,则完成 */ if(!ptr) /* 如果 p 为空,则完成 */
C pointer Detailed
In C, there are many pointers related concepts that are very simple, but very important. Listed below are some important concepts associated with the pointer C programmers must be clear:
概念 | 描述 |
---|---|
指针的算术运算 | 可以对指针进行四种算术运算:++、--、+、- |
指针数组 | 可以定义用来存储指针的数组。 |
指向指针的指针 | C 允许指向指针的指针。 |
传递指针给函数 | 通过引用或地址传递参数,使传递的参数在调用函数中被改变。 |
从函数返回指针 | C 允许函数返回指针到局部变量、静态变量和动态内存分配。 |