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 ๅ ่ฎธๅฝๆฐ่ฟๅๆ้ๅฐๅฑ้จๅ้ใ้ๆๅ้ๅๅจๆๅ ๅญๅ้ ใ |